Sessions 26 and 27 - Data Structures & Algorithm

Sessions 26 and 27 Data Structures& Algorithm

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: Linked Lists Linked lists vs. arrays Arrays can become full Allocating "extra" space in array wasteful, may never be used Linked lists can grow/shrink as needed Linked lists only become full when system runs out of memory Linked lists can be maintained in sorted order Insert element at proper position Existing elements do not need to be moved Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 10 Linked Lists Selected linked list operations Insert node at front Insert node at back Remove node from front Remove node from back In following illustrations List has firstPtr and lastPtr (a) is before, (b) is after Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 11 Insert at front a) firstPtr 7 11 newPtr 12 b) firstPtr 7 11 newPtr firstPtr = newPtr If list empty, then firstPtr = lastPtr = newPtr Sharif University of Technology 12 newPtr->nextPtr = firstPtr Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 12 Insert at back a) firstPtr 12 b) lastPtr 7 firstPtr 12 11 5 lastPtr 7 11 newPtr newPtr 5 lastPtr = newPtr If list empty, then firstPtr = lastPtr = newPtr Sharif University of Technology lastPtr->nextPtr = newPtr Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 13 Remove from front a) firstPtr 12 b) lastPtr 7 11 firstPtr 5 lastPtr tempPtr = firstPtr 12 7 11 5 firstPtr = firstPtr->next tempPtr delete tempPtr Sharif University of Technology If there are no more nodes, firstPtr = lastPtr = 0 Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 14 Remove from back a) firstPtr lastPtr 12 b) 7 11 currentPtr firstPtr 5 lastPtr "Walk" list until get next-to-last node, until currentPtr->nextPtr = lastPtr 12 7 11 5 tempPtr = lastPtr delete tempPtr Sharif University of Technology lastPtr = currentPtr tempPtr Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 15 Linked Lists Upcoming program has two class templates Create two class templates ListNode data (type depends on class template) nextPtr List Linked list of ListNode objects List manipulation functions insertAtFront insertAtBack removeFromFront removeFromBack Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 each node // Template ListNode class definition. #ifndef LISTNODE_H #define LISTNODE_H // forward declaration of class List template< class NODETYPE > class List; nodes template< class NODETYPE> class ListNode { friend class List< NODETYPE >; // make List a friend public: ListNode( const NODETYPE & ); // constructor NODETYPE getData() const; // return data in node private: NODETYPE data; // data ListNode< NODETYPE > *nextPtr; // next node in list "of list node type" }; // end class ListNode Template class ListNode. The type of member data depends on how the class template is used. Sharif University of Technology...
View Full Document

This note was uploaded on 09/01/2013 for the course CE CE230 taught by Professor Arash during the Fall '13 term at Sharif University of Technology.

Ask a homework question - tutors are online