Generic Linked Lists find duplicates - linked-list

Good day all,
I am studying Bsc-IT but am having problems.
With the current covid-19 situation we have been left to basically self-study and I need someone to put me in the right direction (not give me the answer) with my code.
I must write a program called appearsTwice that receives a linked list as parameter and return another list containing all the items from the parameter list that appears twice or more in the calling list.
My code so far(am I thinking in the right direction? What must I look at?)
public MyLinkedList appearsTwice(MyLinkedList paramList)
{
MyLinkedList<E> returnList = new MyLinkedList<E>(); // create an empty list
Node<E> ptrThis = this.head;//Used to traverse calling list
Node<E> ptrParam= paramList.head; //neither lists are empty
if (paramList.head == null) // parameter list is empty
return returnList;
if (ptrThis == null) //Calling list is empty
return returnList;
for (ptrThis = head; ptrThis != null; ptrThis = ptrThis.next)
{
if (ptrThis == ptrThis.element)
{
returnList.append(ptrThis.element);
}
}

Some issues:
Your code never iterates through the parameter list. The only node that is visited is its head node. You'll need to iterate over the parameter list for every value found in the calling list (assuming you are not allowed to use other data structures like hashsets).
if (ptrThis == ptrThis.element) makes little sense: it tries to compare a node with a node value. In practice this will never be true, nor is it useful. You want to compare ptrParam.element with ptrThis.element, provided that you have an iteration where ptrParam moves along the parameter list.
There is no return statement after the for loop...
You need a counter to address the requirement that a match must occur at least twice.
Here is some code you could use:
class MyLinkedList {
public MyLinkedList appearsTwice(MyLinkedList paramList) {
MyLinkedList<E> returnList = new MyLinkedList<E>();
if (paramList.head == null) return returnList; // shortcut
for (Node<E> ptrParam = paramList.head; ptrParam != null; ptrParam = ptrParam.next) {
// For each node in the param list, count the number of occurrences,
// starting from 0
int count = 0;
for (Node<E> ptrThis = head; ptrThis != null; ptrThis = ptrThis.next) {
// compare elements from both nodes
if (ptrThis.element == ptrParam.element) {
count++;
if (count >= 2) {
returnList.append(ptrParam.element);
// no need to look further in the calling list
// for this param element
break;
}
}
}
}
return returnList; // always return the return list
}
}

Related

How can I count all elements in a DXL skip list

I am trying to count all elements within a Skip list without having to know the type of the element contained within it.
like this:
Module mod = current()
Skip skip = create()
put(skip, 1, "test")
put(skip, 2, mod)
print count(skip) // Returns integer
As far as I know, DOORS does not support this natively. To create this functionality I took advantage of an odd loop behavior where the object in a loop doesn't get assigned until you actually use it:
int count(Skip skip)
{
if(null skip)
return 0
int i = 0
// Doesn't get assigned unless you do something like obj = obj within the loop
string obj = null
for obj in skip do
{
i++
}
return i
}

search in maps dart2 , same as list.indexOf?

I Use this sample for search in Map but not work :|:
var xmenList = ['4','xmen','4xmen','test'];
var xmenObj = {
'first': '4',
'second': 'xmen',
'fifth': '4xmen',
'author': 'test'
};
print(xmenList.indexOf('4xmen')); // 2
print(xmenObj.indexOf('4xmen')); // ?
but I have error TypeError: xmenObj.indexOf$1 is not a function on last code line.
Pelease help me to search in map object simple way same as indexOf.
I found the answer:
print(xmenObj.values.toList().indexOf('4xmen')); // 2
or this:
var ind = xmenObj.values.toList().indexOf('4xmen') ;
print(xmenObj.keys.toList()[ind]); // fifth
Maps are not indexable by integers, so there is no operation corresponding to indexOf. If you see lists as specialized maps where the keys are always consecutive integers, then the corresponding operation should find the key for a given value.
Maps are not built for that, so iterating through all the keys and values is the only way to get that result.
I'd do that as:
K keyForValue<K, V>(Map<K, V> map, V value) {
for (var entry in map.entries) {
if (entry.value == value) return key;
}
return null;
}
The entries getter is introduced in Dart 2. If you don't have that, then using the map.values.toList().indexOf(value) to get the iteration position, and then map.keys.elementAt(thatIndex) to get the corresponding key.
If you really only want the numerical index, then you can skip that last step.
It's not amazingly efficient (you allocate a new list and copy all the values). Another approach is:
int indexOfValue<V>(Map<Object, V> map, V value) {
int i = 0;
for (var mapValue in map.values) {
if (mapValue == value) return i;
i++;
}
return -1;
}
You can search using .where(...) if you want to find all that match or firstWhere if you assume there can only be one or you only want the first
var found = xmenObj.keys.firstWhere(
(k) => xmenObj[k] == '4xmen', orElse: () => null);
print(xmenObj[found]);

Dart Cannot find a value in the map even through the keys are equal

I have a map that has a complex object as a key
Map<TimeseriesNode , MyObject> myMap = {};
TimeseriesNode class has implemented hashCode and == operator
class TimeseriesNode {
String product;
String model;
String element;
String locationName;
String locationSuffix;
TimeseriesNode.create(this.product, this.model, this.element, this.locationName, this.locationSuffix);
int get hashCode {
return hashObjects([product, model, element, locationName, locationSuffix]);
}
bool operator ==(other) {
if (other is! TimeseriesNode) return false;
TimeseriesNode key = other;
return (key.element == element
&& key.locationName == locationName
&& key.locationSuffix == locationSuffix
&& key.model == model
&& key.product == product);
}
}
(method hashObjects comes from import "package:quiver/core.dart";)
One part of my application creates the keys and adds them to the map.
Another part of the application creates a new TimeseriesNode (which is equal to the original key) and then uses this instance to query the map.
MyObject obj = myMap[ node];
Oddly the map returns null. I have done some debugging and found that myMap[node] calls the following code in the dart:collection-patch_compact_hash code
V operator [](Object key) {
var v = _getValueOrData(key);
return identical(_data, v) ? null : v;
}
When I inspect v, I can see 'v' is the object that was originally added to the map, but the code returns null.
If I put a break point on my equals method, it is never called.
What is going on?
The fields you use to calculate the hashcode should be immutable (final). I guess you change one of these fields after you inserted the element into the map. This results in the map not finding the instance by hashcode and therefore doesn't reach the state where it does the equals check.

breeze query where clause is not executed

I'm quite new to using breeze and at the moment stuck with something which seems very simple.
I have a API call which returns 4 locations. Then using breeze, I'm trying to filter it down using a where clause as follows:
function getLocations(clientId) {
var self = this;
return EntityQuery.from('GetLocations')
.withParameters({ clientId: clientId })
.where("activeStatus", "==", "0")
.expand('LocationType')
.using(self.manager)
.execute()
.then(querySucceeded, this._queryFailed);
function querySucceeded(data) {
if (data.results.length > 1) {
locations = data.results;
}
return locations;
}
}
Ideally, this should give me 0 rows, because in all 4 rows the 'activeStatus' is 1. However, it still shows me all 4 results. I tried with another filter for locationType, and it's the same result. The breeze side where clause does not get executed.
Update to answer the questions:
Following is how the API call in my controller looks like:
public object GetLocations(int clientId) {
}
As you see it only accepts the clientId as a parameter hence I use the with parameter clause. I was thinking that breeze will take care of the activeStatus where clause and I don't have to do the filter on that in the back-end. Is that wrong?
Can someone help with this?
The Breeze documentation indicates that the withParameters is usually used with non-.NET backends or servers which do not recognize oData URIs. Is it possible that the where clause is being ignored because of .withParameters? Can't you rewrite the where clause using the clientID filter?
function getLocations(clientId) {
var self = this;
var p1 = new breeze.Predicate("activeStatus","==","0");
var p2 = new breeze.Predicate("clientId","==",clientId);
var p = p1.and(p2)
return EntityQuery.from('GetLocations')
.where(p)
.expand('LocationType')
.using(self.manager)
.execute()
.then(querySucceeded, this._queryFailed);
function querySucceeded(data) {
if (data.results.length > 1) {
locations = data.results;
}
return locations;
}
}
I'd try this first. Or put the where clause in the withParameters statement, depending on your backend. If that doesn't work, then there might be other options.
Good Luck.
EDIT: An example that I use:
This is the API endpoint that I query against:
// GET: breeze/RST_ClientHistory/SeasonClients
[HttpGet]
[BreezeQueryable(MaxExpansionDepth = 10)]
public IQueryable<SeasonClient> SeasonClients()
{
return _contextProvider.Context.SeasonClients;
}
And here is an example of a query I use:
// qFilters is object. Values are arrays or strings, keys are id fields. SeasonClients might also be Clients
// Setup predicates
var p, p1;
// link up the predicates for passed data
for (var f in qFilters) {
var compareOp = Object.prototype.toString.call(qFilters[f]) === '[object Array]' ? 'in' : '==';
if (!qFilters[f] || (compareOp == 'in' && qFilters[f].length == 0)) continue;
fLC = f.toLowerCase();
if (fLC == "countryid") {
p1 = breeze.Predicate("District.Region.CountryId", compareOp, qFilters[f]);
} else if (fLC == "seasonid") {
p1 = breeze.Predicate("SeasonId", compareOp, qFilters[f]);
} else if (fLC == "districtid") {
p1 = breeze.Predicate("DistrictId", compareOp, qFilters[f]);
} else if (fLC == "siteid") {
p1 = breeze.Predicate("Group.Site.SiteId", compareOp, qFilters[f]);
} else if (fLC == "groupid") {
p1 = breeze.Predicate("GroupId", compareOp, qFilters[f]);
} else if (fLC == "clientid" || fLC == 'seasonclientid') {
p1 = breeze.Predicate("ClientId", compareOp, qFilters[f]);
}
// Setup predicates
if (p1) {
p = p ? p.and(p1) : p1;
}
}
// Requires [BreezeQueryable(MaxExpansionDepth = 10)] in controller
var qry = breeze.EntityQuery
.from("SeasonClients")
.expand("Client,Group.Site,Season,VSeasonClientCredit,District.Region.Country,Repayments.RepaymentType")
.orderBy("DistrictId,SeasonId,GroupId,ClientId");
// Add predicates to query, if any exist
if (p) qry = qry.where(p);
return qry;
That's longer than it needs to be, but I wanted to make sure a full working example is in here. You will notice that there is no reason to use .withParameters. As long as the Context is set up properly on the server, chaining predicates (where clauses) should work fine. In this case, we are creating where clauses with up to 10 ANDs filtering with strict equality or IN a collection, depending on what is passed in the qFilters Object.
I think you should probably get rid of the parameter in your backend controller, make the method parameterless, and include the clientId match as an additional predicate in your query.
This approach also makes your API endpoint much more flexible -- you can use it for a wide variety of queries, even if the ClientId has nothing to do with them.
Does this help? Let me know if you have any more questions?

How do i select a record in a grouped smartgwt listgrid?

I have site with a listgrid and a openlayers map with points. When i cklick on one of these, the application shall scroll and mark this record. This works with a standard listgrid, but with a grouped listgrid it does not work.
lg = new ListGrid();
lg.setWidth(330);
lg.setDataSource(ds1);
lg.setAutoFetchData(true);
lg.setSortField("KU_NAME");
lg.setGroupStartOpen(GroupStartOpen.ALL);
lg.setGroupByField("KU_NAME");
lg.setShowFilterEditor(true);
kuName = new ListGridField("KU_NAME", "Künstler Name",150);
// Standorte
ListGridField stdOrt = new ListGridField("STDORT_NR","Standort Nr.");
ListGridField oid = new ListGridField("OID","OID.");
lg.setFields(stdOrt,kuName,oid);
and the select:
String stdortOID = stdOrtOIDjso.toString();
ListGridRecord[] records = lg.getRecords();
int i;
for (i = 0; i < records.length; i++) {
if (records[i].getAttribute("OID").equalsIgnoreCase(stdortOID)){
break;
}
}
lg.deselectAllRecords();
lg.selectRecord(i);
lg.scrollToRow(lg.getRecordIndex(record));
the reason is that in the record is only the value of the group name and the other attributs are unavailable.
When grouping is enabled, all data are "transformed" into tree and listgrid itself contains data for groups so you have to look for your record in this tree. Replace last 3 lines with (modified) Vittorio Paternostro suggestion:
Tree tree = lg.getGroupTree();
if (tree != null) {
TreeNode node = tree.find("OID", stdortOID);
if (node != null) {
lg.selectSingleRecord(node);
lg.scrollToRow(getRecordIndex(node));
lg.markForRedraw();
}
}
Note: Instead of deselectAllRecords + selectRecord use simplified selectSingleRecord.
I had the same need and the following works fine for me. You can use getGroupTree() and search the desired property in it (column value) without worrying about grouping. Make sure you search for unique values (i.e. a unique key) to identify a precise node.
Tree tree = getGroupTree();
if (tree != null) {
TreeNode node = tree.find("property", "value");
if (node != null) {
selectSingleRecord(node);
scrollToRow(getRecordIndex(node));
markForRedraw();
}
}

Resources