Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture - 6 On Data Structures

Similar presentations


Presentation on theme: "Lecture - 6 On Data Structures"— Presentation transcript:

1 Lecture - 6 On Data Structures
Linked list

2 Lecture Outline Inserting into a sorted linked list Finding a Node
Deletion from a Linked List Variations of Linked Lists: (a) Circular linked lists (b) Doubly linked lists Array versus Linked Lists

3 Inserting into a sorted linked list
ITEM must be inserted between nodes A and B so that INFO(A)<ITEM<=INFO(B) Traverse the list using a pointer variable PTR Comparing the ITEM with INFO[PTR] at each node. Keep track the location of the preceding node by a pointer variable SAVE, as in fig 5-20 SAVE and PTR are updated by the assignments SAVE : =PTR and PTR : =LINK[PTR] START Node A Node B Fig :5-20 SAVE PTR

4 FINDA(INFO, LINK, START, ITEM, LOC)
Finding a Node FINDA(INFO, LINK, START, ITEM, LOC) This procedure finds the location LOC of the last node in a sorted list such that INFO[LOC] < ITEM or sets LOC= NULL [List Empty?] If START = NULL then: Set LOC = NULL and return. [special case?] If ITEM < INFO[START], then: Set SAVE=START and PTR = LINK[START] [initialize pointer] Repeat step 5 and 6 while PTR ≠ NULL. If ITEM < INFO[PTR] then: Set LOC= SAVE and Return. Set SAVE = PTR and PTR= LINK[PTR] [update pointer] (End of step 4) Set LOC= SAVE Exit.

5 Inserting into a sorted linked list
Algorithm 5.7: INSSRT(INFO, LINK, START, AVAIL, ITEM) This algorithm inserts an item into a sorted linked list. [Use Procedure 5.6 to find the location of the node preceding ITEM] Call FINDA(INFO, LINK, START, ITEM, LOC). [Use algorithm 5.5 to insert ITEM after the node with location LOC] Call INSLOC(INFO, LINK, START, AVAIL, LOC, ITEM). Exit

6 Deletion from a Linked List
The next pointer field of node-A now points to node-B, where node-N previously pointed The next pointer field of N now points to the original first node in the free pool, where AVAIL previously pointed. AVAIL now points to the deleted node-N START Node A Node N (a) Before deletion Node B Node A Node B After deletion AVAIL Free storage list START Node N

7 Deleting a node DELETE(INFO, LINK, START, AVAIL, ITEM)
Delete a node with the value equal to INFO from the list. If such a node is found, return its position. Otherwise, return NULL. Steps Find the desirable node (similar to FINDB) Release the memory occupied by the found node Set the pointer of the predecessor of the found node to the successor of the found node Like INSLOC, there are two special cases Delete first node Delete the node in middle or at the end of the list

8 FINDB(INFO, LINK, START, ITEM, LOC, LOCP)
Finding a Node FINDB(INFO, LINK, START, ITEM, LOC, LOCP) This procedure finds the location LOC of the first node N which contains ITEM and the location LOCP of the node preceding node N. If ITEM does not appear in the list, then sets LOC=NULL If ITEM appear in the first node, then sets LOCP=NULL [List Empty?] If START = NULL then: Set LOC = NULL and LOCP = NULL and return. [ITEM in the first node?] If INFO[] = ITEM, then: Set LOC = START and LOCP = NULL and return. Set SAVE=START and PTR = LINK[START] [initialize pointer] Repeat step 5 and 6 while PTR ≠ NULL. If INFO[] = ITEM then: Set LOC= PTR and LOCP = SAVE and Return. Set SAVE = PTR and PTR= LINK[PTR] [update pointer] Set LOC= NULL Exit.

9 Deleting a node Try to find the node with its value equal to N
DELETE(INFO, LINK, START, AVAIL, ITEM) Call FINDB(INFO, LINK, START, ITEM, LOC, LOCP) IF LOC = NULL then: ITEM not in the list and exit. [Delete node] IF LOCP = NULL then: Set START= = LINK[START] [Delete first node] Else: Set LINK[LOCP] = LINK [LOC] Return deleted node to the AVAIL list Set LINK[LOC] = AVAIL and AVAIL = LOC Exit. Try to find the node with its value equal to N

10 Deleting a node DELETE(INFO, LINK, START, AVAIL, ITEM)
Call FINDB(INFO, LINK, START, ITEM, LOC, LOCP) IF LOC = NULL then: ITEM not in the list and exit. [Delete node] IF LOCP = NULL then: Set START= = LINK[START] [Delete first node] Else: Set LINK[LOCP] = LINK [LOC] Return deleted node to the AVAIL list Set LINK[LOC] = AVAIL and AVAIL = LOC Exit. LOC START

11 Deleting a node DELETE(INFO, LINK, START, AVAIL, ITEM)
Call FINDB(INFO, LINK, START, ITEM, LOC, LOCP) IF LOC = NULL then: ITEM not in the list and exit. [Delete node] IF LOCP = NULL then: Set START= = LINK[START] [Delete first node] Else: Set LINK[LOCP] = LINK [LOC] Return deleted node to the AVAIL list Set LINK[LOC] = AVAIL and AVAIL = LOC Exit. LOC LOCP

12 Variations of Linked Lists
Circular linked lists The last node points to the first node of the list How do we know when we have finished traversing the list? (Tip: check if the pointer of the current node is equal to the head.)

13 Variations of Linked Lists
circular linked list: In a circular linked list there are two methods to know if a node is the first node or not. Either a external pointer, list, points the first node or A header node is placed as the first node of the circular list. The header node can be separated from the others by either heaving a sentinel value as the info part or having a dedicated flag variable to specify if the node is a header node or not.

14 CIRCULAR LIST with header node
The header node in a circular list can be specified by a sentinel value or a dedicated flag: Header Node with Sentinel: Assume that info part contains positive integers. Therefore the info part of a header node can be -1.

15 CIRCULAR LIST with header node
Header Node with Flag: In this case a extra variable called flag can be used to represent the header node. For example flag in the header node can be 1, where the flag is 0 for the other nodes.

16 Example of application of circular linked list
A good example of an application where circular linked list should be used is a timesharing problem solved by the operating system.   In a timesharing environment, the operating system must maintain a list of present users and must alternately allow each user to use a small slice of CPU time, one user at a time.  The operating system will pick a user, let him/her use a small amount of CPU time and then move on to the next user, etc.   For this application, there should be no NIL pointers unless there is absolutely no one requesting CPU time. - polygon clipping - round robin situations - when you need to rotate the list, rather than the reference.

17 Advantages Each node is accessible from any node.
Address of the first node is not needed. Certain operations, such as concatenation and splitting of string, is more efficient with circular linked list. Disadvantage: Danger of an infinite loop !

18 Linear linked list vs Circular Linked Lists
In linear linked lists if a list is traversed (all the elements visited) an external pointer to the list must be preserved in order to be able to reference the list again. Circular linked lists can be used to help the traverse the same list again and again if needed. A circular list is very similar to the linear list where in the circular list the pointer of the last node points not NULL but the first node.

19 Variations of Linked Lists
Doubly linked lists A linked list in which each node has three parts :one information and 2 pointers: An information field which contains the data of node. a forward pointer (a pointer to the next node in the list) and a backward pointer (a pointer to the node preceding the current node in the list) is called a doubly linked list.  Here is a picture: A Head B C Each node points to not only successor but the predecessor There are two NULL: at the first and last nodes in the list Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists backwards. The primary disadvantage of doubly linked lists are that Each node requires an extra pointer, requiring more space, and The insertion or deletion of a node takes a bit longer (more pointer operations).

20 Example :Doubly linked lists
A doubly linked list of lines in a document that may be kept by a text editor.  The following denotes how each node should appear: To move backward and forward through the document (as it appears on the screen ) and insert or delete lines, a doubly linked list is ideal.  With the cursor on the current line (stored in a pointer "current"), it is easy to move up one line (current = current->prev) or down one line (current = current->next).  With a singly linked list this is possible but probably too slow.

21 Array versus Linked Lists
Linked lists are more complex to code and manage than arrays, but they have some distinct advantages. Dynamic: a linked list can easily grow and shrink in size. We don’t need to know how many nodes will be in the list. They are created in memory as needed. In contrast, the size of a C++ array is fixed at compilation time. Easy and fast insertions and deletions To insert or delete an element in an array, we need to copy to temporary variables to make room for new elements or close the gap caused by deleted elements. With a linked list, no need to move other nodes. Only need to reset some pointers.

22 Solved problems 5.1, 5.2, 5.3, 5.4,5.5, 5.10, 5.11 Supplementary problems 5.14, 5.15, 5.16, 5.17, 5.18, 5.19, 5.20


Download ppt "Lecture - 6 On Data Structures"

Similar presentations


Ads by Google