Starting from:

$30

​Recitation Linked​ List Operations Solution







Objectives




Insertion



Traversal



Deletion



Exercise






























Insertion in a linked list



Adding a new node in a linked list is a multi-step activity. We shall learn this with diagrams here. First, create a node using the same structure and find the location where it must be inserted.




Scenarios in insertion




Insertion at the start.



Insertion at a given position.



Insertion at the end.





















Inserting at the start of the list.



Now we will insert an element at the start of the list.





































1
CSCI 2270 – Data Structures




​Recitation 4,




Linked​ List Operations



































































Create a new node,



Update the next pointer of that node to start of the list. (Value of the head pointer)



Update head pointer to new node.









​2. Insertion at a given position

For example let us insert a new node at position 2.



































































Create a new node (B).



Count and traverse until the node previous to given position i.e A.



Store the A’s next pointer value in a temporary variable.



Update the next pointer of A to the address of new node.









2
CSCI 2270 – Data Structures




​Recitation 4,




Linked​ List Operations




Copy the temporary variable’s value to B’s next pointer.



Now B’s next pointer points to the address of the node C.



Insertion at the end






















































Create a new node B.



Traverse till the node whose next pointer points to NULL. (A)



Update the next pointer of A to B’s address.



Point B’s next pointer to NULL.



2. Traversal and printing




To print a list, we need to traverse through all the nodes in the list until we encounter the last node. When a node points to NULL we know that it is the last node.







node = root;

while​ ( node != ​NULL​ )

{

​cout ​<< node-value << ​endl​;

node = node-next;




}
















3. Deletion




​Deleting a node in the linked list is a multi-step activity. Let’s call the node to be deleted as ‘A’ and the node ‘A’ is pointing to as ‘B’.










3
CSCI 2270 – Data Structures




​Recitation 4,




Linked​ List Operations




First, the position of the node to be deleted (‘A’) must be found.



The next step is to point the node pointing to ‘A’, to point to ‘B’.



The last step is to free the memory held by ‘A’.















Deletion of the first node






Given below is the linked list representation before the deletion of the first node










































​b. Steps followed to delete the first node (‘A’) having value ‘1’.

i. Create a variable ​temp​having a reference copy of the head node.




Point the head node from ‘A’ to ‘B’



Head is now pointing to ‘B’. So, The Linked List’s first element now is ‘B’ with the



value‘2’




iv. Free the node ‘A’ pointed to by ​temp​.










​c. Linked list representation after deletion.

















































4
CSCI 2270 – Data Structures




​Recitation 4,




Linked​ List Operations















































































Deletion of the last node






Given below is a linked list representation before deletion of the last node
















































Steps followed to delete the last node (‘A’) having value ‘4’.



Create a variable ​prev ​having a reference copy of the head node.
Create a variable ​pres ​having a reference copy of the next node after the head.



Traverse the list until ​pres​is pointing to the last node ‘A’.
prev ​will be pointing to the second last node now.

Make ​prev ​point to ​NULL.
Free the node ‘A’ pointed to by ​pres.















5
CSCI 2270 – Data Structures




​Recitation 4,




Linked​ List Operations




​c. Linked list representation after ​prev ​and ​pres ​have completed traversing.


























































d. Linked list representation after deletion of the node ‘A’ and pointing prev to NULL.


















































































3. Deletion of a linked list







​The deletion of a linked list involves iteration over the complete linked list and deleting (freeing) every node in the linked list.




a. Given below is a linked list representation before deletion of the last node







6
CSCI 2270 – Data Structures




​Recitation 4,




Linked​ List Operations























































Steps followed to delete every node in the linked list.



Create a variable ​prev ​having a reference copy of the head node.



Create a variable ​pres ​having a reference copy of the next node after the head.



While traversing the list, at each step delete/free the memory pointed to by ​prev.



Now, point ​prev​to the ​pres​and point ​pres​to the next node after ​pres (ie. pres-next)​.



Traverse the list until ​pres​is pointing to the ​NULL​.
prev ​will be pointing to the second last node now.

Free the memory pointed to by ​prev. ​Now every element in the linked list is deleted/freed.






Linked list representation after deletion of all the nodes.

















































































7
CSCI 2270 – Data Structures




​Recitation 4,




Linked​ List Operations


































4. Exercise













Download the Recitation 4 folder from moodle. There are LinkedList header, implementation and main files.




Your task is to complete the following function/functions:




Given a position in the linked list, delete the node at that position​.(Silver problem - Mandatory )
Swap the first and last nodes in a linked list (Gold problem)



































































































8

More products