0% found this document useful (0 votes)
407 views24 pages

C Program To Insert Node at The Beginning of Singl

The document discusses algorithms and steps for performing various operations on singly linked lists and doubly linked lists in C. Specifically, it provides: 1) Algorithms and steps to insert a node at the beginning, end, and middle of a singly linked list. 2) Algorithms to delete the first, last, and middle nodes of a singly linked list. 3) Algorithms to insert a node at the beginning and end of a doubly linked list. The algorithms traverse the lists using next and previous pointers to appropriately link the new nodes into the correct positions.

Uploaded by

Parag Fadnis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
407 views24 pages

C Program To Insert Node at The Beginning of Singl

The document discusses algorithms and steps for performing various operations on singly linked lists and doubly linked lists in C. Specifically, it provides: 1) Algorithms and steps to insert a node at the beginning, end, and middle of a singly linked list. 2) Algorithms to delete the first, last, and middle nodes of a singly linked list. 3) Algorithms to insert a node at the beginning and end of a doubly linked list. The algorithms traverse the lists using next and previous pointers to appropriately link the new nodes into the correct positions.

Uploaded by

Parag Fadnis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

C program to insert node at the

beginning of Singly Linked List


Write a C program to create a singly linked list of n nodes and insert a node in the
beginning of the singly linked list. How to insert a node in the beginning of the singly
linked list. Algorithm to insert a node at the beginning of Singly linked list. Steps to
insert a new node at the start of a singly linked list.

Algorithm to insert node at the beginning of


singly linked list
Algorithm to insert node at the beginning of Singly Linked ListBeing:

createSinglyLinkedList (head)

alloc (newNode)

If (newNode == NULL) then

write ('Unable to allocate memory')

End if

Else then

read (data)wo

newNode.data ← data
newNode.next ← head
head ← newNode
End elseEnd

Steps to insert node at the beginning of singly


linked list
1. Create a new node, say newNode points to the newly created node.

2. Link the newly created node with the head node, i.e. the newNode will now point to head
node.

3. Make the new node as the head node, i.e. now head node will point to newNode.

C program to insert node at the end


of Singly Linked List
Write a C program to create a list of n nodes and insert a new node at the end of the
Singly Linked List. How to insert a new node at the end of a Singly Linked List in C.
Algorithm to insert node at the end of singly linked list. Steps to insert a new node at
the end of singly linked list.
Algorithm to insert node at the end of Singly
linked list
Algorithm to insert node at the end of a Singly Linked ListBegin:

createSinglyLinkedList (head)

alloc (newNode)

If (newNode == NULL) then

write ('Unable to allocate memory')

End if

Else then

read (data)

newNode.data ← data
newNode.next ← NULL
temp ← head
While (temp.next != NULL) do

temp ← temp.next
End while

temp.next ← newNode
End elseEnd

Steps to insert node at the end of Singly linked


list
1. Create a new node and make sure that the address part of the new node points to
NULL i.e. newNode->next=NULL
2. Traverse to the last node of the linked list and connect the last node of the list with
the new node, i.e. last node will now point to new node. (lastNode->next =
newNode).

C program to insert node at the


middle of Singly Linked List
Write a C program to create a singly linked list of n nodes and insert a new node at
the middle of the linked list at nth position. How to insert new node at the middle of a
singly linked list in C. Algorithm to insert a new node in middle of a singly linked list.
Steps to insert new node at any position in singly linked list.

Algorithm to insert node at the middle of singly


Linked List
Algorithm to insert node at the middle of Singly Linked List%% Input :
n position to insert data in the listBegin:
createSinglyLinkedList (head)

alloc (newNode)

If (newNode == NULL) then


write ('Unable to allocate memory.')

End if

Else then

read (data)

newNode.data ← data
temp ← head
For i ← 2 to n-1

temp ← temp.next
If (temp == NULL) then

break

End if

End for

If (temp != NULL) then

newNode.next ← temp.next
temp.next ← newNode
End if

End elseEnd

Steps to insert node at the middle of Singly


Linked List
1. Create a new node.
2. Traverse to the n-1th position of the linked list and connect the new node with the
n+1th node. Means the new node should also point to the same node that the n-1th
node is pointing to. (newNode->next = temp->next where temp is the n-1th node).

3. Now at last connect the n-1th node with the new node i.e. the n-1th node will now
point to new node. (temp->next = newNode where temp is the n-1th node).

C program to delete first node of


Singly Linked List
August 9, 2017Data StructuresC, Data Structures, Linked List, Program,
Singly Linked ListPankaj Prakash

Write a C program to create a singly linked list of n nodes and delete the first node or
beginning node of the linked list. How to delete first node or beginning node from the
singly linked list in C. Algorithm to delete first node from singly linked list. Steps to
delete first node from singly linked list.
Algorithm to delete first node of Singly Linked
List
Algorithm to delete first node of Singly Linked List%%Input: head of
the linked listBegin:

If (head != NULL) then

toDelete ← head
head ← head.next
unalloc (toDelete)

End ifEnd

Steps to delete first node of Singly Linked List


1. Copy the address of first node i.e. head node to some temp variable say toDelete.

2. Move the head to the second node of the linked list i.e. head = head->next.

3. Disconnect the connection of first node to second node.

4. Free the memory occupied by the first node.


C program to delete last node of
Singly Linked List
August 8, 2017Data StructuresC, Data Structures, Linked List, Program,
Singly Linked ListPankaj Prakash

Write a C program to create a singly linked list of n nodes and delete the last node of
the list. How to delete last node of a singly linked list in C. Algorithm to delete last
node from a singly linked list. Steps to remove last node from singly linked list.

Algorithm to delete last node of Singly Linked


List
Algorithm to delete last node of Singly Linked List%%Input : head node
of the linked listBegin:

If (head == NULL) then

write ('List is already empty')

End if

Else then

toDelete ← head
secondLastNode ← head
While (toDelete.next != NULL) do

secondLastNode ← toDelete
toDelete ← toDelete.next
End while

If (toDelete == head) then

head ← NULL
End if

Else then

secondLastNode.next ← NULL
End else
unalloc (toDelete)

End elseEnd

Steps to delete last node of a Singly Linked List


1. Traverse to the last node of the linked list keeping track of the second last node in some
temp variable say secondLastNode.

2. 2If the last node is the head node then make the head node as NULL else disconnect the second
last node with the last node i.e. secondLastNode->next = NULL.

3. Free the memory occupied by the last node.

C program to delete middle node of


Singly Linked List
August 8, 2017Data StructuresC, Data Structures, Linked List, Program,
Singly Linked ListPankaj Prakash

Write a C program to create a singly linked list of n nodes and delete node from the
middle of the linked list. How to delete node from the middle of the singly linked list
in C. Algorithm to delete middle node from singly linked list. Steps to delete middle
node from singly linked list.

Algorithm to delete middle node of Singly Linked


List
Algorithm to delete middle node of Singly Linked List%%Input : head node
of the linked list

n node to be deletedBegin:
If (head == NULL) then

write ('List is already empty')

End if

Else then

toDelete ← head
prevNode ← head
For i←2 to n do

prevNode ← toDelete
toDelete ← toDelete.next
If (toDelete == NULL) then

break

End if

End for

If (toDelete != NULL) then

If (toDelete == head) then

head ← head.next
End if

prevNode.next ← toDelete.next
toDelete.next ← NULL
unalloc (toDelete)

End if

End elseEnd

Steps to delete middle node of Singly Linked List


1. Traverse to the nth node of the singly linked list and also keep reference of n-1th
node in some temp variable say prevnode.
2. Reconnect the n-1th node with the n+1th node i.e. prevNode->next = toDelete->next
(Where prevNode is n-1th node and toDelete node is the nth node and
toDelete->next is the n+1th node).

3. Free the memory occupied by the nth node i.e. toDelete node.

C program to insert node in a Doubly


linked list
August 8, 2017Data StructuresC, Data Structures, Doubly Linked List,
Linked List, ProgramPankaj Prakash

Write a C program to create a doubly linked list and insert a new node in beginning,
end or at any position in the list. How to insert a new node at beginning of a Doubly
linked list. How to insert a new node at the end of a doubly linked list. How to insert a
new node at any position of a doubly linked list in C. Algorithm to insert node in a
doubly linked list.
Algorithm to insert node at the beginning of a
Doubly linked list
Algorithm to insert a node at the beginning of a Doubly linked list%%
Input : head {A pointer pointing to the first node of the list}Begin:

alloc (newNode)

If (newNode == NULL) then

write ('Unable to allocate memory')

End if

Else then

read (data)

newNode.data ← data;
newNode.prev ← NULL;
newNode.next ← head;

head.prev ← newNode;
head ← newNode;
write('Node added successfully at the beginning of List')

End elseEnd

Algorithm to insert node at the end of a Doubly


linked list
Algorithm to insert a node at the end of Doubly linked list%% Input :
last {Pointer to the last node of doubly linked list}Begin:
alloc (newNode)

If (newNode == NULL) then

write ('Unable to allocate memory')

End if

Else then

read (data)

newNode.data ← data;
newNode.next ← NULL;
newNode.prev ← last;

last.next ← newNode;
last ← newNode;
write ('Node added successfully at the end of List')

End elseEnd

Algorithm to insert node at any position of a


Doubly linked list
Algorithm to insert node at any position of doubly linked list%% Input :
head {Pointer to the first node of doubly linked list}
: last {Pointer to the last node of doubly linked list}

: N {Position where node is to be inserted}Begin:

temp ← head
For i←1 to N-1 do

If (temp == NULL) then

break

End if

temp ← temp.next;
End for

If (N == 1) then

insertAtBeginning()

End if

Else If (temp == last) then

insertAtEnd()

End if

Else If (temp != NULL) then

alloc (newNode)

read (data)

newNode.data ← data;
newNode.next ← temp.next
newNode.prev ← temp
If (temp.next != NULL) then

temp.next.prev ← newNode;
End if

temp.next ← newNode;
write('Node added successfully')

End ifEnd

Steps to insert a new node in Doubly linked list


We have supposed that we want to insert a node at 3rd position.

1. Traverse to N-1 node in the list. Where N is the position to insert. Say temp now
points to N-1th node.
2. Create a newNode that is to be inserted and assign some data to its data field.

3. Connect the next address field of newNode with the node pointed by next address
field of temp node.

4. Connect the previous address field of newNode with the temp node.
5. Check if temp->next is not NULL then, connect the previous address field of node
pointed by temp.next to newNode.

6. Connect the next address field of temp node to newNode i.e. temp->next will now
point to newNode.

7. Final list
C program to delete a node from
doubly linked list
Write a C program to create a doubly linked list and delete a node from beginning,
end or at any position of the linked list. How to delete a node from beginning of a
doubly linked list. How to delete a node from end of a doubly linked list. How to
delete a node from any position of a doubly linked list in C. Algorithm to delete a
node from doubly linked list.

Algorithm to delete node from beginning of a


doubly linked list
Algorithm to delete node from beginning%% Input: head {Pointer to first
node of the linked list}Begin:

If (head == NULL) then

write ('Can't delete from an empty list')

End if

Else then

toDelete ← head;
head ← head.next;
head.prev ← NULL;
unalloc (toDelete)

write ('Successfully deleted first node from the list')

End ifEnd

Algorithm to delete node from end of a doubly


linked list
Algorithm to delete node from end%% Input: last {Pointer to last node
of the linked list}Begin:

If (last == NULL) then

write ('Can't delete from an empty list')

End if

Else then

toDelete ← last;
last ← last.prev;
last.next ← NULL;
unalloc (toDelete)

write ('Successfully deleted last node from the list')

End ifEnd

Algorithm to delete node from any position of a


doubly linked list
Algorithm to delete node from any position%% Input : head {Pointer to
the first node of the list}

last {Pointer to the last node of the list}


N {Position to be deleted from list}Begin:
current ← head;
For i ← 1 to N and current != NULL do

current ← current.next;
End for

If (N == 1) then

deleteFromBeginning()

End if

Else if (current == last) then

deleteFromEnd()

End if

Else if (current != NULL) then

current.prev.next ← current.next
If (current.next != NULL) then

current.next.prev ← current.prev;
End if

unalloc (current)

write ('Node deleted successfully from ', N, ' position')

End if

Else then

write ('Invalid position')

End ifEnd

Steps to delete node from any position of a doubly


linked list
Let us suppose that we want to delete node from 2nd position.

1. Traverse to Nth node of the linked list, lets say a pointer current points to N th node in
our case 2 node.
2. Link the node behind current node with the node ahead of current node, which
means now the N-1th node will point to N+1th node of the list. Which can be
implemented as current->prev->next = current->next.

3. If N+1th node is not NULL then link the N+1th node with N-1th node i.e. now the
previous address field of N+1th node will point to N-1th node. Which can be
implemented as current->next->prev = current->prev.

4. Finally delete the current node from memory and you are done.
C program to insert a node in
Circular Linked List
August 8, 2017Data StructuresC, Circular Linked List, Data Structures,
Linked List, ProgramPankaj Prakash

Write a program to create a circular linked list and insert a new node at the beginning
or at any position in the given list. How to insert a new node at the beginning of a
circular linked list in C. How to insert a new node at any position in a circular linked
list in C. Algorithm to insert new node in Circular linked list in C program.

Algorithm to insert a new node at the beginning


of a Circular linked list
For inserting new node at the beginning of a circular linked list. We need to connect
new node with the first node and re-connect the last node with new node instead of
head node.

Algorithm to insert new node at the beginning of Circular linked


list%%Input : head {Pointer to first node of the linked list}Begin

If (head == NULL) then

write ('List is empty')

End if

Else then

alloc (newNode)

read (data)

newNode.data ← data;
newNode.next ← head;

current ← head;
While (current.next != head) do

current ← current.next;
End while

current.next ← newNode;
End ifEnd

Algorithm to insert new node at the any position


in Circular linked list
Algorithm to insert new node at any position of a circular linked
list%%Input : head {Pointer to first node of the list}

N {Position where to insert}Begin


If (head == NULL) then

write ('List is empty')

End if

Else if (N == 1) then

insertAtBeginning()

End if

Else then

alloc (newNode)

read (data)

newNode.data ← data;

current ← head;
For count ← 2 to N-1 do

current ← current.next;
End for
newNode.next ← current.next;
current.next ← newNode;
End ifEnd

Steps to insert new node in a Circular linked list


Suppose we want to insert a new node in the circular linked list at 3 position i.e. just
after 2nd position. The list initially contains 3 nodes. We will follow below steps to
insert node at 2nd position in the list.

1. Create a newNode and assign some data to its data field.

2. Traverse to N-1 position in the list, in our case since we want to insert node at 3rd
position therefore we would traverse to 3-1 = 2nd position in the list. Say current
pointer points to N-1th node.

3. Link the next pointer field of newNode with the node pointed by the next pointer
field of current(N-1) node. Which means newNode.next = current.next.
4. Connect the next pointer field of current node with the newly created node
which means now next pointer field of current node will point to newNode
and you are done.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy