You are on page 1of 7

Description: My doubly-linked list implementation of a list of Node items involves th ree Node pointers:head, tail, and currItem.

The head pointer points to the first node, the tail pointer points to the last n ode, and the currItem pointer is used to iterate through the list. My linked list is not circular and does not contain dummy node s. My list nodes contain an ItemType value, the count of that value, a pointer to the next Node, and a pointer to the previous N ode. I insert values into the values if the Nodes already exist and create new Nodes for the values if the values dont exist. Pseudocode: Bag::Bag() sets the head, currItem, and tail pointer to NULL sets the number of items and unique items to 0 Bag::~Bag() sets currItem to the first item repeatedly: if there is only one item left delete the item and set the head and tail pointers to NU LL if there is more than one type of item in the list sets head to the next item sets the prev pointer of the next item to NULL delete the first item set currItem to the new head sets the number of items and unique items to 0

Bag(const Bag& old) copies the number of items and unique items from old into this bag iterate through the entire linked list create the first node to hold the first node of old head points to a newly allocated node copy the value and count of old into this bag sets the pointers to the correct items points currItem to the head create the consecutive nodes after head tail points to a newly allocated node copy the value and count of old into this bag sets the pointers to the correct items Bag& operator=(const Bag& rightSide) if this bag is the same as the referenced bag return this bag else copy the rightSide Bag into a temp bag swap the temp bag with this bag return this bag bool insert(const ItemType& value) if this bag is completely empty

head and tail will point to a newly allocated node input the value into the node and set the count to 1 set the number of items and unique items in the bag to 1 return true if there are items already in the bag iterate through the list if value is found in the bag increase the count of that item and increase the number of items in the bag return true if the value isnt already there create a new node input the value into the node and set the count to 1 arrange the head, tail, and currItem pointers in the cor rect order increase the number of items and the unique items in the bag return true int erase(const ItemType& value) iterate through the list if the value is found if there is only one instance of value delete the entire node if there is more than one instance of value decrease the count of the number of instances of that value return 1 if the value was not found return 0

int eraseAll(const ItemType& value) iterate through the list if value is found in the list set the head, tail, and currItem pointers to the correct items decrease the number of unique items keep track of the number of items erased decrease the number of total items by the amount erased delete the node that currItem points to return the number of items erased if nothing was erased return 0 bool contains(const ItemType& value) const iterate through the list using a pointer to Node if value is found return true if value is not found return false int count(const ItemType& value) const iterate through the list if value is found return the count of that value if value is not found return 0

void swap(Bag& other) create a temporary pointer to Node, tempNODE use tempNODE to exchange the head,tail, and currItem pointers of this ba g with those of other bag create a temporary integer, temp use temp to exchange the number of total items and unique items in this bag with those of other bag void start() if the list set if the list set set is empty bool hasEnded to true is not empty hasEnded to false currItem to head, the first item

set the integer, counter, to 1 void next() increase the counter set currItem to the next item in the list if counter is equal to the number of unique items in the bag set hasEnded to true

bool ended() const if hasEnded is true return true otherwise return false void emptyTheBag(Bag& a) iterate through the list erase all of the uniqueItems the number of times they occur void subtractInsert(Bag& b1, Bag& b2, Bag& result) create a temporary Bag, temp iterate through the array if occurences of value is less in b2 than in b1 insert the value n times into temp, if n is the differen ce between occurences of value in b1 and b2 if b1 and result refer to the same bag call emptyTheBag(b1) else if b2 and result refer to the same bag call emptyTheBag(b2) else if b1, b2, and result refer to different bags call emptyTheBag(result) assign temp to result

void iterateAndInsert(Bag& a, Bag& result) create temporary Bag, temp store Bag a into Bag temp iterate through the temp Bag

insert all the values, as many times as each value occurs, in Ba g a(Bag temp) into Bag result void combine(Bag& b1, Bag& b2, Bag& result) if b1 and result refer to the same bag add all items from b2 into result(b1) else if b2 and result refer to the same bag add all items from b1 into result(b2) otherwise, call emptyTheBag(result) add all items from both b1 and b2 into result void subtract(Bag& b1, Bag& b2, Bag& result) if b1, b2, and result all refer to the same bag call emptyTheBag(result) else call subtractInsert(b1,b2,result)

List of Test Cases: //These tests were performed on a bag of unsigned longs //default constructor Bag a; //for an empty bag assert(a.size()==0 &&a.uniqueSize()==0 &&a.erase(1)==0); assert(a.empty()); //insert and erase functions assert(a.insert(2123)); assert(a.insert(2123)); assert(a.insert(2123)); assert(a.insert(2123)); assert(a.insert(2123)); assert(a.insert(2123)); assert(a.insert(2123)); assert(a.insert(245)); //count, size and uniqueSize functions assert(a.count(2123)==7&&a.count(1)==0&&a.count(245)==1); assert(a.size()==8 && a.uniqueSize()==2); //erase and eraseAll functions Bag d; d.insert(1); d.insert(1); assert(d.eraseAll(2)==0); assert(d.erase(123)==0); assert(d.eraseAll(1)==2); //swap function Bag b; Bag c; assert(b.empty() && c.empty()); b.swap(c);

b.insert(1); c.insert(2); c.swap(b); //contains function assert(b.contains(2) && b.size()==1 && c.size()==1 && c.contains(1)); assert(c.insert(14)); b.insert(123); b.insert(345); b.swap(c); assert(b.size()==2 && b.uniqueSize()==2 && b.contains(14) && b.contains(1)); assert(c.size()==3 && c.uniqueSize()==3 && c.contains(345) && c.contains(123) && c.count(2)==1); //next, currentCount, and currentValue function calls Bag e; e.insert(1); e.insert(1); e.insert(1); e.insert(1); e.insert(2); e.insert(3); e.insert(4); e.insert(5); e.start(); assert(e.currentValue()==1 && e.currentCount()==4); e.next(); assert(e.currentValue()==2); e.next(); assert(e.currentValue()==3); e.next(); assert(!e.ended()); assert(e.currentValue()==4 && e.currentCount()==1); e.next(); assert(e.currentValue()==5 && e.currentCount()==1); assert(e.ended()); //start and ended function calls in an empty bag Bag f; f.start(); assert(f.empty()); assert(f.ended());

//copy constructor Bag h; h.insert(1); h.insert(1); h.insert(2); h.insert(2); h.insert(2); h.insert(3); h.insert(4); h.insert(5); h.insert(6); h.insert(7); assert(h.size()==10 && h.uniqueSize()==7); Bag i(h); assert(i.contains(6) && i.contains(2) && i.contains(1) && i.contains(7) && i.con

tains(3) &&i.count(4)==1 && i.count(5)==1); assert(i.count(2)==3); assert(i.size()==10 && i.uniqueSize()==7); assert(i.eraseAll(2)==3); //assignment operator Bag g; g.insert(1); g.insert(1); g.insert(1); assert(g.count(1)==3&& g.count(0)==0); i=g; assert(i.count(1)==3 && i.size()==3 && g.count(1)==3 && g.size()==3); //combine function Bag ab; ab.insert(2); ab.insert(2); ab.insert(2); ab.insert(8); ab.insert(3); b.insert(3); ab.insert(9); ab.insert(5); Bag ac; ac.insert(6); ac.insert(3); ac.insert(8); ac.insert(8); ac.insert(5); ac.insert(10); Bag ad; combine(ab,ac,ad); assert(ad.count(2)==3 && ad.count(3)==3 &&ad.count(5)==2 &&ad.count(6)==1 &&ad.c ount(8)==3 && ad.count(9)==1 &&ad.count(10)==1); //combine if b1 and e same bag Bag ae; Bag af; Bag ag; ae.insert(1); ae.insert(2); ae.insert(3); af.insert(10); af.insert(20); af.insert(30); ag.insert(100); combine(ae,af,ae); assert(af.size()==3 & ae.contains(10)); combine(ag,af,af); assert(ag.size()==1 combine(af,af,ag); assert(ag.size()==8 result refer to the same bag or if b2 and result refer to th

&&af.uniqueSize()==3 && ae.uniqueSize()==6 && ae.size()==6 & && af.size()==4 && af.contains(100)); && ag.uniqueSize()==4 && af.size()==4);

//tests if combines works with b1, b2, and result all referring to the same bag combine(ag,ag,ag); assert(ag.size()==16 && ag.count(100)==4 && ag.count(10)==4 && ag.count(20)==4 &

& ag.count(30)==4);

//subtract function Bag ba; Bag bc; Bag bb; ba.insert(2); ba.insert(2); ba.insert(2); ba.insert(8); ba.insert(3); ba.insert(3); ba.insert(9); ba.insert(5); bc.insert(6); bc.insert(3); bc.insert(8); bc.insert(8); bc.insert(5); bc.insert(10); subtract(ba,bc,bb); assert(ba.size()==8 && bc.size()==6 && bb.size()==5 && bb.count(2)==3 && bb.coun t(3)==1 && bb.count(9)==1); //subtract function if b1 or b2 refers to the same bag as result Bag ca; Bag cb; Bag cc; ca.insert(1); ca.insert(1); ca.insert(2); cb.insert(1); cb.insert(10); cc.insert(678); cc.insert(890); cc.insert(10); cc.insert(10); subtract(ca,cb,ca); assert(ca.size()==2 && ca.uniqueSize()==2 && ca.count(1)==1 && ca.count(2)==1); subtract(cc,cb,cb); assert(cb.size()==3 && cb.uniqueSize()==3 && !cb.contains(1) && cb.count(678)==1 ); assert(cc.size()==4 &&cc.uniqueSize()==3 &&cc.contains(678)); subtract(cc,cc,cc); assert(cc.size()==0 && cc.uniqueSize()==0); assert(cc.empty());