Académique Documents
Professionnel Documents
Culture Documents
Algorithm
BESE-2A/B
Lecture 05
Aasma Zahid
Singly Linked List
• Advantages
– Dynamic allocation
– Efficient Insertion and Deletion
– Overhead
• Insertion & deletion at end - Reach Predecessor
– Within sight
– But, out of reach
– Expensive operation
• Depends upon the length of the list
Node
Reference to an
element
Reference to next
next node
prev Reference to
previous node
• Deletion a node
– At the start of the list
– In the middle of the list
– At the end of the list
• Traversal
– At the start of the list
– At the end of the list
• Searching
– Scan list contents
9/20/2012 DSA - Fall 2012 - SEECS, NUST 6
Insert at Start
1. Create a new node 1. Node item = new Node();
2. Initialize the value to user 2. Item.data = data;
specified value
3. If list is empty 3. If(head == tail == NULL)
1. Set next pointer to null 1. item.next = NULL
2. Set back pointer to null 2. Item.back = NULL
3. Set head to new node 3. Head = item
4. Set tail to new node 4. Tail = item
4. If list is not empty 4. Else
1. Set the back pointer of new 1. Item.back = NULL
node to NULL
2. Item.next = head
2. Set the next pointer of new
node to the head 3. Head.back = item
3. Set the back pointer of head 4. Head = item
to new node
4. Set head to new node
9/20/2012 DSA - Fall 2012 - SEECS, NUST 7
Insert at Start