380 likes | 688 Vues
CSS342: Linked Lists. Professor: Munehiro Fukuda. Topics. Basic concepts of linked lists Implementations Special implementations Applications. Basic Concepts. 45. 76. 84. Linked List. header. 20. 51. NULL. X. template<class Object> struct LListNode { Object item;
E N D
CSS342: Linked Lists Professor: Munehiro Fukuda CSS342: Linked Lists
Topics • Basic concepts of linked lists • Implementations • Special implementations • Applications CSS342: Linked Lists
Basic Concepts 45 76 84 Linked List header 20 51 NULL X template<class Object> struct LListNode { Object item; LListNode *next; }; template<class Object> 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<Object> *header; LListNode<Object> *findByIndex( int index ) const; LListNode<Object> *findPrevious( const Object &x ) const; }; Removed item 60 Inserted item X == 45 CSS342: Linked Lists
Basic Concepts STL Class list CSS342: Linked Lists
Basic Concepts STL Class listExample #include <list> #include <iostream> #include <string> using namespace std; int main( ) { list<string> facultyList; list<string>::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 CSS342: Linked Lists
Implementation 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[2] list[3] list[4] list[n] list[n-1] CSS342: Linked Lists
Implementation 84 76 84 76 45 45 20 51 Merits of Linked List • Each item points to its successor • Arbitrary list size • No shift operations • Pointer operations required NULL 20 51 NULL Deleted item 60 Inserted item CSS342: Linked Lists
Implementation item item item item next next next next Pointer-Based Linked Listprivate data items item • List element struct LListNode { Object item; Node *next; } • Header pointer LListNode *header; If header == NULL, list is empty. Object* Object Object LListNode* LListNode header NULL head NULL CSS342: Linked Lists
Implementation NULL current cur current 76 76 51 60 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? header 51 NULL 60 newValue index header NULL Current actually cannot point to header! CSS342: Linked Lists
Implementation item item item next next next X new Value next insert( )Node Definition Revisited dummy header 0th element header NULL • 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; dummy header 0th element 1st node header NULL current newptr CSS342: Linked Lists
Implementation NULL 45 76 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; x dummy header 20 NULL current Node to be deleted CSS342: Linked Lists
Implementation header 51 60 p p p int find( const Object &x )Finding a Position That Includes X for (LListNode *p = header->next; p != NULL && p->element != x; p = p->next ); X dummy NULL Iteration 1: Exited from here Iteration 2: Iteration 3: CSS342: Linked Lists
Implementation header 60 51 p p p LListNode<Object> *findByIndex( int index )Finding a Position by an Index template<class Object> LListNode<Object> *LList<Object>::findByIndex( int index ) const { if ( index < 0 ) return NULL; LListNode<Object> *p = header; for ( int i = 0; p != NULL; p = p->next, i++ ) if ( i == index ) break; return p; } index = 2 dummy NULL index 0: Note: index 0 is a dummy index 1: index 2: Exited from here CSS342: Linked Lists
Implementation header 55 51 60 p p p p LListNode<Object> *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); X dummy NULL Iteration 1: Iteration 2: Iteration 3: Exitted from here Iteration 4: CSS342: Linked Lists
Implementation head 51 45 51 51 20 45 ~LList( )Destructor template<class Object> LList<Object>::~LList( ) { clear( ); delete header; } template<class Object> void LList<Object>::clear( ) { while ( !isEmpty( ) ) { LListNode<Object> *node = findByIndex( 1 ); remove( node->item ); } } • Deallocate all pointer variables NULL findByIndex(1) head NULL findByIndex(1) head NULL findByIndex(1) CSS342: Linked Lists
Implementation NULL copy of header header header 20 45 51 76 20 45 76 20 45 51 76 51 NULL copy of header NULL LList( const LList &rhs )Copy Constructor • Implicit calledupon passing, returning, and assigning an object. • Shallow copy: C++ and Java’s operator= copies an object’s data members. • Deep copy: Java’s clone( ) copies all traversable pointer variables. CSS342: Linked Lists
Implementation dummy rhs NULL copy copy copy dummy this copy of header NULL index 0 header 51 76 51 45 45 76 LList( const LList &rhs )Copy Constructor template<class Object> const LList<Object> &LList<Object>::operator=( const LList &rhs ) { if ( this != &rhs ) {// why do we have to check ? clear( );// why do we have to call clear( )? int index; LListNode<Object> *rnode; for ( index = 0, rnode = rhs.header->next; rnode != NULL; rnode = rnode->next, ++index ) insert( rnode->item, index );// insert rnode’s item after index } return *this; } template<class Object> LList<Object>::LList( const LList &rhs ) { header = new LListNode<Object>; header->next = NULL; *this = rhs; } CSS342: Linked Lists
Implementation Entire Implementation #ifndef _LLIST_CPP_ #define _LLIST_CPP_ #include <iostream> using namespace std; template<class Object> LList<Object>::LList( ) { header = new LListNode<Object>; header->next = NULL; } template<class Object> LList<Object>::LList( const LList &rhs ) { header = new LListNode<Object>; header->next = NULL; *this = rhs; } template<class Object> LList<Object>::~LList( ) { clear( ); delete header; } template<class Object> bool LList<Object>::isEmpty( ) const { return header->next == NULL; } template<class Object> void LList<Object>::clear( ) { while ( !isEmpty( ) ) { LListNode<Object> *node = findByIndex( 1 ); remove( node->item ); } } template<class Object> void LList<Object>::insert( const Object &x, int index ){ LListNode<Object> *current = findByIndex( index ); LListNode<Object> *newPtr = new LListNode<Object>; newPtr->item = x; newPtr->next = current->next; current->next = newPtr; } template<class Object> int LList<Object>::find( const Object &x ) const { LListNode<Object> *p = header->header; int i = 0; for ( ; p != NULL && p->item != x; p = p->next, ++i ); return ( p == NULL ) ? -1 : i; } 2/4 1/4 CSS342: Linked Lists
Implementation Entire Implementation (Cont’d) template<class Object> void LList<Object>::remove( const Object &x ) { LListNode<Object> *current = findPrevious( x ); if ( current == NULL ) return; LListNode<Object> *deletedNode = current->next; current->next = current->next->next; delete deletedNode; } template<class Object> const LList<Object> &LList<Object>::operator=( const LList &rhs ) { if ( this != &rhs ) { clear( ); int index; LListNode<Object> *rnode; for ( index = 0, rnode = rhs.header->next; rnode != NULL; rnode = rnode->next, ++index ) insert( rnode->item, index ); } return *this; } 3/4 CSS342: Linked Lists
Implementation Entire Implementation (Cont’d) template<class Object> template<class Object> LListNode<Object> *LList<Object>::findByIndex( int index ) const { if ( index < 0 ) return NULL; LListNode<Object> *p = header; for ( int i = 0; p != NULL; p = p->next, i++ ) if ( i == index ) break; return p; } template<class Object> LListNode<Object> *LList<Object>::findPrevious( const Object &x ) const{ LListNode<Object> *p; for ( p = header; p->next != NULL && p->next->item != x; p = p->next ); return p; } #endif 4/4 CSS342: Linked Lists
Implementation head 45 51 Saving a Linked List in a File ofstream outFile(filename); for (LListNode *cur = head->next; cur != NULL; cur = cur->next ) { outFile << cur->item << endl; outFile << cur->next << endl; // Must it be saved? } outFile.close( ); Cur->next, memory address has no meaning when reloaded dummy 45 51 EOF NULL Save Linked list File CSS342: Linked Lists
Implementation header 20 3 4 51 NULL 2 1 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 } dummy 20 45 51 EOF 45 NULL Restore Linked list File tail How about the first node? CSS342: Linked Lists
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 Implementation We would like to mitigate this O(N) access time Array-Based v.s. Pointer-Based Implementation CSS342: Linked Lists
Special Implementation NULL 51 head list 45 51 45 Circular Linked Lists dummy • 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 1st dummy node LListNode *cur = first->next; while ( cur != first ) { display(cur->item); cur = cur->next; }; dummy CSS342: Linked Lists
Special Implementation 45 73 51 head head 45 51 Doubly Linked Lists Single Linked Lists dummy 73 NULL To be deleted Precedent must be kept track of. Doubly Linked Lists dummy NULL NULL cur->prev->next = cur->next But pointer operations become more complicated. CSS342: Linked Lists
Special Implementation 45 73 51 20 Circular doubly linked list with a dummy head node head dummy 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 CSS342: Linked Lists
Special Implementation 45 73 20 Inserting a Nodebefore the current pointer (d) (a) (b) (c) cur newPtr->next = cur; // (a) newPrt->prev = cur->prev; // (b) cur->prev = newPtr; // (c) newPtr->prev->next = newPtr; // (d) CSS342: Linked Lists
Special Implementation 45 73 51 51 head Move-To-Front List • int find( const Object &x ) { • Move the item found to the top. } dummy NULL NULL To the front CSS342: Linked Lists
Special Implementation 45 73 51 51 head Transpose List • int find( const Object &x ) { • Swap the item with its previous item. } dummy NULL NULL swap CSS342: Linked Lists
Special Implementation 31 31 31 Skip Listhttp://en.wikipedia.org/wiki/Skip_list(designed in 1990) S5 – ∞ + ∞ Search for 39 S4 – ∞ 17 + ∞ S3 – ∞ 17 25 55 + ∞ Delete 31 rand( ) % 2 = 1 S2 – ∞ 17 25 55 + ∞ rand( ) % 2 = 1 S1 – ∞ 12 17 25 55 + ∞ S0 – ∞ 12 17 20 25 39 50 55 + ∞ Insert 31 CSS342: Linked Lists
Applications directory entry name start block 0 test 217 217 618 339 EOF 217 339 618 339 618 #blocks -1 FAT Single Linked List Application • File Allocation Table (FAT) • 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 CSS342: Linked Lists
Applications Process ID 99 Process ID 100 Process ID 217 Process ID 105 Prog name “g++” Prog name “ps2pdf” Prog name “a.out” Prog name “emacs” CPU Registers CPU Registers CPU Registers CPU Registers Page Table Page Table Page Table Page Table File Descriptors File Descriptors File Descriptors File Descriptors Circular List Application head • Round-Robin Based Job Scheduling CSS342: Linked Lists
Applications HEADER NULL string int main( ) { int a = 3, b = 5; cout << a + b << endl; } NULL Doubly Linked List Application • Line-based text editors like Unix ed CSS342: Linked Lists
Applications PC PC B ? 4 v Load M M MTF List Application • Page Replacement • 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 Proc1’s page table 0 1 2 1 H 3 v 0 1 2 3 4 5 6 7 OS Load M i 5 v OS J i D M H Process 1’s logical memory Proc2’s page table 0 1 2 3 A J 6 v B A i 2 v D E 7 v Physical memory E Process 2’s logical memory CSS342: Linked Lists
Applications LRU Implementation with MTF List • Replace a least recently used page with a new page. 1. Swap in page B page B LRU page Swap out page M page A page E page J page H page D page B page M page A page E page J page H 2. Access page H 3. Swap in D again Move to Front page D page H page B page M page A page E page J Swap out CSS342: Linked Lists