CS250 - Data Structures and
Algorithm
BESE-2A/B
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
Insertion Corner Case
• Empty List
– With head pointer only
• Head = NULL
– With head and tail pointer only
• Head = Tail = NULL
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;
Insert Item at Start
• When list is empty
Insert Item at Start
• When list is not empty
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;
Insert Item at End
• When list
is not empty
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
Delete Item from Start
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
Delete Item from end
Delete Item from end
Deletion Corner Cases
• Empty List
– Prompt user
• Only one item in list
– Set head and tail to NULL
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
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
Search Item in Linked List
Homework Assignment
• Middle of linked list
– Insert an item
– Delete an item
Questions?

Link list 2

  • 1.
    CS250 - DataStructures and Algorithm BESE-2A/B
  • 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 atStart 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;
  • 5.
    Insert Item atStart • When list is empty
  • 6.
    Insert Item atStart • When list is not empty
  • 7.
    Insert item atend 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;
  • 8.
    Insert Item atEnd • When list is not empty
  • 9.
    Delete Item fromStart 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
  • 10.
  • 11.
    Delete Item fromend 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
  • 12.
  • 13.
  • 14.
    Deletion Corner Cases •Empty List – Prompt user • Only one item in list – Set head and tail to NULL
  • 15.
    Traverse List 1. SettempPtr 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 inLinked 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
  • 17.
    Search Item inLinked List
  • 18.
    Homework Assignment • Middleof linked list – Insert an item – Delete an item
  • 19.