linklist

Click here to load reader

Upload: sheetal-waghmare

Post on 26-May-2015

1.357 views

Category:

Education


2 download

DESCRIPTION

SINGLY LINKLIST (INSERTION,DELETION,SORT,SEARCH) STACK(PUSH,POP) QUEUE(ADD,REMOVE) CIRCULAR LINKLIST DOUBLE ENDED QUEUE CIRCULAR QUEUE PRIORITY QUEUE DOUBLY LINKLIST(INSERTION,DELETION,SORT,SEARCH)

TRANSCRIPT

  • 1. LINK LISTSHEETAL WAGHMAREM.TECH (Computer Science & Data Processing)IIT KHARAGPUREMAIL-ID: [email protected]@gmail.com

2. Instruction To Use This Presentation Dear user I have kept someanimated/executable/video files so it will be easy foryou to understand but that wont be run/play inslideshow To see/run that files you have to exit from slideshowthen click on the icon and also install KM Player For example: click on the below icons one by one(one is executable file and other is video) you will beable to see the animationSHEETAL WAGHMARE FROM IIT KHARAGPUR 3. CONTENTSSINGLY LINKLIST (INSERTION,DELETION,SORT,SEARCH)STACK(PUSH,POP)QUEUE(ADD,REMOVE)CIRCULAR LINKLISTDOUBLE ENDED QUEUECIRCULAR QUEUEPRIORITY QUEUEDOUBLY LINKLIST(INSERTION,DELETION,SORT,SEARCH)SHEETAL WAGHMARE FROM IITKHARAGPUR 4. Whats wrong with Array and Why lists? Disadvantages of arrays as storage data structures: slow searching in unordered array slow insertion in ordered array Fixed size Linked lists solve some of these problemsSHEETAL WAGHMARE FROM IITKHARAGPUR 5. List Implementation using Linked Lists Linked list Linear collection of self-referential class objects, called nodes Connected by pointer links Accessed via a pointer to the first node of the list Link pointer in the last node is set to null to mark the lists end Use a linked list instead of an array when You have an unpredictable number of data elements You want to insert and delete quickly.SHEETAL WAGHMARE FROM IITKHARAGPUR 6. Linked Lists A B C Head A linked list is a series of connected nodes Each node contains at least A piece of data (any type) Pointer to the next node in the list Head: pointer to the first nodenode The last node points to NULLA SHEETAL WAGHMAREFROM IITdata pointer KHARAGPUR 7. Part II: Linked ListsAs an abstract data type, a list is a finite sequence (possibly empty) ofelements with basic operations that vary from one application toanother.Basic operations commonly include:Construction: Allocate and initialize a list object (usually empty)Empty:Check if list is emptyInsert: Add an item to the list at any pointDelete: Remove an item from the list at any pointTraverse:Go through the list or a part of it, accessing andprocessing theWAGHMARE in the order they are stored SHEETAL elements FROM IIT KHARAGPUR 8. Linked Representation Data structure for a linked list: first NodeDataLink (pointer): used to store the address of the next node. SHEETAL WAGHMAREFROM IIT KHARAGPUR 9. Anatomy of a linked list A linked list consists of: A sequence of nodesmyList abcd Each node contains a value and a link (pointer or reference) to some other node The last node contains a null link The list may (or may not) have a headerSHEETAL WAGHMARE FROM IITKHARAGPUR 10. More terminology A nodes successor is the next node in the sequence The last node has no successor A nodes predecessor is the previous node in the sequence The first node has no predecessor A lists length is the number of elements in it A list may be empty (contain no elements)SHEETAL WAGHMARE FROM IITKHARAGPUR 11. Linked Lists Types of linked lists: Singly linked list Begins with a pointer to the first node Terminates with a null pointer Only traversed in one direction Circular, singly linked Pointer in the last node points back to the first node Doubly linked list Two start pointers first element and last element Each node has a forward pointer and a backward pointer Allows traversals both forwards and backwards Circular, doubly linked list Forward pointer of the last node points to the first node and backward pointer of the first node points to the last nodeSHEETAL WAGHMARE FROM IIT KHARAGPUR 12. Declarations First you must declare a data structure that will be used for the nodes. For example, the following struct could be used to create a list where each node holds a float:SHEETAL WAGHMARE FROM IITKHARAGPUR 13. Conventions of Linked ListThere are several conventions for the link to indicate the end of the list.1. A null link that points to no node (0 or NULL)2. A dummy node that contains no item3. A reference back to the first node, making it a circular list. SHEETAL WAGHMARE FROM IIT KHARAGPUR 14. Convention of the linked list A special list is maintained which consists of unused memorycells. This list, which has its own pointer, is called the list ofavailable space or the free storage list or the free pool. The operating system of a computer may periodically collect allthe deleted space onto the free storage list. Any techniquewhich does this collection is called garbage collection. SHEETAL WAGHMARE FROM IIT KHARAGPUR 15. Conventions Sometimes new data are to be inserted into the data structure butthere is no available space, i.e. the free storage list is empty. Thissituation is usually called overflow.means when AVAIL = NULL and there is an insertion. The term underflow refers to the situation where one wants to deletedata from a data structure that is empty. The programmer may handleunderflow by printing the message UNDERFLOW. means If START = NULL, SHEETAL WAGHMARE FROM IIT KHARAGPUR 16. Inserting to the Fronthead93 head48 17142 There is no work to find the correct location Empty or not, head will point to the right locationSHEETAL WAGHMARE FROM IITKHARAGPUR 17. Insert first position INFIRST(INFO,START,AVAIL,ITEM)1. [OVERFLOW] if AVAIL=NULL then write:OVERFLOW and exit.2. [remove first node from AVAIL list]set NEW=AVAIL and AVAIL=LINK[AVAIL].3. Set INFO[NEW]=ITEM [copies new data into new node]4. Set LINK[NEW]=START [new node now points to original first node]5. Set START=NEW [changes START so it points to the new node]6. ExitSHEETAL WAGHMARE FROM IITKHARAGPUR 18. Inserting to the Middlehead1748142 93 //142 // Used when order is important Go to the node that should follow the one to add Recursion or iterationSHEETAL WAGHMARE FROM IITKHARAGPUR 19. Algorithm INSLOC[INFO,LINK,START,AVAIL,LOC,ITEM]1. [OVERFLOW] if AVAIL=NULL then write OVERFLOW and exit.2. [remove first node from AVAIL list] set NEW:=AVAIL and AVAIL:=LINK[AVAIL]3. Set INFO[NEW]:=ITEM[copies new data into new node]4. If LOC=NULL then [insert as a first node ] set LINK[NEW]:=START and START:=NEWElse [insert after node with location Loc] set LINK[NEW]:=LINK[LOC] and LINK[LOC]:=NEW[end of if structure]5. Exitafter.exe SHEETAL WAGHMARE FROM IIT KHARAGPUR 20. Inserting to the Endhead4817142//93 // Find the end of the list (when at NIL) Recursion or iterationSHEETAL WAGHMARE FROM IITKHARAGPUR 21. Inserting to End of a Linked List Recursively traverse the list until at end Then: Create new node at current Fill in data Terminate the new nodes next pointer to point to NIL SHEETAL WAGHMARE FROM IIT KHARAGPUR 22. Inserting to End of a Linked List 1. allocate memory for the new node. 2. Assign value to the data of the new node. 3. if START is NULL then(if the list is empty) a. Make START point to the NEW node.b. make LAST point to the new nodec. go to step 6. 4. Make the next field of LAST point to the New node 5. Mark the new node as LAST. 6. Make the next field of the new node point to NULL.SHEETAL WAGHMAREFROM IITKHARAGPUR 23. Inserting to End of a Linked List INLAST(INFO,START,AVAIL,ITEM)1. [OVERFLOW] if AVAIL=NULL then write:OVERFLOW and exit.2, [remove first node from AVAIL list]set NEW=AVAIL and AVAIL=LINK[AVAIL].3. Set INFO[NEW]=ITEM [copies new data into new node]4. If START=NULLSet START=NEW and LOC=START5. Repeat step 6 untill LINK[LOC] != NULL6. Set LOC=LINK[LOC]7. Set LINK[LOC]=NEW8. ExitSHEETAL WAGHMARE FROM IITKHARAGPUR 24. Inserting at the End of a Linked Listhead 48 17 14253 currentRnew_data 53 current Rnew_data 53currentRnew_data 53current Rnew_data 53 SHEETAL WAGHMARE FROM IIT end.exe KHARAGPUR 25. Find the desired element FINDB(INFO,LINK,START,ITEM,LOC,LOCP] This function finds the location LOC of the first node N which contains ITEM and the location LOCP of the node preceding N. if ITEM does not appear in the list, then the function set LOC=NULL and it item appears in the first node then it sets LOCP=NULL] 1.[list empty?] if START=NULL then set LOC=NULL andLOCP=NULL and return. [end of if structure] 2. [ITEM in first node?] if INFO[START]==ITEM then Set LOC=START and LCOP=NULL and return. [end of if structure] 3 . Set SAVE=START and PTR=LINK[START] [intializespointers] 4. Repeat step 5 and 6 while PTR != NULL 5. Contd.SHEETAL WAGHMAREFROM IITKHARAGPUR 26. Contd. 5. if INFO[PTR]=ITEM then Set LOC= PTR and LOCP=SAVE and return [end of IF structure]6. set SAVE=PTR and PTR=LINK[PTR] [ updates pointers][end of step 4 loop] 7. set LOC=NULL. [search unsuccessful] 8. return .SHEETAL WAGHMARE FROM IITKHARAGPUR 27. Delete DELETE(INFO,LINK,START,AVAIL,ITEM) This algorithm deletes from a linked list the first node N whichcontains the given ITEM of information 1.[use function FINDB]call FINDB 2. if LOC=NULL then write ITEM not in list and exit 3. [delete node]if LOCP=NULL then set START=LINK[START] [delete firstnode] Else set LINK[ LOCP]=LINK[LOC] [end of if structure] 4.[return deleted node to the AVAIL list]set LINK[LOC]=AVAIL and AVAIL=LOC Exit deletion.exe SHEETAL WAGHMARE FROM IIT KHARAGPUR 28. SortingSHEETAL WAGHMARE FROM IITKHARAGPUR 29. Singly Linked Lists and ArraysSingly linked list Array Elements are stored in linear Elements are stored in linear order, accessible with links. order, accessible with an index. Do not have a fixed size. Have a fixed size. Cannot access the previousCan access the previous element directly. element easily. No binary search. Binary search. SHEETAL WAGHMAREFROM IIT KHARAGPUR 30. ARRAY IMPLEMENTATION OF STACK PUSH:- 1.if(top==max-1) 1.print(stack overflow) 2.else 1.top=top+1 2.stack arr[top]=pushed item 3.endif end algorithm POP:- 1.if (top==-1) 1.print(stack underflow) 2.else 1.print(popped element is stack arr[top]) 2.top=top-1 3.end if end algorithmSHEETAL WAGHMARE FROM IITKHARAGPUR 31. QUEUE IMPLEMENTATION Procedure Q INSERT (Q, FRONT, REAR,MAX,Y)1.[Overflow ?]IF REAR>=NTHEN WRITE ("OVERFLOW") RETURN2.REAR