Professional Documents
Culture Documents
Linked List Merged
Linked List Merged
Linked List Merged
List –
Insert a
Node
❑ Linked list
◈ Introduction
◈ Representation of Linked List
◈ Linked List v/s Array
◈ Types of Linked List –
◈ Singly Linked List, Circular
Linked List, Double Linked List,
Operations on Singly Linked List
and Double Linked List.
❑ Learning
Outcomes
To be able to
• Explain linked list and
representation of data in memory
• List types of linked list
• Explain node structures of singly
linked list
• Apply operations on single linked
list
Linked list in memory
❑ Linked Lists versus Arrays
Both arrays and linked lists are a linear collection of data elements.
But unlike an array, a linked list does not store its nodes in consecutive memory locations.
Another point of difference between an array and a linked list is that a linked list does not
allow random access of data. Nodes in a linked list can be accessed only in a sequential
manner.
Unlike an array, insertions and deletions can be done at any point in the list in a constant
time.
Array Linked List
Linked lists contain a pointer variable START that stores the address of the first node
in the list.
❑ “malloc” or “memory allocation”
The “malloc” or “memory allocation” method in C is used to dynamically
allocate a single large block of memory with the specified size.
10 NULL
❑ Adding the second node and linking
node* nextnode = (node *) malloc(sizeof(node));
nextnode->data = 12;
nextnode->next = NULL;
Start->next = nextnode;
Start
10 2000
12 NULL
Memory location
2000
01 Traversing a Updating a
node in the list
06
linked list.
https://www.youtube.com/@v2vedtechllp
❑ Algorithm to print the number of nodes in a linked list
❑ Searching for a Value in a Linked List
Case 01: The new node Case 02: The new node
is inserted at the is inserted at the end.
beginning.
22
❑ Example
❑ Inserting a Node at the End of a Linked List
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET NEW_NODE->DATA = VAL
SET NEW_NODE->NEXT = NULL
Step 4: PTR=START
Step 5: Repeat Step 6 while PTR->NEXT != NULL
Step 6: SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: SET PTR->NEXT = NEW_NODE
Step 6: EXIT
❑ C Function to insert new node at the end
struct node *insert_end(struct node *start)
{
struct node *ptr, *new_node;
int num;
printf(“\n Enter the data : “);
scanf(“%d”, &num);
________
new_node -> data = num;
new_node -> next = NULL;
__________
while(ptr -> next != NULL)
A. ptr = start;
__________ B. new_node =(struct node *)
ptr -> next = new_node; malloc(sizeof(struct node));
C. ptr = ptr -> next;
return start;
}
❑ Inserting a Node before a given node
PTR
❑ Example
Move PTR and PREPTR until the DATA part of PTR = value
❑ Inserting a Node before a given node
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 2: SET NEW_NODE = Allot memory
Step 3: SET NEW_NODE->DATA = VAL
SET NEW_NODE->NEXT = NULL
Step 4: SET PTR=START
SET PREPTR = PTR
Step 5: Repeat Step 6 & 7 while PTR->DATA != NUM
Step 6: SET PREPTR=PTR
SET PTR = PTR->NEXT
[END OF LOOP]
Step 5: SET PREPTR->NEXT = NEW_NODE
SET NEW_NODE->NEXT=PTR
Step 6: EXIT
❑ Activity
• If a newnode has to be inserted after 10 then PTR should stop at which node
(Assume PTR=START)
Start 1 2000 12 1200 10 3000 13 Null
PTR
19 NULL
NEWNODE
Start=Start->next
Start
1 Null
❑ Delete first node algorithm
Step 1: IF START == NULL
Write UNDERFLOW
Go to Step 5
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START-> NEXT
Step 4: FREE PTR
Step 5: EXIT
❑ Delete the last node from the linked list
Start
PTR=Start
PREPTR= PTR (follows PTR)
Move PTR and PREPTR till PTR points to the last node
Start
PTR=Start ->next
PREPTR= START (follows PTR)
Move PTR and PREPTR till PREPTR points to the node with the given value 10.
Set ____________
Start
Start …
.
Insert a new node in a double linked list
Case 2
Case 1 Case 3
The new node is inserted
The new node is inserted at at the end. The new node is inserted
the beginning. in between a given node.
❑ Insert a new node at the beginning
4 9 7 8
Start Insert Beg(10)
10 4 9 7 8
New node Start
3. Move Start to point to newnode
10 4 9 7 8
Start
❑ Algorithm – Insert Beg
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 9
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL-> NEXT
Step 4: SET NEW_NODE ->DATA = VAL
Step 5: SET NEW_NODE ->PREV = NULL
Step 6: SET NEW_NODE ->NEXT = START
Step 7: SET START -> PREV =NEW_NODE
Step 8: SET START =NEW_NODE
Step 9: EXIT
❑ Insert a new node at the end
4 9 7 8
Start Insert End(10)
4 9 7 8
Start PTR
3. Create links to insert newnode at the beginning
4 9 7 8 10
Start PTR New node
❑ Algorithm – Insert End
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 9
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL-> NEXT
Step 4: SET NEW_NODE ->DATA = VAL
Step 5: SET NEW_NODE ->PREV = NULL
Step 6: SET NEW_NODE ->NEXT = NULL
Step 7: SET PTR = START
Step 7:Repeat Step 8 while PTR -> NEXT != NULL
Step 8: SET PTR = PTR NEXT
[END OF LOOP]
Step 9: Set ______________
Step 10: Set _____________
Step 11: EXIT
❑ Insert a newnode after a given node
4 9 7 8
Start Insertafter(9, 10)
1. Create newnode and initialize
10
newnode
2. Move PTR from from node till it points to node with data 9
4 9 7 8
Start PTR
3. Create links to insert newnode at the beginning
4 9 10 7 8
Start PTR newnode
❑ Algorithm - InsertAfter
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 9
[END OF IF]
Step 2: SET NEW_NODE = AVAIL
Step 3: SET AVAIL = AVAIL-> NEXT
Step 4: SET NEW_NODE ->DATA = VAL
Step 5: SET NEW_NODE ->PREV = NULL
Step 6: SET NEW_NODE ->NEXT = NULL
Step 7: SET PTR = START
Step 7:Repeat Step 8 while PTR -> DATA != NUM
Step 8: SET PTR = PTR NEXT
[END OF LOOP]
Step 9: Set NEW_NODE ->NEXT = PTR-> NEXT
Step 10: Set NEW_NODE ->PREV = ____
Step 11: Set PTR->NEXT->PREV= ______
Step 12: Set PTR->NEXT=_______
Step 11: EXIT
Deleting a node
Case 2
Case 1 Case 3
Delete last
Delete first Delete node
node
node in between
❑ Delete First Node
4 9 7 8
Start deleteBeg()
1. Move start to next node, Start=Start->next
4 9 7 8
Start
2. Update Start->prev=NULL
4 9 7 8
Start
3. Free the memory
9 7 8
Start
❑ Algorithm to delete the first node
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 6
[END OF IF]
Step 2: SET PTR = START
Step 3: SET START = START-> NEXT
Step 4: SET START-> PREV = NULL
Step 5: FREE PTR
Step 6: EXIT
❑ Delete last Node
4 9 7 8
Start PTR deleteEnd()
1. Move PTR to next node
4 9 7 8
Start PTR
2. Update PTR->prev -> next=NULL
4 9 7 8
Start
3. Free the memory
4 9 7
Start
❑ Algorithm to delete the last node
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 6
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR -> NEXT != NULL
PTR = PTR-> NEXT
[END OF LOOP]
Step 4: SET PTR-> PREV->NEXT = NULL
Step 5: FREE PTR
Step 6: EXIT
❑ Delete a node with the given value
4 9 7 8
Start PTR delete()
1. Move PTR to the node with the given value
4 9 7 8
Start PTR
2. Update PTR->prev -> next=____ PTR->next->prev=_____
4 9 7 8
Start PTR
3. Free the memory
4 9 8
Start
❑ Algorithm to delete the node with the given value
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 6
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Step 4 while PTR -> NEXT != VAL
PTR = PTR-> NEXT
[END OF LOOP]
Step 4: SET PTR-> PREV->NEXT = PTR->NEXT
SET PTR-> NEXT->PREV = PTR->PREV
Step 5: FREE PTR
Step 6: EXIT
Circular
Linked
List
❑ Circular linked list
The last node contains a pointer to the first node of the list.
Memory representation
Draw the circular linked list representation from
the memory representation given.
❑ Traverse operation
Insert
operation
❑ Insert new node at the beginning
Start=1000
Insertbeg (10)
Start=1000
Newnode= 5000
10 Null 1 2000 12 1200 10 3000 13 1000
Start=?
Start
InsertEnd (10)
1. Create a newnode 2. PTR=Start
Start PTR
Newnode
1 2000 12 1200 10 3000 13 2000 10 Null
https://www.youtube.com/@v2vedtechllp