Download presentation
Presentation is loading. Please wait.
Published byPhilomena Rodgers Modified over 8 years ago
1
Array 10 GB Hard Disk 2 GB 4 GB2 GB 3 GB DATA 4 GB Free Store data in the form of Array (Continuous memory locations) Solution-1: No Solution. Memory Insufficient Solution-2: Shifting of the occupied space to make space. Not a feasible solution.
2
Linked List 10 GB Hard Disk 2 GB 4 GB2 GB 3 GB DATA 4 GB Free Store data in the form of Linked List (Discontinuous memory locations) Solution: How will the data be read / accessed? Start / Header Basic Structure of Linked List: Header
3
Linked List Header Element DATA LINK Link / Pointer to the next node. OR Address of the next node. NODE
4
Linked List Representation Question: Create a linked list containing data 10, 15. Get a node. Avail / Header 7000 5000 2000 1000 10 NULL Header 2000 15 NULL Add ’10’ Add ’15’ Delete ’15’. Return the node. Memory Bank Memory Pool Getting a node. Memory Allocation.Memory Manager Returning a node.Memory De-Allocation.Garbage Collector Process:Called as:Done by: new
5
Linked List Representation 1000 10 Header 2000 15 NULL Linked List 1000 10 Header 2000 15 NULL Header Linked List NULL 1000 500 Same / Similar
6
Linked List in Algo v/s Program Algorithm Program Creation of a node. DATA LINK NODE struct node { int data; struct node * link; } Get a new node. (Memory Manager) new = GetNode(NODE) struct node * header; header = (struct node *) malloc(sizeof(struct node)); Return/Delete a node. (Garbage Collector) Return(ptr) Return(header) ptr, header are the references of the nodes to be deleted. free(ptr) free(header) Process Assume that it exists. float, char etc. Pointer to a structure temp = GetNode(NODE) Access parts of node. new->DATA, new->LINK temp->DATA, temp->LINK header->DATA, header->LINK temp->DATA, temp->LINK
7
Array v/s Linked List Array Linked List Uses the concept of Dynamic Memory Allocation. All the required memory needs to be allocated in advance. Memory can be allocated on demand and not in advance. Has a fixed dimension. So cannot grow or shrink at run time. Can always grow or shrink at run time. Elements are always stored at continuous / contiguous memory locations. Elements might be stored at discontinuous memory locations. Operations like Insertion and Deletion are time-consuming. They might require major shifting of elements. Operations like Insertion and Deletion are simple and less time-consuming. Shifting of elements is not required. Only adjustment of links are required. Uses the concept of Static Memory Allocation. No need to store the address of the next element. Need to store the address of the next element along with the value.
8
Linked List NULL 1000 500 Header 10 700 1000 15 2000 700 20 NULL 2000 Question: Increment all the values of the linked list by 1. 10 needs to be 11, 15 to 16 and 20 to 21. In short, some processing needs to be done on each and every node of the linked list. Algorithm: TraverseLinkedList At the start of the algorithm, we only have a reference to the starting node of the linked list, i.e. Header. Before we can do some process on any node, we need to have a reference to that node.
9
Linked List NULL 1000 500 Header 10 700 1000 15 2000 700 20 NULL 2000 Algorithm: TraverseLinkedList Question: Increment all the values of the linked list by 1. Steps: ptr =Header->Link ptr ptr->Data = ptr->Data + 1 ptr =ptr->Link ptr->Data = ptr->Data + 1 ptr = ptr->Link ptr->Data = ptr->Data + 1 ptr = ptr->Link Steps: ptr = Header->Link While(ptr != NULL) EndWhile ptr->Data = ptr->Data + 1 ptr = ptr->Link ptr NULL 111621
10
Linked List NULL 1000 500 Header 10 700 1000 15 2000 700 20 NULL 2000 Algorithm: TraverseLinkedList Question: Increment all the values of the linked list by 1. ptr Steps: ptr = Header->Link While(ptr != NULL) EndWhile ptr->Data = ptr->Data + 1 ptr = ptr->Link ptr NULL 6000 Header Steps: ptr = Header While(ptr->Link != NULL) EndWhile ptr->Data = ptr->Data + 1 ptr = ptr->Link 1116 21 ptr NULL
11
Linked List NULL 1000 500 Header 10 NULL 1000 NULL 1000 500 10 NULL 1000 Algorithm: TraverseLinkedList Question: Increment all the values of the linked list by 1. ptr Steps: ptr = Header->Link While(ptr != NULL) EndWhile ptr->Data = ptr->Data + 1 ptr = ptr->Link Header ptr Steps: ptr = Header While(ptr->Link != NULL) EndWhile ptr = ptr->Link ptr->Data = ptr->Data + 1 ptr NULL 11 ptr Process(ptr->Data)
12
Linked List Algorithm: TraverseLinkedList NULL 1000 500 Header 10 700 1000 15 2000 700 20 NULL 2000 ptr In which direction was the traversal possible? In a single direction from left to right. ptr Linked List / Header Linked ListSingle Linked List / Singly Linked List Algorithm: Traverse_SL
13
Linked List Algorithm: –Traverse_SL. Input: –Header: Pointer to the starting node of the linked list. Output: –According to Process(). Data Structure: –Single Linked List whose address of starting node is known from Header.
14
Algorithm: Traverse_SL Steps: ptr = Header While(ptr->Link != NULL), do ptr = ptr->Link Process(ptr->Data) EndWhile
15
Algorithm: InsertFront_SL Question: Insert a node in the linked list. NULL Header 20 30 NULL At the beginning of the linked list.Value = 10 10 100020003000 new 4000 2000 3000 4000 2000 Steps: new = GetNode(NODE) ptr = Header->Link new->Data = 10 new->Link = ptr Header->Link = new 10 new->Link = Header->Link = new Header->Link new->Data = Steps: new = GetNode(NODE) ptr Sequence Important ? Yes Sequence Important ? No
16
Algorithm: InsertFront_SL Question: Insert a node in the linked list.At the beginning of the linked list.Value = 10 ptr = Header->Link new->Data = 10 new->Link = ptr Header->Link = new Steps: new = GetNode(NODE) NULL Header 1000 NULL 10 new 2000 NULL ptr NULL 2000 What if a node is not available? X new->Data = X
17
Linked List Algorithm: –InsertFront_SL. Input: –Header: Pointer to the starting node of the linked list. –X: Data of the node to be inserted. Output: –Single Linked List with a newly inserted node at the front. Data Structure: –Single Linked List whose address of starting node is known from Header.
18
Algorithm: InsertFront_SL Steps: new = GetNode(NODE) //Get a memory block of type NODE and stores its pointer in new. If(new == NULL), then print “Memory is Insufficient” Else ptr = Header->Link new->Data = X new->Link = ptr Header->Link = new EndIf Stop
19
Algorithm: InsertEnd_SL Question: Insert a node in the linked list. NULL Header 20 30 NULL At the end of the linked list.Value = 10 10 100020003000 new 4000 2000 3000 NULL Steps: new = GetNode(NODE) 4000 ptr If(new == NULL), then print “Memory Insufficient” Else ptr = Header While(ptr->Link != NULL) ptr = ptr->Link Process(ptr->Data) EndWhile Steps (cntd): new->Data = 10 new->Link = NULL //new->Link = ptr->Link ptr->Link = new EndIf Stop
20
Algorithm: InsertEnd_SL Question: Insert a node in the linked list. NULL Header At the end of the linked list.Value = 10 1000 NULL Steps: new = GetNode(NODE) ptr If(new == NULL), then print “Memory Insufficient” Else ptr = Header While(ptr->Link != NULL) ptr = ptr->Link EndWhile new->Data = 10 new->Link = NULL //new->Link = ptr->Link ptr->Link = new EndIf Stop Traverse to the end of Single Linked List. 10 new 2000 NULL 2000 new->Data = X
21
Linked List Algorithm: –InsertEnd_SL Input: –Header: Pointer to the starting node of the linked list. –X: Data of the node to be inserted. Output: –Single Linked List with a newly inserted node at the end. Data Structure: –Single Linked List whose address of starting node is known from Header.
22
Algorithm: InsertEnd_SL Steps: new = GetNode(NODE) //Get a memory block of type NODE and stores its pointer in new. If(new == NULL), then print “Memory is Insufficient” Else ptr = Header While(ptr->Link != NULL) ptr = ptr->Link EndWhile new->Data = X new->Link = NULL // new->Link = ptr->Link ptr->Link = new EndIf Stop
23
Algorithm: DeleteFront_SL Question: Delete a node from the linked list. NULL Header 20 30 4000 At the front of the linked list. 100020003000 2000 3000 10 NULL 4000 3000 ptr ptr1 Steps: ptr = Header->Link ptr1 = ptr->Link Header->Link = ptr1 ReturnNode(ptr) Header->Link = ptr->Link Steps: ptr = Header->Link ReturnNode(ptr) Sequence does not matter. Sequence matters.
24
Algorithm: DeleteFront_SL Question: Delete a node from the linked list. NULL Header At the front of the linked list. 1000 NULL Steps: ptr = Header->Link ptr1 = ptr->Link Header->Link = ptr1 ReturnNode(ptr) ptr NULL Steps: ptr = Header->Link If(Header->Link == NULL) Else print “List is empty. Deletion not possible” ptr1 = ptr->Link Header->Link = ptr1 ReturnNode(ptr) EndIf
25
Linked List Algorithm: –DeleteFront_SL Input: –Header: Pointer to the starting node of the linked list. Output: –Single Linked List after eliminating/deleting node from the front of the list in case the list is not empty. Data Structure: –Single Linked List whose address of starting node is known from Header.
26
Algorithm: DeleteFront_SL Steps: If(Header->Link == NULL), then print “List is Empty. Deletion not possible.” Else ptr = Header->Link ptr1 = ptr->Link Header->Link = ptr1 ReturnNode(ptr) //Return the memory block referenced by ptr to memory bank. EndIf Stop
27
Algorithm: DeleteEnd_SL Question: Delete a node from the linked list. NULL Header 20 30 4000 From the end of the linked list. 100020003000 2000 3000 10 NULL 4000 Steps: NULL ptr ptr1 ptr = Header While(ptr->Link != NULL), do EndWhile ptr = ptr->Link ptr1 = ptr Steps: ptr1->Link = NULL ReturnNode(ptr) //ptr1->Link = ptr->Link EndIf If(Header->Link == NULL), then print “List is empty. Deletion not possible” Else (cntd)
28
Algorithm : DeleteEnd_SL Question: Delete a node from the linked list. NULL Header 20 From the end of the linked list. 10002000 NULL Steps: ptr ptr1 ptr = Header While(ptr->Link != NULL), do EndWhile ptr = ptr->Link ptr1 = ptr ptr1->Link = NULL ReturnNode(ptr) //ptr1->Link = ptr->Link EndIf If(Header->Link == NULL), then print “List is empty. Deletion not possible” Else NULL Stop
29
Linked List Algorithm: –DeleteEnd_SL Input: –Header: Pointer to the starting node of the linked list. Output: –Single Linked List after eliminating/deleting node from the end of the list in case the list is not empty. Data Structure: –Single Linked List whose address of starting node is known from Header.
30
Algorithm: DeleteEnd_SL Steps: If(Header->Link == NULL), then print “List is Empty. Deletion not possible.” Else ptr = Header While(ptr->Link != NULL), do ptr1 = ptr ptr = ptr->Link EndWhile ptr1->Link = NULL //ptr1->Link = ptr->Link ReturnNode(ptr) //Return the memory block referenced by ptr to memory bank. EndIf Stop
31
Algorithm: Search_SL Question: Search for a particular node in the linked list. NULL Header 10 15 4000 Value = 15 100020003000 2000 3000 20 NULL 4000 NULL Header 10 30 4000 100020003000 2000 3000 20 NULL 4000 ptr Search is successful.Address of the element is 3000 (ptr). Output: Search is unsuccessful.Address is NULL. Output:
32
Algorithm: Search_SL Question: Search for a particular node in the linked list.Value = 15 Steps: ptr = Header While(ptr->Link != NULL) EndWhile ptr = ptr->Link Process(ptr->Data) If(ptr->Data == 15) found = 1 location = ptr, found = 0, location = NULL If(found == 0), then Else print “Search unsuccessful.” print “Search successful.” EndIf Return(location) AND (found == 0), do Conditions
33
Algorithm: Search_SL Question: Search for a particular node in the linked list. NULL Header 10 15 4000 Value = 15 100020003000 2000 3000 20 NULL 4000 NULL Header 10 30 4000 100020003000 2000 3000 20 NULL 4000 ptr NULL Header 10 30 4000 100020003000 2000 3000 15 NULL 4000 ptr While(ptr->Link != NULL) AND (ptr->Data != 15) Condition:
34
Algorithm: Search_SL Question: Search for a particular node in the linked list.Value = 15 Steps: ptr = Header While(ptr->Link != NULL) AND (ptr->Data != 15) EndWhile ptr = ptr->Link If(ptr->Data == 15) Else EndIf Return(location) print “Search successful.” print “Search unsuccessful.” location = ptr location = NULL Stop
35
Algorithm: Search_SL Question: Search for a particular node in the linked list.Value = 15 Steps: ptr = Header While(ptr->Link != NULL) AND (ptr->Data != 15) EndWhile ptr = ptr->Link If(ptr->Data == 15) Else EndIf Return(location) print “Search successful.” print “Search unsuccessful.” location = ptr location = NULL Steps: ptr = Header, found = 0, location = NULL While(ptr->Link != NULL) AND (found == 0) EndWhile ptr = ptr->Link If(ptr->Data == 15) found = 1 location = ptr If(found == 0), then Else print “Search unsuccessful.” print “Search successful.” EndIf Return(location) KEY
36
Linked List Algorithm: –Search_SL Input: –Header: Pointer to the starting node of the linked list. –KEY: Data to be searched. Output: –Location: Pointer to a node where the KEY belongs or an error message. Data Structure: –Single Linked List whose address of starting node is known from Header.
37
Algorithm: Search_SL Steps: ptr = Header, found = 0, location = NULL While(ptr->Link != NULL) AND (found == 0) ptr = ptr->Link If(ptr->Data == KEY) found = 1 location = ptr EndIf EndWhile If(found == 0), then print “Search unsuccessful.” Else print “Search successful.” EndIf Return(location) Stop
38
Algorithm: InsertAny_SL Question: Insert a node in the linked list. NULL Header 35 25 4000 After node with value = 25.Value = 15 15 100020003000 new 6000 2000 3000 4000 6000 ptr 45 5000 4000 ptr1 NULL Header 35 20 4000 100020003000 2000 3000 45 NULL 4000 ptr 10 5000 NULL
39
Algorithm: InsertAny_SL Question: Insert a node in the linked list.After node with value = 25.Value = 15 Steps: ptr = Header While(ptr->Link != NULL) AND (ptr->Data != 25) EndWhile ptr = ptr->Link 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->Link new->Data = 15 new->Link = ptr1 ptr->Link = new EndIf KEY X
40
Trace Algorithm: InsertAny_SL NULL Header 35 45 4000 100020003000 2000 3000 25 NULL 4000 15 NULL 5000 new 5000 ptr1 NULL ptr = Header While(ptr->Link != NULL) AND (ptr->Data != KEY) EndWhile ptr = ptr->Link 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->Link new->Data = X new->Link = ptr1 ptr->Link = new EndIf Steps: Steps: (cntd) ptr KEY = 25X = 15
41
Linked List Algorithm: –InsertAny_SL 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 in a Single Linked List. Data Structure: –Single Linked List whose address of starting node is known from Header.
42
Algorithm: InsertAny_SL Steps: new = GetNode(NODE) If(new == NULL), then print “Memory Insufficient.” Else ptr = Header While(ptr->Link != NULL) AND (ptr->Data != KEY), do ptr = ptr->Link EndWhile If(ptr->Data != KEY), then print “KEY not found in the linked list.” Else ptr1 = ptr->Link new->Data = X new->Link = ptr1 ptr->Link = new EndIf
43
Algorithm: DeleteAny_SL Question: Delete a node from the linked list. X Node with value = 45 1000 2000 ptr ptr1 25 2000 3000 ptr ptr1 15 3000 4000 ptr ptr1 45 4000 5000 ptr 35 5000 6000 5 X 5000 ptr2 X Header 35 20 4000 100020003000 2000 3000 55 X 4000 ptr Header ptr1
44
Algorithm: DeleteAny_SL Question: Delete a node from the linked list.Node with value = 45 Steps: ptr = Header While(ptr->Link != NULL) AND (ptr->Data != 45), do ptr1 = ptr ptr = ptr->Link EndWhile If(ptr->Data == 45), then ptr2 = ptr->Link ptr1->Link = ptr2 ReturnNode(ptr) Else print “45 not found. Deletion not possible.” EndIf Stop KEY
45
Trace Algorithm: DeleteAny_SL KEY = 55 X Header 35 20 4000 100020003000 2000 3000 55 X 4000 ptr ptr1 ptr2 NULL Steps: ptr = Header While(ptr->Link != NULL) AND (ptr->Data != KEY), do ptr1 = ptr ptr = ptr->Link EndWhile If(ptr->Data == KEY), then ptr2 = ptr->Link ptr1->Link = ptr2 ReturnNode(ptr) Else print “KEY not found. Deletion not possible.” EndIf Stop Steps: (cntd) X
46
Linked List Algorithm: –DeleteAny_SL 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 from the linked list or message on failure. Data Structure: –Single Linked List whose address of starting node is known from Header.
47
Algorithm: DeleteAny_SL Steps: ptr = Header While(ptr->Link != NULL) AND (ptr->Data != KEY), do ptr1 = ptr ptr = ptr->Link EndWhile If(ptr->Data == KEY), then ptr2 = ptr->Link ptr1->Link = ptr2 ReturnNode(ptr) Else print “KEY not found. Deletion not possible.” EndIf Stop
48
Algorithm: Merge_SL Question: Join/Merge 2 linked lists into 1 single linked list. X Header1 35 25 4000 100020003000 2000 3000 55 X 4000 ptr X Header2 15 5 5000 800070006000 7000 6000 65 X 5000 ptr1 7000 Steps: ptr = Header1 While(ptr->Link != NULL), do EndWhile ptr = ptr->Link ptr1 = Header2->Link Steps: (cntd) ptr->Link = ptr1 Header = Header1 Stop Header ReturnNode(Header2)
49
Linked List Algorithm: –Merge_SL 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 starting node of the merged/joined/combined linked list. Data Structure: –Single Linked List whose address of starting node is known from Header.
50
Algorithm: Merge_SL Steps: ptr = Header1 While(ptr->Link != NULL), do ptr = ptr->Link EndWhile ptr1 = Header2->Link ptr->Link = ptr1 ReturnNode(Header2) Header = Header1 Stop
51
Algorithm: Split_SL Question: Split the linked list into 2 linked lists. X From node with value = 45 1000 2000 ptr 25 2000 3000 ptr 15 3000 4000 ptr 45 4000 5000 ptr 35 5000 6000 5 X X ptr1 X Header 35 20 4000 100020003000 2000 3000 55 X 4000 ptr Header X 7000 Header1 5000
52
Algorithm: Split_SL Question: Split the linked list into 2 linked lists. X From node with value = 45 1000 2000 ptr 25 2000 3000 ptr 15 3000 4000 ptr 45 4000 5000 ptr 35 5000 6000 5 X X ptr1 Header X 7000 Header1 5000 Steps: ptr = Header While(ptr->Link != NULL) AND (ptr->Data != 45), EndWhile ptr = ptr->Link Steps: (cntd) If(ptr->Data == 45) Header1 = GetNode(NODE) ptr1 = ptr->Link Header1->Data = NULL Header1->Link = ptr1 ptr->Link = NULL Else print “45 not found. Split not possible.” KEY
53
Linked List Algorithm: –Split_SL 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: –Single Linked List whose address of starting node is known from Header.
54
Algorithm: Split_SL Steps: ptr = Header While(ptr->Link != NULL) AND (ptr->Data != KEY), do ptr = ptr->Link EndWhile If(ptr->Data == KEY), then Header1 = GetNode(NODE) ptr1 = ptr->Link Header1->Data = NULL Header1->Link = ptr1 ptr->Link = NULL Else print “KEY not found. Split not possible.” EndIf Stop
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.