Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked List X Header 35 20 4000 100020003000 2000 3000 55 X 4000 ptr Question: Search a value 40 in the linked list. Steps: ptr = Header->Link While(ptr.

Similar presentations


Presentation on theme: "Linked List X Header 35 20 4000 100020003000 2000 3000 55 X 4000 ptr Question: Search a value 40 in the linked list. Steps: ptr = Header->Link While(ptr."— Presentation transcript:

1 Linked List X Header 35 20 4000 100020003000 2000 3000 55 X 4000 ptr Question: Search a value 40 in the linked list. Steps: ptr = Header->Link While(ptr != NULL) AND (ptr->Data != 40), do ptr NULL EndWhile ptr = ptr->Link Steps: (cntd) If(ptr->Data == 40) print “Search successful.” print “Search unsuccessful.” Else EndIf Stop 1000 Circular Linked List While(ptr != Header) [Null Link Problem] ptr

2 Linked List Circular Linked List: –Advantages: Virtual traversal possible in both the directions. –Left to Right (Direct) –Right to Left (Indirect) Overcomes the NULL Link problem.

3 Circular Linked List Algorithm: InsertFront_CL, X = 10 NULL Header 20 30 1000 10 100020003000 new 4000 20003000 4000 2000 ptr

4 Circular Linked List Algorithm: InsertEnd_CL, X = 10 NULL Header 20 30 1000 10 100020003000 new 4000 2000 3000 1000 4000 ptr

5 Circular Linked List Algorithm: Merge_CL X Header1 35 25 4000 100020003000 2000 3000 55 4000 1000 X Header2 15 5 5000 800070006000 7000 6000 65 8000 5000 ptr ptr1 7000 ptr1 2000 Header

6 Linked List X Header 35 25 4000 100020003000 2000 3000 55 4000 NULL X Header 35 25 4000 100020003000 2000 3000 55 1000 4000 Single Linked List Circular Linked List / Circular Single Linked List Null Link Problem: Yes 2-way traversal: No Null Link Problem: No 2-way traversal: Yes (Only virtual) Node Structure DATA LINK DATA RLINKLLINK NextPreviousNext

7 Linked List Node Structure DATA RLINKLLINK NextPrevious X X 5000 1000 40 7000 5000 60 X 7000 Header Double Linked List / Doubly Linked List Left to Right Right to Left Traversal?

8 Algorithm: InsertFront_DL X X 5000 1000 15 7000 5000 25 X 7000 Header Question: Insert a nodeAt the front.Value = 35 1000 35 5000 9000 new 9000 ptr Steps: new = GetNode(NODE) If(new == NULL), then Else print “Memory Insufficient.” ptr = Header->RLink new->Data = 35 new->LLink = Header new->RLink = ptr Header->RLink = new ptr->LLink = new EndIf Steps: (cntd) X

9 Trace Algorithm: InsertFront_DL, X = 35 X X X 1000 Header ptr Steps: new = GetNode(NODE) If(new == NULL), then Else print “Memory Insufficient.” ptr = Header->RLink new->Data = X new->LLink = Header new->RLink = ptr Header->RLink = new ptr->LLink = new EndIf Stop NULL If(ptr != NULL), then EndIf 1000 35 X 2000 new 2000

10 Linked List Algorithm: –InsertFront_DL Input: –Header: Pointer to the starting node of the linked list. –X: Data of the node to be inserted at front. Output: –Node with data X inserted at front of Double Linked List. Data Structure: –Double Linked List whose address of starting node is known from Header.

11 Algorithm: InsertFront_DL Steps: new = GetNode(NODE) If(new == NULL), then print “Memory Insufficient.” Else ptr = Header->RLink new->Data = X new->LLink = Header new->RLink = ptr Header->RLink = new If(ptr != NULL), then ptr->LLink = new EndIf Stop

12 Algorithm: InsertEnd_DL X X 5000 1000 15 7000 5000 25 X 7000 Header Question: Insert a nodeAt the end.Value = 35 7000 35 X 9000 new 9000 ptr Steps: new = GetNode(NODE) If(new == NULL), then Else print “Memory Insufficient.” new->Data = 35 new->LLink = ptr new->RLink = NULL ptr->RLink = new EndIf Steps: (cntd) X ptr While(ptr->RLink != NULL) EndWhile ptr = ptr->RLink //new->RLink = ptr->RLink Stop ptr ptr = Header

13 new->LLink = ptr Trace Algorithm: InsertEnd_DL, X = 35 X X X 1000 Header Steps: new = GetNode(NODE) 1000 35 X 2000 new 2000 ptr If(new == NULL), then Else print “Memory Insufficient.” new->Data = X new->RLink = NULL ptr->RLink = new EndIf While(ptr->RLink != NULL) EndWhile ptr = ptr->RLink //new->RLink = ptr->RLink Stop ptr = Header

14 Linked List Algorithm: –InsertEnd_DL Input: –Header: Pointer to the starting node of the linked list. –X: Data of the node to be inserted at end. Output: –Node with data X inserted at end of Double Linked List. Data Structure: –Double Linked List whose address of starting node is known from Header.

15 Algorithm: InsertEnd_DL Steps: new = GetNode(NODE) If(new == NULL), then print “Memory Insufficient.” Else ptr = Header While(ptr->RLink != NULL) ptr = ptr->RLink EndWhile new->Data = X new->LLink = ptr new->RLink = NULL //new->RLink = ptr->RLink ptr->RLink = new EndIf Stop

16 Algorithm: InsertAny_DL Question: Insert a node.After node with value 25.Value = 35 X X 3000 1000 45 5000 3000 25 7000 5000 Header 9000 ptr 5000 15 X 7000 9000 ptr1 5000 35 7000 9000 new X X 3000 1000 45 5000 3000 55 7000 5000 Header ptr 5000 15 X 7000 ptr

17 Algorithm: InsertAny_DL Question: Insert a node. After node with value 25. Value = 35 Steps: ptr = Header While(ptr->RLink != NULL) AND (ptr->Data != 25) EndWhile ptr = ptr->RLink new = GetNode(NODE) If(new == NULL), then print “Memory Insufficient.” Else If(ptr->Data != 25), then print “25 not found in the linked list.” Else ptr1 = ptr->RLink new->Data = 35 new->LLink = ptr new->RLink = ptr1 EndIf ptr->RLink = new ptr1->LLink = new Stop X KEY

18 X X 3000 1000 45 5000 3000 25 X 5000 Header 7000 ptr 5000 35 X 7000 new Trace Algorithm: InsertAny_DL Question: Insert a node. KEY = 25.X = 35 Steps: ptr = Header While(ptr->RLink != NULL) AND (ptr->Data != KEY) EndWhile ptr = ptr->RLink new = GetNode(NODE) If(new == NULL), then print “Memory Insufficient.” Else If(ptr->Data != KEY), then print “KEY not found in the linked list.” Else ptr1 = ptr->RLink new->Data = X new->LLink = ptr new->RLink = ptr1 EndIf ptr->RLink = new ptr1->LLink = new Stop Steps: (cntd) If(ptr1 != NULL) EndIf ptr1 NULL

19 Linked List Algorithm: –InsertAny_DL Input: –Header: Pointer to the starting node of the linked list. –KEY: Data of the node after which a new node is to be inserted. –X: Data of the node to be inserted. Output: –Node with data X inserted after node with data KEY if successful, a message otherwise. Data Structure: –Double Linked List whose address of starting node is known from Header.

20 Algorithm: InsertAny_DL Steps: new = GetNode(NODE) If(new == NULL), then print “Memory Insufficient.” Else ptr = Header While(ptr->RLink != NULL) AND (ptr->Data != KEY) ptr = ptr->RLink EndWhile If(ptr->Data != KEY), then print “KEY not found in the linked list.” Else ptr1 = ptr->RLink new->Data = X new->LLink = ptr new->RLink = ptr1 ptr->RLink = new If(ptr1 != NULL) ptr1->LLink = new EndIf

21 Algorithm: DeleteFront_DL Question: Delete a node.From the front of linked list. X X 3000 1000 45 5000 3000 25 7000 5000 Header 5000 ptr ptr1 5000 15 X 7000 1000 X X X Header Steps: If(Header->RLink == NULL) print “Empty Linked List. Deletion not possible.” Else ptr = Header->RLink ptr1 = ptr->RLink Header->RLink = ptr1 ptr1->LLink = Header ReturnNode(ptr) EndIf

22 Trace Algorithm: DeleteFront_DL Question: Delete a node.From the front of linked list. X X 3000 1000 45 X 3000 Header X ptr Steps: If(Header->RLink == NULL) print “Empty Linked List. Deletion not possible.” Else ptr = Header->RLink ptr1 = ptr->RLink Header->RLink = ptr1 ptr1->LLink = Header ReturnNode(ptr) EndIf Stop If(ptr1 != NULL) EndIf ptr1 NULL

23 Linked List Algorithm: –DeleteFront_DL Input: –Header: Pointer to the starting node of the linked list. Output: –Node from the front deleted from the linked list if it exists, a message otherwise. Data Structure: –Double Linked List whose address of starting node is known from Header.

24 Algorithm: DeleteFront_DL Steps: If(Header->RLink == NULL) print “Empty Linked List. Deletion not possible.” Else ptr = Header->RLink ptr1 = ptr->RLink Header->RLink = ptr1 If(ptr1 != NULL) ptr1->LLink = Header EndIf ReturnNode(ptr) EndIf Stop

25 Algorithm: DeleteEnd_DL Question: Delete a node.From the end of linked list. X X 3000 1000 45 5000 3000 25 7000 5000 Header ptr 5000 15 X 7000 X X X 1000 Header Steps: If(Header->RLink == NULL) print “Empty Linked List. Deletion not possible.” Else ptr1 = ptr->LLink ptr1->RLink = NULL ReturnNode(ptr) EndIf ptr ptr1 While(ptr->RLink != NULL) EndWhile ptr = ptr->RLink X ptr ptr = Header // ptr1->RLink = ptr->RLink

26 Algorithm: DeleteEnd_DL Question: Delete a node.From the end of linked list. X X 3000 1000 45 X Header ptr Steps: If(Header->RLink == NULL) print “Empty Linked List. Deletion not possible.” Else ptr1 = ptr->LLink ptr1->RLink = NULL ReturnNode(ptr) EndIf ptr1 While(ptr->RLink != NULL) EndWhile ptr = ptr->RLink X ptr ptr = Header // ptr1->RLink = ptr->RLink Stop

27 Linked List Algorithm: –DeleteEnd_DL Input: –Header: Pointer to the starting node of the linked list. Output: –Node from the end deleted from the linked list if it exists, a message otherwise. Data Structure: –Double Linked List whose address of starting node is known from Header.

28 Algorithm: DeleteEnd_DL Steps: If(Header->RLink == NULL) print “Empty Linked List. Deletion not possible.” Else ptr = Header While(ptr->RLink != NULL) ptr = ptr->RLink EndWhile ptr1 = ptr->LLink ptr1->RLink = NULL //ptr1->RLink = ptr->RLink ReturnNode(ptr) EndIf Stop

29 Algorithm: DeleteAny_DL Question: Delete a node.Value = 25 X X 3000 1000 45 5000 3000 25 7000 5000 Header ptr 5000 15 9000 7000 ptr1 7000 ptr 7000 35 X 9000 3000 ptr2 X X 3000 1000 45 5000 3000 15 7000 5000 Header ptr 5000 35 X 7000 ptr X X X 1000 Header

30 Algorithm: DeleteAny_DL Question: Delete a node.Value = 25 Steps: ptr = Header While(ptr->RLink != NULL) AND (ptr->Data != 25) ptr = ptr->RLink EndWhile If(ptr->Data != 25) ptr1 = ptr->LLink ptr2 = ptr->RLink ptr1->RLink = ptr2 ptr2->LLink = ptr1 ReturnNode(ptr) Else print “25 not found. Deletion not possible.” EndIf Stop KEY

31 Trace Algorithm: DeleteAny_DL KEY = 25 Steps: ptr = Header While(ptr->RLink != NULL) AND (ptr->Data != KEY) ptr = ptr->RLink EndWhile If(ptr->Data != KEY) ptr1 = ptr->LLink ptr2 = ptr->RLink ptr1->RLink = ptr2 ptr2->LLink = ptr1 ReturnNode(ptr) Else print “KEY not found. Deletion not possible.” EndIf Stop Steps: (cntd) If(ptr2 != NULL) EndIf X X 3000 1000 45 5000 3000 25 X 5000 Header ptr ptr2 NULL ptr1 X

32 Linked List Algorithm: –DeleteAny_DL Input: –Header: Pointer to the starting node of the linked list. –KEY: Data of the node to be deleted. Output: –Node with data KEY deleted if it exists, a message otherwise. Data Structure: –Double Linked List whose address of starting node is known from Header.

33 Algorithm: DeleteAny_DL Steps: ptr = Header While(ptr->RLink != NULL) AND (ptr->Data != KEY) ptr = ptr->RLink EndWhile If(ptr->Data != KEY) print “KEY not found. Deletion not possible.” Else ptr1 = ptr->LLink ptr2 = ptr->RLink ptr1->RLink = ptr2 If(ptr2 != NULL), ptr2->LLink = ptr1 EndIf ReturnNode(ptr) EndIf Stop

34 Algorithm: Merge_DL Question: Merge 2 linked lists into a single double linked list. X X 2000 1000 15 3000 2000 35 X 3000 Header1 ptr 8000 X X 7000 25 9000 8000 45 X 9000 Header2 ptr1 3000 Steps: ptr = Header1 While(ptr->RLink != NULL) EndWhile ptr = ptr->RLink ptr1 = Header2->RLink Steps: (cntd) ptr->RLink = ptr1 ptr1->LLink = ptr Stop Header ReturnNode(Header2) Header = Header1 Needs to be conditional.

35 Trace Algorithm: Merge_DL Steps: ptr = Header1 While(ptr->RLink != NULL) EndWhile ptr = ptr->RLink ptr1 = Header2->RLink ptr->RLink = ptr1 ptr1->LLink = ptr Stop ReturnNode(Header2) Header = Header1 If(ptr1 != NULL) EndIf X X X 1000 Header1 ptr X X X 2000 Header2 Header ptr1 NULL X X 2000 1000 Header1 ptr X X X 3000 Header2 Header ptr1 NULL 1000 15 X 2000 ptr

36 Linked List Algorithm: –Merge_DL Input: –Header1: Pointer to the starting node of the 1 st linked list. –Header2: Pointer to the starting node of the 2 nd linked list. Output: –Header: Pointer to the node of merged/combined/joined linked list. Data Structure: –Double Linked List whose address of starting node is known from Header.

37 Algorithm: Merge_DL Steps: ptr = Header1 While(ptr->RLink != NULL) ptr = ptr->RLink EndWhile ptr1 = Header2->RLink ptr->RLink = ptr1 If(ptr1 != NULL) ptr1->LLink = ptr EndIf ReturnNode(Header2) Header = Header1 Stop

38 Algorithm: Split_DL Question: Split a list into 2 separate lists. X X 2000 1000 15 3000 2000 35 4000 3000 Header ptr 3000 25 5000 4000 45 X 5000 ptr1 From value = 35 X X 4000 6000 Header1 X X 2000 1000 15 3000 2000 45 4000 3000 Header 3000 25 X 4000 ptr X 6000

39 Algorithm: Split_DL Question: Split a list into 2 separate lists.From value = 35 Steps: ptr = Header While(ptr->RLink != NULL) AND (ptr->Data != 35) EndWhile ptr = ptr->RLink If(ptr->Data == 35) Header1 = GetNode(NODE) ptr1 = ptr->RLink Header1->Data = NULL Header1->LLink = NULL Header1->RLink = ptr1 ptr->RLink = NULL ptr1->LLink = Header1 Else print “35 not found. Split not possible.” EndIf KEY Needs to be conditional.

40 Trace Algorithm: Split_DL, KEY = 35 Steps: ptr = Header While(ptr->RLink != NULL) AND (ptr->Data != KEY) EndWhile ptr = ptr->RLink If(ptr->Data == KEY) Header1 = GetNode(NODE) ptr1 = ptr->RLink Header1->Data = NULL Header1->LLink = NULL Header1->RLink = ptr1 ptr->RLink = NULL ptr1->LLink = Header1 Else print “KEY not found. Split not possible.” EndIf If(ptr! != NULL) EndIf X X 2000 1000 35 X 2000 Header ptr ptr1 NULL X X X 3000 Header1

41 Linked List Algorithm: –Split_DL Input: –Header: Pointer to the starting node of the linked list. –KEY: Data of the node after which the list is supposed to be split. Output: –Header1: Pointer to the starting node of the 2 nd linked list if split is successful or message otherwise. Data Structure: –Double Linked List whose address of starting node is known from Header.

42 Algorithm: Split_DL Steps: ptr = Header While(ptr->RLink != NULL) AND (ptr->Data != KEY) ptr = ptr->RLink EndWhile If(ptr->Data == KEY) Header1 = GetNode(NODE) ptr1 = ptr->RLink Header1->Data = NULL Header1->LLink = NULL Header1->RLink = ptr1 ptr->RLink = NULL If(ptr1 != NULL) ptr1->LLink = Header1 EndIf Else print “KEY not found. Split not possible.” EndIf Stop

43 Linked List Double Linked List X X 20001000 15 30002000 45 X Header 100020003000 Circular Circular Doubly Linked List Null Link Problem: Yes 1000 3000

44 Applications of Linked List 5x 2 y 2 + 3x 2 y - 20y - 100 Polynomial in 2 variables Co-efficient Node Structure: CoefficientPower of xPower of yLink Next Node XXX522321 -2001 -10000 X Header Representation of Polynomials

45 Applications of Linked List 10x 4 y 3 + 5xy - 50 Polynomial in 2 variables XXX1043 511 -5000 X Header Representation of Polynomials

46 Applications of Linked List P(x) = 20x 3 - 15x 2 - 50x + 100 Polynomial in 1 variable Representation of Polynomials Node Structure: CoefficientPower of xLink Next Node Node Structure: CoefficientPower of xPower of yLink Next Node Power of z P(x,y,z) = 10x 3 y 2 z + 5xy 2 z 2 +....... Polynomial in 3 variables


Download ppt "Linked List X Header 35 20 4000 100020003000 2000 3000 55 X 4000 ptr Question: Search a value 40 in the linked list. Steps: ptr = Header->Link While(ptr."

Similar presentations


Ads by Google