Presentation is loading. Please wait.

Presentation is loading. Please wait.

Professor: Munehiro Fukuda

Similar presentations


Presentation on theme: "Professor: Munehiro Fukuda"— Presentation transcript:

1 Professor: Munehiro Fukuda
CSS342: Linked Lists Professor: Munehiro Fukuda CSS342: Linked Lists

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

3 Linked List X 20 45 51 76 84 60 X == 45 NULL Removed item
Basic Concepts header 20 45 51 76 84 NULL X template<class Object> struct LListNode { Object item; LListNode *next; }; 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

4 STL Class list Basic Concepts #include <list>
list( ); list(size_type num, const T& value); list( const list<T>& anotherList); Constructors. See the text page 221 for more details. Example: list<int> list1; list<string> 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<string>::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<string>::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 CSS342: Linked Lists

5 STL Class list Example #include <list> #include <iostream>
Basic Concepts #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

6 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

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

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

9 Current actually cannot point to header!
insert( &Object newValue, int index) Inserting a New Node (after an Index) Implementation 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? NULL header 51 76 NULL current cur 60 newValue index header 51 76 NULL 60 current Current actually cannot point to header! CSS342: Linked Lists

10 insert( ) Node Definition Revisited
Implementation dummy header 0th element 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 item next NULL dummy header 0th element 1st node X header item next item next NULL current new Value next newptr CSS342: Linked Lists

11 remove( const Object &x ) Removing a Given Node
Implementation 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 NULL dummy header 20 45 76 NULL current Node to be deleted CSS342: Linked Lists

12 int find( const Object &x ) Finding a Position That Includes X
Implementation 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 header 51 60 NULL p Iteration 1: p Exited from here Iteration 2: p Iteration 3: CSS342: Linked Lists

13 Implementation 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 header 51 60 NULL p index 0: Note: index 0 is a dummy p index 1: p index 2: Exited from here CSS342: Linked Lists

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

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

16 LList( const LList &rhs ) Copy Constructor
Implementation Implicit called upon 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. 20 45 51 76 NULL header copy of 20 45 51 76 NULL header copy of CSS342: Linked Lists

17 LList( const LList &rhs ) Copy Constructor
Implementation 45 51 76 NULL header copy of rhs this dummy index 0 copy 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; LList<Object>::LList( const LList &rhs ) { header = new LListNode<Object>; header->next = NULL; *this = rhs; CSS342: Linked Lists

18 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; } LList<Object>::LList( const LList &rhs ) { *this = rhs; LList<Object>::~LList( ) { clear( ); delete header; 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 ); } 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; 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

19 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; } 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

20 Entire Implementation (Cont’d)
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; } 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 ); #endif 4/4 CSS342: Linked Lists

21 Saving a Linked List in a File
Implementation 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 head 45 51 45 51 EOF NULL Save Linked list File CSS342: Linked Lists

22 Restoring a Linked List from a File
Implementation 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 header 20 45 NULL 1 3 51 File Restore Linked list NULL 4 tail 2 How about the first node? CSS342: Linked Lists

23 Array-Based v.s. Pointer-Based Implementation
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 We would like to mitigate this O(N) access time CSS342: Linked Lists

24 Circular Linked Lists Special Implementation 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 45 51 NULL head dummy 45 51 list CSS342: Linked Lists

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

26 Circular doubly linked list with a dummy head node
Special Implementation Circular doubly linked list with a dummy head node head 20 45 73 51 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

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

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

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

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

31 Single Linked List Application
Applications File Allocation Table (FAT) test 217 name start block directory entry 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 339 618 EOF 217 #blocks -1 FAT 217 339 618 CSS342: Linked Lists

32 Circular List Application
Applications Round-Robin Based Job Scheduling head 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 CSS342: Linked Lists

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

34 MTF List Application Page Replacement Applications 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 1 2 H PC 3 v 1 2 3 4 5 6 7 OS Load M 4 v i 5 v OS J i D M H Process 1’s logical memory B ? Load M M 1 2 3 Proc2’s page table A J PC 6 v B i A D 2 v E 7 v E Physical memory Process 2’s logical memory CSS342: Linked Lists

35 LRU Implementation with MTF List
Applications 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


Download ppt "Professor: Munehiro Fukuda"

Similar presentations


Ads by Google