Place node struct node int data node next many texts

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: edList { public: // the following are as before // the LinkedList(); // constructor ~LinkedList(); // destructor void AddToTail(const int&); void AddToTail(const // previous store function bool IsEmpty(); int RemoveFromHead(); // previous retrieve function Applied Programming Here is an alternative: Place the declaration of node outside the class. Place node struct node { int data; node* next; }; Many texts use this approach although the node Many struct is not encapsulated. struct Applied Programming Applied Programming void Insert(const int&); void Insert(const bool Locate(const int&, int&); &, int bool Delete(const int&, int&); &, int // all using comparison private: struct node; typedef node *nodePtr; struct node { int data; nodePtr next; }; nodePtr head; }; #endif Applied Programming void LinkedList::Insert(const int& newdata) void LinkedList::Insert(const newdata { nodePtr curr, prev, newnode; prev newnode curr = head; prev = 0; while (curr!=0 && ListDataCmp(newdata,curr->data) > 0) !=0 ListDataCmp(newdata,curr { prev = curr; curr curr = curr->next; curr } newnode = new node; newnode->data = newdata; >data newdata // needs copy constructor if (prev != 0) prev->next = newnode; >next newnode else head = newnode; head newnode newnode->next = curr; >next curr } Applied Programming Many of the processes carried out on lists (and in Many fact arrays) are sequential such as fact • displaying all contents in order W e will see later how we can create a separate We iterator class to perform the process. iterator For now we will add three functions to our class. • calculating statistics W e need to be able to iterate through the list from We iterate through start to finish. start These three functions require the addition of one These more piece of private data in the class. more A current location – call it current. current call current How can we incorporate this process? Applied Programming This variable needs to be set to the head of the This list at the start of the iteration. So list void LinkedList::SetIt...
View Full Document

This document was uploaded on 04/07/2014.

Ask a homework question - tutors are online