Linked List Merged

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 68

Linked

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

Type Linear Linear


Memory locations Consecutive Non-consecutive
Access to elements Random Sequential
Insertion and deletion Tedious as elements needs Can be done in constant
to be shifted time.
Memory allocation Static Dynamic
Types of linked list

Circular Linked List


Singly Linked List Doubly Linked List-
last node of the list points A doubly linked list is a list that has
A singly linked list as back to the first node (or two references, one to the next node
described above provides and another to previous node.
the head) of the list.
access to the list from the Doubly linked list also starts from
head node , but provide access both
head node. Traversal is ways. That is one can traverse
allowed only one way and forward or backward from any
there is no going back. node.
❑ Node structure in C
In C, we can implement a linked list using the following code:

typedef struct node


{
int data;
struct node *next;
}node;
node *Start=NULL;

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.

The computer maintains a list of all free memory cells.

This list of available space is called the free pool.

AVAIL is a pointer which points to free space.

Useful during insertion and deletion.


❑ Creating the first node
Memory must be allocated for one node and assigned to head
as follows.
node *Start=NULL;
Start = (node*) malloc(sizeof(node));
Start ->data = 10;
Start ->next = NULL;
Start

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.

02 Append a new node


(to the end) of a list
Linked list - 05
Deleting a node
from the list
Operations

Prepend a new node (to Inserting a new node


03 the beginning) of the 04 to a specific position
list on the list
❑ Traversing a singly linked list
Traversing a linked list means accessing the nodes of the list in order to
perform some processing on them.
Step 1: [INITIALIZE] SET PTR = START
Step 2: Repeat Steps 3 and 4 while PTR != NULL
Step 3: Apply Process to PTR-> DATA
Step 4: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 5: EXIT
❑ Traverse
struct node *display(struct node *start)
{
struct node *ptr;
ptr = start;
while(ptr != NULL)
{
printf(“\t %d”, ptr -> data);
ptr = ptr -> next;
}
return start;
}
10 2000 12 1200 19 3000 11 Null

Start 10 2000 12 1200 19 3000 11 Null

10 2000 12 1200 19 3000 11 Null

10 2000 12 1200 19 3000 11 Null


14
Number of Nodes

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.

Insert a new node

Case 04. The new node


is inserted before a given Case 03. :The new node
node. is inserted after a given
node.
❑ Inserting a Node at the Beginning of a Linked List
❑ Algorithm
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
Step 4: SET NEW_NODE-> NEXT = START
Step 5: SET START = NEW_NODE
Step 6: EXIT
❑ C Function to insert new node at the beginning
struct node *insert_beg(struct node *start)
{
struct node *new_node;
int num;
printf(“\n Enter the data : “);
scanf(“%d”, &num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
new_node -> next = start;
start = new_node;
return start;
}
❑ Inserting a Node at the End of a Linked List

10 2000 12 1200 19 3000 11 Null


Start
PTR=START PTR

10 2000 12 1200 19 3000 11 Null


Start
PTR = PTR->NEXT PTR

10 2000 12 1200 19 3000 11 Null


Start
PTR = PTR->NEXT
PTR

10 2000 12 1200 19 3000 11 22


Null
Start
PTR New node
17 Null

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

10 2000 12 1200 19 3000 11 Null


Start

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

• Write C function to insert a newnode after the given node.


Linked List

Delete a node
❑ Delete operation in linked list
❑ Learning Outcomes
1. Delete first node
2. Delete last node
3. Delete a node after a given node
4. Delete a node with a given value

• List types of linked list


• Explain node structures of singly
linked list
• Apply operations on single linked list
❑ Conditions to be checked
❑ Learning Outcomes
1. check for underflow condition
If (start==NULL)
2.After deleting the node free the
memory of the node deleted
• Explain node structures of singly
linked list
• Apply operations on single linked list
❑ Delete first node
Case 1- LL with more than one node
Start

1 2000 12 1200 10 3000 13 Null

Start=Start->next

Start

1 2000 12 1200 10 3000 13 Null

Start Case 2- LL with one node

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

1 2000 12 1200 10 3000 13 Null

PTR=Start
PREPTR= PTR (follows PTR)
Move PTR and PREPTR till PTR points to the last node

Start PREPTR PTR

1 2000 12 1200 10 3000 13 Null

Set next part of PREPTR to NULL

Start

1 2000 12 1200 10 NULL


❑ Delete last node algorithm
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = START
Step 3: Repeat Steps 4 and 5 while PTR->NEXT != NULL
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 6: SET PREPTR -> NEXT = NULL
Step 7: FREE PTR
Step 8: EXIT
❑ Delete a node after a given node in the linked list

Start Delete node after 10

1 2000 12 1200 10 3000 13 4000 19 Null

PTR=Start ->next
PREPTR= START (follows PTR)
Move PTR and PREPTR till PREPTR points to the node with the given value 10.

Start PREPTR PTR

1 2000 12 1200 10 3000 13 4000 19 Null

Set ____________

Start

1 2000 12 1200 10 4000 19 Null


❑ Delete a node after the given node algorithm
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 10
[END OF IF]
Step 2: SET PTR = START->NEXT
Step 3: SET PREPTR = PTR
Step 4: Repeat Steps 5 and 6 while PREPTR->DATA != NUM
Step 5: SET PREPTR = PTR
Step 6: SET PTR = PTR-> NEXT
[END OF LOOP]
Step 7: SET TEMP = PTR
Step 8: SET PREPTR->NEXT = PTR->NEXT
Step 9: FREE TEMP
Step 10 : EXIT
Double
Linked
List
❑ Memory Representation

Draw the doubly linked list in the form given below-

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)

1. Create newnode and initialize


10
New node
2. Create links to insert newnode at the beginning

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)

1. Create newnode and initialize


10
New node
2. Move PTR from from node till it points to last node

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

void traverse (struct node *start)


{ struct node *ptr;
ptr=start;
while (ptr->next != start)
{
printf (“ %d”, ptr->data);
ptr=ptr->next;
}
printf (“ %d”, ptr->data);
}
OBJECTS :
Objects are instances
of classes and are
used to interact
amongst each other to
create applications.

Insert
operation
❑ Insert new node at the beginning
Start=1000

1 2000 12 1200 10 3000 13 1000

Insertbeg (10)

Start=1000
Newnode= 5000
10 Null 1 2000 12 1200 10 3000 13 1000

Start=?

10 ? 1 2000 12 1200 10 3000 13 ?


❑ Algorithm to insert a new node
Inserting At Beginning of the list
We can use the following steps to insert a new node at beginning of the circular
linked list...
Step 1 - Create a newNode with given value.
Step 2 - Check whether list is Empty (start == NULL)
Step 3 - If it is Empty then, set start = newNode and newNode→next =
start .
Step 4 - If it is Not Empty then, define a Node pointer 'ptr' and initialize with
'start'.
Step 5 - Keep moving the 'ptr' to its next node until it reaches to the last
node (until 'ptr → next == start').
Step 6 - Set 'newNode → next =start', 'start = newNode' and 'ptr → next =
start'.
❑ Insert new node at the end

Start

1 2000 12 1200 10 3000 13 2000

InsertEnd (10)
1. Create a newnode 2. PTR=Start
Start PTR
Newnode
1 2000 12 1200 10 3000 13 2000 10 Null

Start 3. Traverse till PTR points to last node PTR Newnode


1 2000 12 1200 10 3000 13 2000 10 Null

Start 4. Create new links PTR Newnode


1 2000 12 1200 10 3000 13 10 Null
❑ Algorithm to insert at the end
We can use the following steps to insert a new node at end of the
circular linked list...
Step 1 - Create a newNode with given value.
Step 2 - Check whether list is Empty (start == NULL).
Step 3 - If it is Empty then, set start = newNode and newNode →
next = start.
Step 4 - If it is Not Empty then, define a node pointer ptr and
initialize with start.
Step 5 - Keep moving the ptr to its next node until it reaches to the
last node in the list (until ptr → next == start).
Step 6 - Set ptr → next = newNode and newNode → next = start.
❑ Delete first node
❑ Algorithm to delete first node
We can use the following steps to delete a node from beginning of the circular
linked list...
Step 1 - Check whether list is Empty (start == NULL)
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible'
and terminate the function.
Step 3 - If it is Not Empty then, define two Node pointers 'ptr' and 'preptr' and
initialize both 'ptr' and 'preptr' with start.
Step 4 - Check whether list is having only one node (ptr → next == start)
Step 5 - If it is TRUE then set start = NULL and delete ptr (Setting Empty list
conditions)
Step 6 - If it is FALSE move the ptr until it reaches to the last node. (until ptr →
next == start )
Step 7 - Then set start = preptr → next, ptr → next = start and delete preptr.
❑Delete last node
❑ Algorithm to delete last node
We can use the following steps to delete a node from end of the circular linked list...
Step 1 - Check whether list is Empty (start == NULL)
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and
terminate the function.
Step 3 - If it is Not Empty then, define two Node pointers 'ptr' and 'preptr' and
initialize 'ptr' with start.
Step 4 - Check whether list has only one Node (ptr → next == start)
Step 5 - If it is TRUE. Then, set start = NULL and delete ptr. And terminate from
the function. (Setting Empty list condition)
Step 6 - If it is FALSE. Then, set 'preptr = ptr ' and move ptr to its next node.
Repeat the same until ptr reaches to the last node in the list. (until ptr → next ==
start)
Step 7 - Set preptr → next = start and delete ptr.
Thank you

https://www.youtube.com/@v2vedtechllp

You might also like