Vous êtes sur la page 1sur 22

CS250 - Data Structures and

Algorithm
BESE-2A/B

Lecture 05
Aasma Zahid
Singly Linked List
• Advantages
– Dynamic allocation
– Efficient Insertion and Deletion

9/20/2012 DSA - Fall 2012 - SEECS, NUST 2


Singly Linked List
• Disadvantages
– Each node maintain forward linkage only

– Overhead
• Insertion & deletion at end - Reach Predecessor
– Within sight
– But, out of reach

– Expensive operation
• Depends upon the length of the list

9/20/2012 DSA - Fall 2012 - SEECS, NUST 3


Doubly Linked List
• Each node maintain two pointer
– One pointing forward
– One pointing backward
Element

Node
Reference to an
element
Reference to next
next node
prev Reference to
previous node

9/20/2012 DSA - Fall 2012 - SEECS, NUST 4


Doubly Linked List

.back .data .next

9/20/2012 DSA - Fall 2012 - SEECS, NUST 5


Doubly Linked List Operations
• Insert a node
– At the start of the list
– In the middle of the list
– At the end of the list

• 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

9/20/2012 DSA - Fall 2012 - SEECS, NUST 8


Insert at Start

9/20/2012 DSA - Fall 2012 - SEECS, NUST 9


Insert at End
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 = tail
node to tail
2. Item.next = NULL
2. Set the next pointer of new
node to the NULL 3. Tail.next = item
3. Set the next pointer of tail to 4. Tail = item
new node
4. Set tail to new node
9/20/2012 DSA - Fall 2012 - SEECS, NUST 10
Insert at End

9/20/2012 DSA - Fall 2012 - SEECS, NUST 11


Insert at End

9/20/2012 DSA - Fall 2012 - SEECS, NUST 12


Delete from Start
1. Set head to its next 1. Head = Head.Next
node
2. Delete node where 2. Delete Head.Back
head’s back pointing at
3. Set head’s back to 3. Head.Back = NULL
NULL

9/20/2012 DSA - Fall 2012 - SEECS, NUST 13


Delete from Start

9/20/2012 DSA - Fall 2012 - SEECS, NUST 14


Delete from End
1. Set tail to its back 1. Tail = tail.back
node
2. Delete node where 2. Delete Tail.next
tail’s next pointing at
3. Set tail next to null 3. Tail.next = NULL

9/20/2012 DSA - Fall 2012 - SEECS, NUST 15


Delete from End

9/20/2012 DSA - Fall 2012 - SEECS, NUST 16


Traversal
• Scan list
– Starting from the start of the list, moving one
node forward
– Starting from the end of the list, moving one node
backward

9/20/2012 DSA - Fall 2012 - SEECS, NUST 17


Traversal from Start
1. Set tmp to head
2. if tmp is not null
1. Print Node Details
2. Move tmp to one node forward
3. Go to 2.
3. Else
1. Exit

9/20/2012 DSA - Fall 2012 - SEECS, NUST 18


Traversal from End
1. Set tmp to tail
2. if tmp is not null
1. Print Node details
2. Move tmp to one node backward
3. Go to 2
3. Else
1. Exit

9/20/2012 DSA - Fall 2012 - SEECS, NUST 19


Searching
• Scan a list to find a specific item

1. Set tmp = head


2. If(tmp!=NULL)
– If(tmp->data == data)
 Exit with success
– Else
 tmp = tmp->next
 Go to 2
3. Else
– Exit
9/20/2012 DSA - Fall 2012 - SEECS, NUST 20
Homework Assignment
• At the middle
– Insert an item
– Delete an item

9/20/2012 DSA - Fall 2012 - SEECS, NUST 21


Questions?

9/20/2012 DSA - Fall 2012 - SEECS, NUST 22

Vous aimerez peut-être aussi