1 data structures. 2 linked list 3 pros dynamic in nature, so grow and shrink in size during...
TRANSCRIPT
![Page 1: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/1.jpg)
1
DATA STRUCTURE
S
![Page 2: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/2.jpg)
2
LINKED LIST
![Page 3: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/3.jpg)
3
PROS Dynamic in nature, so grow and shrink in size during
execution
Efficient memory utilization
Insertion can be done at specified location
Many complex applications can be carried out using Linked List
CONS Occupy more memory
Random access of data is some what cumbersome & Time consuming
WHY & WHY NOT ?
![Page 4: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/4.jpg)
4
A linked list is a linear collection of specially designed data elements, called NODE, linked to one another by means of Pointer.
Each NODE is divided In two parts, first part contains the information and second part contains the address of the next node.
Need a head to point to the first node of the list. Otherwise we won’t know where the start of the list is.
The next field in the last node points to nothing. We will place the memory address NULL
Struct Node
{
};
TERMINOLOGY
int DATAStruct node *Next
DATA
Next
![Page 5: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/5.jpg)
5
1051
1052
1055
1059
1060
1061
1062
1063
1064
1056
1057
1058
1053
1054 2
6
8
7
1
1051
1063
1057
1060
0
head 1054
1063
2 6 8 7 1
head
1065
ACTUAL PICTURE IN MEMORY
![Page 6: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/6.jpg)
6
add(9): Create a new node in memory to hold ‘9’
Node* newNode = new Node(9);
Link the new node into the list
9newNode
2 6 8 7 1
head
current
size=5 6
9
newNode
1
3
2
OPERATION
![Page 7: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/7.jpg)
7
2 6 8 7 1
headSINGLY
LINKED LIST
2 6 8 1head
DOUBLY LINKED LIST
2 6 8 7 1head
current
CIRCULAR LINKED LIST
TYPES
![Page 8: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/8.jpg)
8
SINGLY LINKED LIST
![Page 9: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/9.jpg)
9
headNode size=0List list;
2headNode
currentNode
size=1
lastcurrentNode
list.add(2);
2 6headNode
currentNode
size=2
lastcurrentNode
list.add(6);
list.add(8); list.add(7); list.add(1);
2 6 7 1headNode
currentNode
size=5
lastcurrentNode
8
![Page 10: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/10.jpg)
10
30START
Create Node With DATA (30)
30START
Create Node With DATA (40) at END
40
10START
Create Node With DATA (10) at BEGINING
30 40
10START
Create Node With DATA (20) at SECOND Position
20 4030
10START
Create Node With DATA (20) at LAST Position
20 5030 40
![Page 11: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/11.jpg)
11
Insert node at BEGINING
1. Input DATA to be inserted
2. Create a NewNode
3. NewNode -> DATA = DATA
4. If (START == NULL)
NewNode -> Next = NULL
else
NewNode -> Next = START
5. START = NewNode
6. Exit
ALGORITHM
![Page 12: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/12.jpg)
12
1. Input DATA to be inserted
2. Create a NewNode
3. NewNode - > DATA = DATA
4. NewNode -> Next = NULL
4. If (START == NULL)
START = NewNode
else
TEMP = START
while (TEMP -> NEXT != NULL)
TEMP = TEMP -> NEXT
5. TEMP -> NEXT = NewNode
6. Exit
ALGORITHM Insert node at END
![Page 13: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/13.jpg)
13
1. Input DATA & POS to be inserted
2. Initialize TEMP = START & K =0
3. Repeat step 3 while K < POS
(a) TEMP = TEMP -> NEXT
(b) if (TEMP == NULL)
“Node Is Not In The List”
Exit
(c) K = K + 1
4. Create a New Node
5. NewNode -> DATA = DATA
6. NewNode -> Next = TEMP -> NEXT
7. TEMP -> Next = NewNode
8. EXIT.
ALGORITHM Insert node at SPECIFIED LOCATION
![Page 14: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/14.jpg)
14
1. Input DATA to be deleted
2. If (START-> DATA = = DATA)(a) TEMP = START(b) START = START -> Next(c) free (TEMP)(d) Exit
3. HOLD = START
4. While (HOLD -> Next -> Next != NULL)(a) if (HOLD -> Next ->DATA == DATA)
(i) TEMP = HOLD -> Next(ii) HOLD -> Next = TEMP ->
Next(iii) free (TEMP)(iv) Exit.
(b) HOLD = HOLD -> Next
5. if (HOLD -> Next -> DATA == DATA )(a) TEMP = HOLD -> Next(b) free (TEMP)(c) HOLD -> Next = NULL(d) Exit.
6. “DATA not Found”
7. Exit.
1st Location
Ith Location
Last Location
ALGORITHM DELETE NODE
![Page 15: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/15.jpg)
15
1. Input DATA to be searched
2. Initialize TEMP = START & POS = 1
3. Repeat Steps 4, 5 & 6 until TEMP = NULL
4. If (TEMP -> DATA == DATA) (a) “DATA found at POS” (b) Exit.
5. TEMP = TEMP -> Next
6. POS = POS + 1
7. If (TEMP = NULL) “DATA Not Found in LIST”
8. Exit.
ALGORITHM SEARCHING NODE
![Page 16: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/16.jpg)
16
1. If (START == NULL)
(a) “LIST is Empty”
(b) Exit.
2. Initialize TEMP =START
3. Repeat Step 4 and 5 Until TEMP = NULL
4. Display “TEMP -> DATA”
5. TEMP = TEMP -> Next
6. Exit.
ALGORITHM DISPLAY NODES
![Page 17: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/17.jpg)
17
10 NULL Push (10)
TOP
20 Push (20)
10 NULLTOP
30 Push (30)
20TOP 10 NULL
20 Pop()
10 NULLTOP
40 Push (40)
20TOP 10 NULL
STACK – Using LINKED LIST
![Page 18: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/18.jpg)
18
PUSH
1. Input DATA to be pushed
2. Create a New Node
3. NewNode -> DATA = DATA
4. NewNode -> Next = TOP
5. TOP = NewNode
6. Exit.
ALGORITHM
![Page 19: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/19.jpg)
19
ALGORITHM POP
1. If ( TOP == NULL )
“STACK Is Empty”
2. Else
(a). TEMP = TOP
(b). “Poped Data : TOP -> DATA”
(c). TOP = TOP -> Next
(d). TEMP -> Next = NULL
(e). Free (TEMP)
3. Exit.
![Page 20: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/20.jpg)
20
10 NULL Push (10)
Front
10 Push (20)
20 NULL
10 Push (30)
20 30 NULL
20 Pop()
30 NULL
20 Push (40)
30 40 NULL
Rear
Front Rear
Front
Front
Front
Rear
Rear
Rear
NULL
QUEUE – Using LINKED LIST
![Page 21: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/21.jpg)
21
PUSH
1. Input DATA to be pushed
2. Create a New Node
3. NewNode -> DATA = DATA
4. NewNode -> Next = NULL
5. If ( REAR != NULL )
REAR -> Next = NewNode
6. REAR = NewNode
7. Exit.
ALGORITHM
![Page 22: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/22.jpg)
22
ALGORITHM POP
1. If ( FRONT == NULL || FRONT > REAR )
“QUEUE Is Empty”
2. Else
(a). “Popped Data : FRONT -> DATA”
(b). If (FRONT != REAR )
FRONT = FRONT -> Next
Else
FRONT = NULL
3. Exit.
![Page 23: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/23.jpg)
23
DOUBLY LINKED LIST
![Page 24: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/24.jpg)
24
Moving forward in a singly-linked list is easy; moving backwards is not so easy.
To move back one node, we have to start at the head of the singly-linked list and move forward until the node before the current.
To avoid this we can use two pointers in a node: one to point to next node and another to point to the previous node
Struct node
{
}
TERMINOLOGY
int DATA;
Struct node *next;Struct node *prev;
DATA
next
prev
![Page 25: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/25.jpg)
25
Need to be more careful when adding or removing a node.
size=52 6 8 7 1head
current
Insert Node
![Page 26: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/26.jpg)
26
1. NewNode->Next = current->Next
size=52 6 8 7head
current
1
9newNode 1
Insert Node
![Page 27: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/27.jpg)
27
size=52 6 8 7head
current
1
9newNode 1
2
2. NewNode->prev = current
Insert Node
![Page 28: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/28.jpg)
28
size=52 6 8 7head
current
1
9newNode 1
2 3
3. current->next->prev = NewNode
Insert Node
![Page 29: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/29.jpg)
29
size=52 6 8 7head
current
1
9newNode 1
2 34
4. current->next = NewNode
Insert Node
![Page 30: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/30.jpg)
30
Size=62 6 8 7head
current
1
9newNode 1
2 34
5. current = NewNode
Insert Node
![Page 31: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/31.jpg)
31
1. Input DATA & POS
2. Initialize TEMP = START & i=1
3. while ( i < POS ) & (TEMP != NULL)
TEMP = TEMP -> Next
4. If (TEMP != NULL) & I = POS
a). Create a NewNode
b). NewNode -> DATA = DATA
c). NewNode -> next = TEMP -> next
d). NewNode -> prev = TEMP
e). TEMP -> next - > prev = NewNode
f). TEMP - > next = NewNode
5. Else
“Position Not Found”
6. Exit
INSERT NODE
![Page 32: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/32.jpg)
32
1. Input DATA to be deleted
2. Initialize TEMP = START
3. while (TEMP -> next -> DATA != DATA)
TEMP = TEMP -> next
5. HEAD = TEMP
TEMP = TEMP -> next
6. HEAD -> next = TEMP -> next
7. TEMP -> next -> prev = HEAD
8. Exit.
DELETE NODE
![Page 33: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/33.jpg)
33
CIRCULAR LINKED LIST
![Page 34: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/34.jpg)
34
The next field in the last node in a singly-linked list is set to NULL.
Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node.
A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list.
TERMINOLOGY
![Page 35: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/35.jpg)
35
2 6 8 7 1head
current
size=5
2
8
7
1
head
current
size=5
6
View of CIRCULAR QUEUE
![Page 36: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/36.jpg)
36
2 6 8 7 1start size=5
Traversing a CIRCULAR QUEUE
Display(Start)
1. If Start = NULL
Print “List is empty!!”
End If
2. Set TEMP = Start
3. Do
Print TEMP -> DATA
Set TEMP = TEMP -> next
While TEMP != Start
4. Exit.
![Page 37: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/37.jpg)
37
2 6 8 7 1
start
Insertion in the Begining
Insert_beg(Start)
1. If Start = NULL
Set Start=nptr
Set Start -> next = Start
Else
Set TEMP = Start
While TEMP ->next !=Start
Set TEMP = TEMP -> next
End While
Set nptr -> next = Start
Set Start = nptr
Set TEMP -> next = Start
End If
2. Exit.
nptr
4
nptr
4start
(a)
(b)
![Page 38: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/38.jpg)
38
2 6 8 7 1
start
Insertion at the End
Insert_beg(Start)
1. If Start = NULL
Set Start=nptr
Set Start -> next = Start
Else
Set TEMP = Start
While TEMP ->next !=Start
Set TEMP = TEMP -> next
End While
Set TEMP -> next = nptr
Set nptr -> next = Start
End If
2. Exit.
nptr
(b)
4
temp
![Page 39: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/39.jpg)
39
2 6 8 7 1
start
Deletion from Beginingdelete_beg(Start)
1. If Start = NULL
Print “Underflow: List is empty! “
End If
2. Set TEMP = Start
3. Set ptr = TEMP
4. While ptr ->next !=Start
Set ptr = ptr -> next
End While
5. Set Start = Start -> next
6. Set ptr -> next = Start
7. Deallocate TEMP
8. Exit.(b)
ptrtemp
![Page 40: 1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be](https://reader035.vdocuments.us/reader035/viewer/2022081602/5518d26c550346991f8b5d77/html5/thumbnails/40.jpg)
40
2 6 8 7 1
start
Deletion from the Enddelete_end(Start)
1. If Start = NULL
Print “Underflow: List is empty! “
End If
2. Set TEMP = Start
3. If temp -> next = start
Set Start = NULL
Else
While TEMP ->next !=Start
Set save = TEMP
Set TEMP = TEMP -> next
End While
Set save -> next = Start
End If
4. Deallocate TEMP
5. Exit.(b)
tempsave