​Recitation 4, Linked​ List Operations

$24.99 $18.99

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…

5/5 – (2 votes)

You’ll get a: zip file solution

 

Categorys:

Description

5/5 – (2 votes)

Objectives

    1. Insertion

    1. Traversal

    1. Deletion

    1. Exercise

  1. 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

    1. Insertion at the start.

    1. Insertion at a given position.

    1. Insertion at the end.

  1. Inserting at the start of the list.

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

  1. Create a new node,

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

  1. Update head pointer to new node.

2. Insertion at a given position

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

  1. Create a new node (B).

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

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

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

    • 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’.

  1. Deletion of the first node

      1. 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 variabletemphaving a reference copy of the head node.

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

  1. 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 bytemp.

c. Linked list representation after deletion.

  1. Deletion of the last node

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

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

    1. Create a variableprevhaving a reference copy of the head node.

    2. Create a variablepreshaving a reference copy of the next node after the head.

    1. Traverse the list untilpresis pointing to the last node ‘A’.

prevwill be pointing to the second last node now.

  1. Makeprevpoint toNULL.

  2. Free the node ‘A’ pointed to bypres.

c. Linked list representation afterprevandpreshave 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

  1. Steps followed to delete every node in the linked list.

    1. Create a variableprevhaving a reference copy of the head node.

    1. Create a variablepreshaving a reference copy of the next node after the head.

    1. While traversing the list, at each step delete/free the memory pointed to byprev.

    1. Now, pointprevto thepresand pointpresto the next node afterpres (ie. pres->next).

    1. Traverse the list untilpresis pointing to theNULL.

prevwill be pointing to the second last node now.

    1. Free the memory pointed to byprev.Now every element in the linked list is deleted/freed.

  1. Linked list representation after deletion of all the nodes.

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:

  1. Given a position in the linked list, delete the node at that position.(Silver problem – Mandatory )

  2. Swap the first and last nodes in a linked list (Gold problem)

8

​Recitation 4, Linked​ List Operations
$24.99 $18.99