C Program To Insert Node at The Beginning of Singl
C Program To Insert Node at The Beginning of Singl
createSinglyLinkedList (head)
alloc (newNode)
End if
Else then
read (data)wo
newNode.data ← data
newNode.next ← head
head ← newNode
End elseEnd
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.
createSinglyLinkedList (head)
alloc (newNode)
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
alloc (newNode)
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
newNode.next ← temp.next
temp.next ← newNode
End if
End elseEnd
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).
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:
toDelete ← head
head ← head.next
unalloc (toDelete)
End ifEnd
2. Move the head to the second node of the linked list i.e. head = head->next.
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.
End if
Else then
toDelete ← head
secondLastNode ← head
While (toDelete.next != NULL) do
secondLastNode ← toDelete
toDelete ← toDelete.next
End while
head ← NULL
End if
Else then
secondLastNode.next ← NULL
End else
unalloc (toDelete)
End elseEnd
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.
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.
n node to be deletedBegin:
If (head == NULL) then
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
head ← head.next
End if
prevNode.next ← toDelete.next
toDelete.next ← NULL
unalloc (toDelete)
End if
End elseEnd
3. Free the memory occupied by the nth node i.e. toDelete node.
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)
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
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
temp ← head
For i←1 to N-1 do
break
End if
temp ← temp.next;
End for
If (N == 1) then
insertAtBeginning()
End if
insertAtEnd()
End if
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
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.
End if
Else then
toDelete ← head;
head ← head.next;
head.prev ← NULL;
unalloc (toDelete)
End ifEnd
End if
Else then
toDelete ← last;
last ← last.prev;
last.next ← NULL;
unalloc (toDelete)
End ifEnd
current ← current.next;
End for
If (N == 1) then
deleteFromBeginning()
End if
deleteFromEnd()
End if
current.prev.next ← current.next
If (current.next != NULL) then
current.next.prev ← current.prev;
End if
unalloc (current)
End if
Else then
End ifEnd
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.
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
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
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.