Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSS342: Linked Lists1 Professor: Munehiro Fukuda.

Similar presentations


Presentation on theme: "CSS342: Linked Lists1 Professor: Munehiro Fukuda."— Presentation transcript:

1 CSS342: Linked Lists1 Professor: Munehiro Fukuda

2 CSS342: Linked Lists2 Topics Basic concepts of linked lists Implementations Special implementations Applications

3 CSS342: Linked Lists3 Linked List template struct LListNode { Object item; LListNode *next; }; template class LList { public: LList( ); LList( const LList &rhs ); ~LList( ); bool isEmpty( ) const; void clear( ); void insert( const Object &x, int index ); // insert a new item right after the index int find( const Object &x ) const; void remove( const Object &x ); const LList &operator=( const LList &rhs ); private: LListNode *header; LListNode *findByIndex( int index ) const; LListNode *findPrevious( const Object &x ) const; }; 20 45 51 7684 60 Removed item Inserted item NULL X X == 45 header Basic Concepts

4 CSS342: Linked Lists4 STL Class list #include list( ); list(size_type num, const T& value); list( const list & anotherList); Constructors. See the text page 221 for more details. Example: list list1; list list2(10, straightB); bool empty( ) const; Returns true if the list has no elements. Example: while (list1.empty( ) == false) { … } iterator begin( ); Returns an iterator that references the first element.If there is nothing, it is the same as the value returned by end( ) Example: list ::iterator i = list2.begin( ); if ( i != list2.end( ) ) cout << *I << endl; iterator end( ); Return the end marker. Example: see begin( ). size_type size() const; Returns the number of elements. Size_type is an integral type. You can compare it with an integer. Example: while (list1.size( ) > 0) { … } iterator insert(iterator position, const T& value); Inserts an item value immediately before the element specified by the iterator position. Example: list ::iterator i = list2.insert (10, abc ); void remove (const T& value); Removes all items with value from the list. Example: list2.remove(straightB); iterator erase( iterator position ); Removes the item pointed to by iterator position. Example: list1.erase( 2 ); void push_back(const T& x); void push_front(const T& x); T& front(); void clear(); void pop_front(); void pop_back(); void reverse(); See your own C++ textbook or http://www.cppreference.com/ Basic Concepts

5 CSS342: Linked Lists5 STL Class list Example #include using namespace std; int main( ) { list facultyList; list ::iterator i = facultyList.begin( ); i = facultyList.insert( i, berger ); i = facultyList.insert( i, cioch ); i = facultyList.insert( i, fukuda ); cout << #faculty members: << facultyList.size( ) << endl; for (i = facultyList.begin( ); i != facultyList.end( ); i++ ) cout <<*i << endl; } Results (= inputs are reversed): #faculty members: 3 fukuda cioch berger Basic Concepts

6 CSS342: Linked Lists6 Array-Based List Implementation Easy implementation Fixed list size Necessity to shift data down to an extended space upon an insertion and to shift data up upon a deletion –What is big-O for those operations? Find Insert Delete list[0] list[1] list[3] list[2] list[4] list[n] list[n-1] Implementation

7 CSS342: Linked Lists7 Merits of Linked List Each item points to its successor Arbitrary list size No shift operations Pointer operations required 2045517684 20 45 51 7684 60 Deleted item Inserted item NULL Implementation

8 CSS342: Linked Lists8 Pointer-Based Linked List private data items List element struct LListNode { Object item; Node *next; } Header pointer LListNode *header; If header == NULL, list is empty. itemnextitemnext item ObjectLListNode* Object*Object LListNode header head itemnextitemnext NULL Implementation

9 CSS342: Linked Lists9 insert( &Object newValue, int index) Inserting a New Node (after an Index) Creating a new node –LListNode *newptr = new Node; –newPtr->item = newValue; Inserting a node between nodes –newPtr->next = current->next; –current->next = newPtr; Inserting a node at the top –newPtr->next = head; –header = newptr; Should we really distinguish these two cases? 51 76 60 NULL header cur current 5176 60 NULL header current NULL Current actually cannot point to header! Implementation index newValue

10 CSS342: Linked Lists10 insert( ) Node Definition Revisited List element struct LListNode { Object item; Node *next; } Header pointer and node // adding a dummy header node LListNode *header = new Node; header->next = null; If head->next == NULL, list is empty. Node Insertion –LListNode *newptr = new Node; –newPtr->item = newValue; –newPtr->next = current->next; –current->next = newPtr; header itemnextitemnext NULL itemnext dummy header 0 th element 1 st node current dummy header 0 th element new Value next newptr X Implementation

11 CSS342: Linked Lists11 remove( const Object &x ) Removing a Given Node Stop the current pointer at the node previous to a deleted node (with findPrevious( )) Memorize the node to remove LListNode *deletedNode = current->next; Remove this node current->next = current->next->next; Deallocating the node delete deletedNode; 20 45 NULL 76 header current NULL dummy Implementation x Node to be deleted

12 CSS342: Linked Lists12 int find( const Object &x ) Finding a Position That Includes X for (LListNode *p = header->next; p != NULL && p->element != x; p = p->next ); header NULL 6051 pp Iteration 1: Iteration 2: Iteration 3: dummy p X Exited from here Implementation

13 CSS342: Linked Lists13 LListNode *findByIndex( int index ) Finding a Position by an Index header NULL 6051 pp index 0: index 1: index 2: dummy p index = 2 Exited from here template LListNode *LList ::findByIndex( int index ) const { if ( index < 0 ) return NULL; LListNode *p = header; for ( int i = 0; p != NULL; p = p->next, i++ ) if ( i == index ) break; return p; } Implementation Note: index 0 is a dummy

14 CSS342: Linked Lists14 LListNode *findPrevious( Object &x ) Finding the Node Previous to the One to Be Deleted for (LListNode *p = header; p->next != NULL && p->next->item != x; p = p->next); header 5551 pp Iteration 1: Iteration 2: Iteration 3: dummy p Exitted from here NULL 60 X Iteration 4: p Implementation

15 CSS342: Linked Lists15 ~LList( ) Destructor Deallocate all pointer variables 204551 NULL head 4551 NULL head 51 NULL head findByIndex(1) template LList ::~LList( ) { clear( ); delete header; } template void LList ::clear( ) { while ( !isEmpty( ) ) { LListNode *node = findByIndex( 1 ); remove( node->item ); } Implementation

16 CSS342: Linked Lists16 LList( const LList &rhs ) Copy Constructor Implicit called upon passing, returning, and assigning an object. Shallow copy: C++ and Javas operator= copies an objects data members. Deep copy: Javas clone( ) copies all traversable pointer variables. 20455176 NULL header copy of header 20455176 NULL header copy of header 20455176 NULL Implementation

17 CSS342: Linked Lists17 template const LList &LList ::operator=( const LList &rhs ) { if ( this != &rhs ) {// why do we have to check ? clear( );// why do we have to call clear( )? int index; LListNode *rnode; for ( index = 0, rnode = rhs.header->next; rnode != NULL; rnode = rnode->next, ++index ) insert( rnode->item, index ); // insert rnodes item after index } return *this; } template LList ::LList( const LList &rhs ) { header = new LListNode ; header->next = NULL; *this = rhs; } 455176 NULL header copy of header 455176 NULL rhs this dummy index 0 copy Implementation LList( const LList &rhs ) Copy Constructor

18 CSS342: Linked Lists18 Entire Implementation #ifndef _LLIST_CPP_ #define _LLIST_CPP_ #include using namespace std; template LList ::LList( ) { header = new LListNode ; header->next = NULL; } template LList ::LList( const LList &rhs ) { header = new LListNode ; header->next = NULL; *this = rhs; } template LList ::~LList( ) { clear( ); delete header; } template bool LList ::isEmpty( ) const { return header->next == NULL; } template void LList ::clear( ) { while ( !isEmpty( ) ) { LListNode *node = findByIndex( 1 ); remove( node->item ); } template void LList ::insert( const Object &x, int index ){ LListNode *current = findByIndex( index ); LListNode *newPtr = new LListNode ; newPtr->item = x; newPtr->next = current->next; current->next = newPtr; } template int LList ::find( const Object &x ) const { LListNode *p = header->header; int i = 0; for ( ; p != NULL && p->item != x; p = p->next, ++i ); return ( p == NULL ) ? -1 : i; } 1/4 2/4 Implementation

19 CSS342: Linked Lists19 Entire Implementation (Contd) template void LList ::remove( const Object &x ) { LListNode *current = findPrevious( x ); if ( current == NULL ) return; LListNode *deletedNode = current->next; current->next = current->next->next; delete deletedNode; } template const LList &LList ::operator=( const LList &rhs ) { if ( this != &rhs ) { clear( ); int index; LListNode *rnode; for ( index = 0, rnode = rhs.header->next; rnode != NULL; rnode = rnode->next, ++index ) insert( rnode->item, index ); } return *this; } 3/4 Implementation

20 CSS342: Linked Lists20 Entire Implementation (Contd) template LListNode *LList ::findByIndex( int index ) const { if ( index < 0 ) return NULL; LListNode *p = header; for ( int i = 0; p != NULL; p = p->next, i++ ) if ( i == index ) break; return p; } template LListNode *LList ::findPrevious( const Object &x ) const{ LListNode *p; for ( p = header; p->next != NULL && p->next->item != x; p = p->next ); return p; } #endif 4/4 Implementation

21 CSS342: Linked Lists21 ofstream outFile(filename); for (LListNode *cur = head->next; cur != NULL; cur = cur- >next ) { outFile item << endl; outFile next << endl;// Must it be saved? } outFile.close( ); Saving a Linked List in a File 4551 NULL head 4551 EOF Save FileLinked list Cur->next, memory address has no meaning when reloaded dummy Implementation

22 CSS342: Linked Lists22 Restoring a Linked List from a File LListNode *tail = header; while ( inFile >> nextItem ) {tail->next = new Node; // 1. Allocate a new node next to the tail tail = tail->next; // 2. Have the tail point to the new node tail->item = nextItem; // 3. Put the new item in the node tail->next = NULL; // 4. Set the pointer in the new node to NULL } 20 45 NULL header 204551 EOF tail 1 2 NULL 4 Restore File Linked list How about the first node? 3 51 dummy Implementation

23 CSS342: Linked Lists23 Array-based: Advantages: –Easy to use –A good choice for a small list –O(1) access time Disadvantages: –space and time wasting in dynamic array Pointer-Based: Advantages: –Arbitrary size –No shift required Disadvantages: –Necessity to allocate next –O(N) access time Array-Based v.s. Pointer-Based Implementation Implementation We would like to mitigate this O(N) access time

24 CSS342: Linked Lists24 Circular Linked Lists Linear linked lists: grocery, inventory, and customer lists Circular linked lists: login users list Traverse in circular linked lists: LListNode *first = list; // list points the 1 st dummy node LListNode *cur = first->next; while ( cur != first ) { display(cur->item); cur = cur->next; }; 4551 NULL head 4551 list Special Implementation dummy

25 CSS342: Linked Lists25 Doubly Linked Lists NULL 457351 NULL head 45 73 51 NULL head Single Linked Lists To be deleted Precedent must be kept track of. Doubly Linked Lists cur->prev->next = cur->next But pointer operations become more complicated. dummy Special Implementation

26 CSS342: Linked Lists26 Circular doubly linked list with a dummy head node 45735120 head Traversing a node: cur = head->next; while (cur != head && newItem != cur->item) cur = cur->next; Deleting a node: cur->prev->next = cur->next; cur->next->prev = cur->prev dummy Special Implementation

27 CSS342: Linked Lists27 Inserting a Node before the current pointer 457320 newPtr->next = cur;// (a) newPrt->prev = cur->prev;// (b) cur->prev = newPtr;// (c) newPtr->prev->next = newPtr;// (d) cur (a) (b)(c) (d) Special Implementation

28 CSS342: Linked Lists28 Move-To-Front List int find( const Object &x ) { –Move the item found to the top. } Special Implementation NULL 457351 NULL head dummy 51 To the front

29 CSS342: Linked Lists29 Transpose List int find( const Object &x ) { –Swap the item with its previous item. } Special Implementation NULL 457351 NULL head dummy 51 swap

30 CSS342: Linked Lists30 Skip List http://en.wikipedia.org/wiki/Skip_list (designed in 1990) http://en.wikipedia.org/wiki/Skip_list Special Implementation – – – – – – 12 17 20 25 31 3950 55 + + + + + + S5 S4 S3 S2 S1 S0 Search for 39 Insert 31 rand( ) % 2 = 1 Delete 31

31 CSS342: Linked Lists31 Single Linked List Application FAT has an entry for each disk block. FAT entries rather than blocks themselves are linked. Example: –MS-DOS and OS/2 Merit: –Save disk block space –Faster random accesses Demerit: –A significant number of disk head seeks test217 name start block directory entry 339 618 EOF 0 217 339 618 #blocks -1 FAT 217 339 618 Applications File Allocation Table (FAT)

32 CSS342: Linked Lists32 Circular List Application Round-Robin Based Job Scheduling Applications CPU Registers Page Table File Descriptors Prog name emacs Process ID 100 CPU Registers Page Table File Descriptors Prog name g++ Process ID 105 CPU Registers Page Table File Descriptors Prog name a.out Process ID 99 CPU Registers Page Table File Descriptors Prog name ps2pdf Process ID 217 head

33 CSS342: Linked Lists33 Doubly Linked List Application Line-based text editors like Unix ed Applications int main( ) { int a = 3, b = 5; cout << a + b << endl; } NULL string NULL HEADER

34 CSS342: Linked Lists34 MTF List Application No more free frame Find a victim page –Paging out M cause another page fault –Paging out H may be okay. –Algorithm to minimize page faults H Load M J M A B D E OS D H J A E 3v i 5v i 6v i 2v 7v Process 1 s logical memory Process 2 s logical memory Proc1 s page table Proc2 s page table 01210121 01230123 0123456701234567 Physical memory PC Load M M 4v B ? Applications Page Replacement

35 CSS342: Linked Lists35 LRU Implementation with MTF List Replace a least recently used page with a new page. Applications page D page H page J page E page B page M page A Swap out page D page H page J page E page M page A page B 2. Access page H 1. Swap in page B page H page J page E page M page A page B 3. Swap in D again Swap out Move to Front LRU page


Download ppt "CSS342: Linked Lists1 Professor: Munehiro Fukuda."

Similar presentations


Ads by Google