This document discusses various operations that can be performed on linked lists including inserting and deleting nodes at different positions, traversing the list, and searching for a node. It provides pseudocode to insert nodes at the beginning and end of a linked list, delete nodes from the beginning and end, and traverse and search through a linked list. It also discusses handling empty lists and lists with only one item for these operations.
2. Linked List Operations
• Insert a node
– At the start of the list
– At the end of the list
• Delete a node
– At the start of the list
– At the end of the list
• Traverse linked list
– Iterate through each item/node
• Search
– Find the specified item in linked list
9/17/2012 DSA - Fall 2012 - SEECS, NUST 2
3. Insertion Corner Case
• Empty List
– With head pointer only
• Head = NULL
– With head and tail pointer only
• Head = Tail = NULL
4. Insert Item at Start
1. Empty node is created –
new node
2. Initialize the value to
user specified value
3. If list is empty
1. Set next pointer to null
2. Set head to new node
4. If list is not empty
1. Set the next pointer to
the head
2. Set head to new node
1. Node item = new
Node();
2. Item.data = data;
3. If (head == NULL)
1. Item.next = NULL;
2. Head = item;
4. Else
1. Item.next = head;
2. Head = item;
7. Insert item at end
1. Empty node is created
2. Initialize the value to
user specified value
3. When list is not empty
1. Set next pointer of new
node to NULL
2. Set the next pointer of
last node to the new
node
4. When list is empty
1. Set the next pointer of
new node to NULL
2. Set head to new node
1. Node item = new Node();
2. Item.data = data;
3. If( head == NULL)
1. Item.next = NULL;
2. Tail.next = item;
4. Else
1. Item.next = NULL;
2. Head = Tail = item;
9. Delete Item from Start
1. Set tempPtr to head
2. Set head to next of
itself / tempPtr
3. Delete tempPtr
1. tempPtr = head
2. Head = head->next
3. Delete tempPtr
11. Delete Item from end
1. Find the node just
before the start by
traversing the list
2. Delete the last node
3. Set tail to current node
4. Set next of current
node to null
1. tempPtr->next = tail;
2. Delete tail
3. Tail = node
4. tempPtr->next = NULL
14. Deletion Corner Cases
• Empty List
– Prompt user
• Only one item in list
– Set head and tail to NULL
15. Traverse List
1. Set tempPtr to head
2. If tempPtr is not NULL
– Print node data value
– Set tempPtr to next of itself
– Go to 2
3. Else
– Exit
16. Search Item in Linked List
• Scan exiting list to find the specified item
1. Get the value to search
2. Set tempPtr to head
3. Check if tempPtr is NULL
4. No
1. If tempPtr data is same as user ’s data
• Exit Loop with Success
2. No
• Set tempPtr to tempPtr next value
• Go to 3
5. Yes
1. Exit Loop