DS Algorithms
DS Algorithms
Pop operations:
Creating a node:
Step 1: Include all the Header files which are used in the program.
Step 3: Define a Node structure with two members data and next
Step 4: Implement the main method by displaying operations menu and make suitable
function calls in the main method to perform user selected operation.
Insert Operations
We can use the following steps to insert a new node in the single linked list...
Step 3: If it is Empty then, set newNode → next = NULL and Start = newNode.
Step 4: If it is Not Empty then, define a node pointer current and initialize with Start.
Step 5: Keep moving the Current to its next node until it reaches to the node after which
we want to insert the newNode
Step 6: Finally, Set 'Current → next = newNode' and 'Current =newNode '
Delete operation
We can use the following steps to delete a specific node from the single linked list...
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 'temp' and 'prev' and initialize
'temp' with Start.
Step 4: Keep moving the temp until it reaches to the exact node to be deleted or to the
last node. And every time set 'prev = temp' before moving the 'temp' to its next node.
Step 5: If it is reached to the last node then display 'Given node not found in the list!
Deletion not possible!!!'. And terminate the function.
Step 6: If it is reached to the exact node which we want to delete, then check whether
list is having only one node or not
Step 7: If list has only one node and that is the node to be deleted, then
set Start = NULL and delete temp (free(temp)).
Step 8: If list contains multiple nodes, then check whether temp is the first node in the
list (temp == Start).
Step 9: If temp is the first node then move the Start to the next node (Start = Start →
next) and delete temp.
Step 10: If temp is not first node then check whether it is last node in the list (temp →
next == NULL).
Step 11: If temp is last node then set prev → next = NULL and delete temp (free(temp)).
Step 12: If temp is not first node and not last node then set prev → next = temp →
next and delete temp (free(temp)).
Insertion
We can use the following steps to insert a new node after a node in the double linked list...
Step 4: If it is not Empty then, define a node pointer last and initialize last with Start.
Step 5: Step 7: Assign Last to Newnode-> prev, newNode ->right to Null and then assign
newnode to last
Deletion
We can use the following steps to delete a node from the double linked list...
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 a Node pointer 'temp' and initialize with Start.
Step 4: Keep moving the temp until it reaches to the exact node to be deleted or to the last
node.
Step 5: If it is reached to the last node, then display 'Given node not found in the list!
Deletion not possible!!!' and terminate the fuction.
Step 6: If it is reached to the exact node which we want to delete, then check whether list is
having only one node or not
Step 7: If list has only one node and that is the node which is to be deleted then
set Start to NULL and delete temp (free(temp)).
Step 8: If list contains multiple nodes, then check whether temp is the first node in the list
(temp == Start).
Step 9: If temp is the first node, then move the Start to the next node (Start = Start → next),
set Start of previous to NULL (Start → previous = NULL) and delete temp.
Step 10: If temp is not the first node, then check whether it is the last node in the list (temp
→ next == NULL).
Step 11: If temp is the last node then set temp of previous of next to NULL (temp →
previous → next = NULL) and delete temp(free(temp)).
Step 12: If temp is not the first node and not the last node, then
set temp of previous of next to temp of next (temp → previous → next = temp →
next), temp of next of previous to temp of previous (temp → next → previous = temp →
previous) and deletetemp (free(temp)).