In: Computer Science
Can I get some simple pseudocode (simple descriptions as exampled in top italicized line) added to this small section of Java code? Thumbs up always left for answers!
-------------------------------------------------------------------------------------------------------
// Check to see if two bags are equals.
public boolean equals(LinkedBag<T> aBag)
{
boolean result = false; // result
of comparison of bags
if (this.numberOfEntries ==
aBag.numberOfEntries) {
if
(numberOfEntries == 0) {
return true;
}
int i = 0, j =
0;
Node scout =
firstNode;
Node retrieve =
aBag.firstNode;
while ((i <
numberOfEntries) && (scout != null)) {
while ((j < aBag.numberOfEntries) &&
(retrieve != null)) {
if
(scout.data.equals(retrieve.data)) {
int freq1
= this.getFrequencyOf(scout.data);
int freq2
= this.getFrequencyOf(retrieve.data);
if (freq1
== freq2) {
result = true;
} else
{
result = false;
}
} // end if
retrieve =
retrieve.next;
j++;
}
scout = scout.next;
i++;
} // end
while
} else {
result =
false;
}
return result;
} // end equals
//Duplicate all the items in a bag.
public boolean duplicateAll() {
boolean success = true;
Node scout = firstNode;
int i = 0;
while (i < numberOfEntries)
{
Node temp = new
Node(scout.data, firstNode);
firstNode =
temp;
scout =
scout.next;
i++;
}
numberOfEntries *= 2;
return success;
} // end duplicateAll
// Remove all duplicate items from a bag
public void removeDuplicates() {
System.out.print("Initial >
");
System.out.println(this.toString());
LinkedBag<T> newBag = new
LinkedBag<T>();
if (numberOfEntries == 0)
{
return; // Do
nothing
}
// Create filler values to avoid null pointer
for (int k = 0; k <
numberOfEntries; k++) {
newBag.add((T) "_");
}
Node scout = firstNode;
Node replace =
newBag.firstNode;
int i = 0;
while ((i < numberOfEntries)
&& (scout != null)) {
int j = 0;
while ((j <
numberOfEntries) && (replace != null)) {
if (newBag.contains(scout.data)) {
break;
} else {
replace.data =
scout.data;
replace = replace.next;
j++;
}
}
if (scout !=
null) {
scout = scout.next;
i++;
}
}
// Remove filler values
Node newScout =
newBag.firstNode;
while (newBag.contains((T) "_"))
{
newBag.remove((T) "_");
}
this.firstNode =
newBag.firstNode;
this.numberOfEntries =
newBag.numberOfEntries;
System.out.print("Final >
");
System.out.println(toString());
return;
}
}
CODE
/**
* This method is used to determine if two bags are equal or not. Two bags are equal if both the bags
* contains the same elements equal number of times (in case of duplicates)
* @param aBag -> the other LinkedBag to be compared to
* @return a boolean representing if the bags are same
*/
public boolean equals(LinkedBag<T> aBag) {
boolean result = false; // result of comparison of bags
if (this.numberOfEntries == aBag.numberOfEntries) {
if (numberOfEntries == 0) {
return true;
}
int i = 0, j = 0;
Node scout = firstNode;
Node retrieve = aBag.firstNode;
while ((i < numberOfEntries) && (scout != null)) {
while ((j < aBag.numberOfEntries) && (retrieve != null)) {
if (scout.data.equals(retrieve.data)) {
int freq1 = this.getFrequencyOf(scout.data);
int freq2 = this.getFrequencyOf(retrieve.data);
if (freq1 == freq2) {
result = true;
} else {
result = false;
}
} // end if
retrieve = retrieve.next;
j++;
}
scout = scout.next;
i++;
} // end while
} else {
result = false;
}
return result;
} // end equals
//Duplicate all the items in a bag.
/**
* This util method is used to duplicate the first node in the bag and insert it until we have enough entries in the bag.
* @return
*/
public boolean duplicateAll() {
boolean success = true;
Node scout = firstNode;
int i = 0;
while (i < numberOfEntries) {
Node temp = new Node(scout.data, firstNode);
firstNode = temp;
scout = scout.next;
i++;
}
numberOfEntries *= 2;
return success;
} // end duplicateAll
/**
* This method is used to remove all duplicate elements from the bag
*/
public void removeDuplicates() {
System.out.print("Initial > ");
System.out.println(this.toString());
LinkedBag<T> newBag = new LinkedBag<T>();
if (numberOfEntries == 0) {
return; // Do nothing
}
// Create filler values to avoid null pointer
for (int k = 0; k < numberOfEntries; k++) {
newBag.add((T) "_");
}
Node scout = firstNode;
Node replace = newBag.firstNode;
int i = 0;
while ((i < numberOfEntries) && (scout != null)) {
int j = 0;
while ((j < numberOfEntries) && (replace != null)) {
if (newBag.contains(scout.data)) {
break;
} else {
replace.data = scout.data;
replace = replace.next;
j++;
}
}
if (scout != null) {
scout = scout.next;
i++;
}
}
// Remove filler values
Node newScout = newBag.firstNode;
while (newBag.contains((T) "_")) {
newBag.remove((T) "_");
}
this.firstNode = newBag.firstNode;
this.numberOfEntries = newBag.numberOfEntries;
System.out.print("Final > ");
System.out.println(toString());
return;
}