Download presentation

Presentation is loading. Please wait.

Published byJanice Allinson Modified over 2 years ago

1
CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations of Stacks, Sets, etc.

2
CS 1032 Definition of Linked Lists A linked list is a sequence of items (objects) where every item is linked to the next. Graphically: data head_ptr tail_ptr

3
CS 1033 Definition Details Each item has a data part (one or more data members), and a link that points to the next item One natural way to implement the link is as a pointer; that is, the link is the address of the next item in the list It makes good sense to view each item as an object, that is, as an instance of a class. We call that class: Node The last item does not point to anything. We set its link member to NULL. This is denoted graphically by a self-loop

4
CS 1034 Examples of Linked Lists (A Waiting Line) A waiting line of customers: John, Mary, Dan, Sue (from the head to the tail of the line) A linked list of strings can represent this line: JohnMaryDanSue head_ptr tail_ptr

5
CS 1035 Examples of Linked Lists (A Stack of Numbers) A stack of numbers (from top to bottom): 10, 8, 6, 8, 2 A linked list of ints can represent this stack: head_ptr tail_ptr 8

6
CS 1036 Examples of Linked Lists ( A Set of Non-redundant Elements ) A set of characters: a, b, d, f, c A linked list of chars can represent this set: abdc head_ptr tail_ptr f

7
CS 1037 Examples of Linked Lists ( A Sorted Set of Non-redundant Elements ) A set of characters: a, b, d, f, c The elements must be arranged in sorted order: a, b, c, d, f A linked list of chars can represent this set: abcf head_ptr tail_ptr d

8
CS 1038 Examples of Linked Lists ( A Polynomial ) A polynomial of degree n is the function P n (x)=a 0 +a 1 x+a 2 x 2 +…+a n x n. The a i s are called the coefficients of the polynomial The polynomial can be represented by a linked list ( 2 data members and a link per item ): a 0,0a 1,1a 2,2a n,n head_ptr tail_ptr

9
CS 1039 Operations on Linked Lists Insert a new item –At the head of the list, or –At the tail of the list, or –Inside the list, in some designated position Search for an item in the list –The item can be specified by position, or by some value Delete an item from the list –Search for and locate the item, then remove the item, and finally adjust the surrounding pointers size( ); isEmpty( )

10
CS Insert– At the Head Insert a new data A. Call new: newPtr List before insertion: After insertion to head: data head_ptrtail_ptr Adata head_ptr tail_ptr A The link value in the new item = old head_ptr The new value of head_ptr = newPtr

11
CS Insert – at the Tail Insert a new data A. Call new: newPtr List before insertion After insertion to tail: data head_ptrtail_ptr Adata head_ptr tail_ptr A The link value in the new item = NULL The link value of the old last item = newPtr

12
CS Insert – inside the List Insert a new data A. Call new: newPtr List before insertion: After insertion in 3 rd position: data head_ptrtail_ptr data A head_ptr tail_ptr data The link-value in the new item = link-value of 2 nd item The new link-value of 2 nd item = newPtr

13
CS Delete – the Head Item List before deletion: List after deletion of the head item: data head_ptr tail_ptr data head_ptr tail_ptr data The new value of head_ptr = link-value of the old head item The old head item is deleted and its memory returned data

14
CS Delete – the Tail Item List before deletion: List after deletion of the tail item: data head_ptr tail_ptr data head_ptr tail_ptr New value of tail_ptr = link-value of the 3 rd from last item New link-value of new last item = NULL. data

15
CS Delete – an inside Item List before deletion: List after deletion of the 2 nd item: data head_ptr tail_ptr data head_ptr tail_ptr New link-value of the item located before the deleted one = the link-value of the deleted item data

16
CS size() and isEmpty() We need to scan the items in the list from the head_ptr to the last item marked by its link-value being NULL Count the number of items in the scan, and return the count. This is the size(). Alternatively, keep a counter of the number of item, which gets updated after each insert/delete. The function size( ) returns that counter If head_ptr is NULL, isEmpty() returns true; else, it returns false.

17
CS Searching for an Item Suppose you want to find the item whose data value is A You have to search sequentially starting from the head item rightward until the first item whose data member is equal to A is found. At each item searched, a comparison between the data member and A is performed.

18
CS Time of the Operations Time to search() is O(L) where L is the relative location of the desired item in the List. In the worst case. The time is O(n). In the average case it is O(N/2)=O(n). Time for remove() is dominated by the time for search, and is thus O(n). Time for insert at head or at tail is O(1). Time for insert at other positions is dominated by search time, and thus O(n). Time for size() is O(1), and time for isEmpty() is O(1)

19
CS Implementation of an Item Each item is a collection of data and pointer fields, and should be able to support some basic operations such as changing its link value and returning its member data Therefore, a good implementation of an item is a class The class will be called Node

20
CS Class Node Design for Item The member variables of Node are: –The data field(s) –The link pointer, which will be called next The functions are: FunctionActionWhy Needed getNext( )returns the link.for navigation getData( )returns the datafor search setNext( Node *ptr) sets link to ptrfor insert/delete setData (type x) sets data to x.to modify data contents

21
CS Class Node Type class Node { private: int data; // different data type for other apps Node *next; // the link pointer to next item public: Node(int x=0;Node * ptr=NULL); // constructor int getData( ); Node *getNext( ); void setData(int x); void setNext(Node *ptr); };

22
CS Class Node Implementation Node::Node(int x, Node *p){ data=x; next=p;}; int Node::getData( ){return data;}; Node * Node::getNext( ){return next;}; void Node::setData(int x) {data=x;}; void Node::setNext(Node *ptr){next=ptr;};

23
CS Implementation of Linked List A linked list is a collection of Node objects, and must support a number of operations Therefore, it is sensible to implement a linked list as a class The class name for it is List

24
CS Class Design for List The member variables are: –Node *head_ptr; Node *tail_ptr; –int numOfItems; Member functions –Node * search(int x); Node * itemAt(int position); –void removeHead(); void removeTail(); void remove(int x); –void insertHead(int x); void insertTail(int x); void insert(Node *p, int x) // inserts item after the item // pointed to by p –int size( ); Node *getHead( ); Node getTail( ); –bool isEmpty( );

25
CS Class List Type class List { private: Node *head_ptr; Node *tail_ptr; int numOfItems; public: List( ); // constructor int size( ); Node *getHead( ); Node *getTail( ); bool isEmpty( ); Node *search(int x); Node *itemAt(int position); void removeHead(); void removeTail(); void remove(int x); // delete leftmost item having x void insertHead(int x); void insertTail(int x); void insert(Node *p, int x); };

26
CS Implementation of Class List List::List( ){head_ptr= NULL; tail_ptr=NULL; numOfItems=0;}; int List::size( ){return numOfItems;}; Node * List::getHead( ) {return head_ptr;}; Node * List::getTail( ) {return tail_ptr;}; bool List::isEmpty() {return (numOfItem==0);};

27
CS Implementation of search( ) Node *List::search(int x){ Node * currentPtr = getHead( ); while (currentPtr != NULL){ if (currentPtr->getData( ) == x) return currentPtr; else currentPtr = currentPtr->getNext(); } return NULL;// Now x is not, so return NULL };

28
CS Implementation of itemAt( ) Node *List::itemAt(int position){ if (position =numOfItems) return NULL; Node * currentPtr = getHead( ); for(int k=0;k != position; k++) currentPtr = currentPtr -> getNext( ); return currentPtr; };

29
CS Implementation of removeHead( ) void List::removeHead( ){ if (numOfItems == 0) return; Node * currentPtr = getHead( ); head_ptr=head_ptr->getNext( ); delete currentPtr; numOfItems--; };

30
CS Implementation of removeTail( ) void List::removeTail( ){ if (numOfItems == 0) return; if (head_ptr == tail_ptr){ head_ptr=NULL; tail_ptr= NULL; numOfItems=0; return; } Node * beforeLast = itemAt(numOfItems-2); beforeLast->setNext(NULL); // beforeLast becomes last delete tail_ptr; // deletes the last object tail_ptr=beforeLast; numOfItems--; };

31
CS Implementation of remove( ) void List::remove(int x){ if (numOfItems == 0) return; if (head_ptr==tail_ptr && head_ptr->getData()==x){ head_ptr=NULL; tail_ptr= NULL; numOfItems=0; return; } Node * beforePtr=head_ptr; // beforePtr trails currentPtr Node * currentPtr=head_ptr->getNext(); Node * tail = getTail(); while (currentPtr != tail) if (currentPtr->getData( ) == x){ // x is found. Do the bypass beforePtr->setNext(currentPtr->getNext()); delete currentPtr; numOfItems--; } else { // x is not found yet. Forward beforePtr & currentPtr. beforePtr = currentPtr; currentPtr = currentPtr->getNext(); } };

32
CS Implementation of insertHead( ) void List::insertHead(int x){ Node * newHead = new Node(x,head_ptr); head_ptr= newHead; if (tail_ptr == NULL) // only one item in list tail_ptr = head_ptr; numOfItems++; };

33
CS Implementation of insertTail( ) void List::insertTail(int x){ if (isEmpty()) insertHead(x); else{ Node * newTail = new Node(x); tail_ptr->setNext(newTail); tail_ptr = newTail; numOfItems++; } };

34
CS Implementation of insert( ) // inserts item x after the item pointed to by p void List::insert(Node *p, int x){ Node *currentPtr = head_ptr; while(currentPtr !=NULL && currentPtr != p) currentPtr = currentPtr->getNext(); if (currentPtr != NULL ) { // p is found Node *newNd=new Node(x,p->getNext()); p->setNext(newNd); numOfItems++; } };

35
CS For your Work in the Lab Make the necessary modifications to the List class implementations so that no two Nodes have the same data value. This is useful when using linked lists to implement sets. Make the necessary changes to the List class so that the Nodes are in increasing order of data values. In particular, replace all the insert methods, and replace them with insert(int x), which inserts x in the right position so that the List remains sorted.

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google