Академический Документы
Профессиональный Документы
Культура Документы
Tail or last node is a node of list whose next link will be null, as any other node would
have a link for it’s next node.
node.getNext()
Accessing the next pointer node->next node.next
node.SetNext()
data Ø data Ø
General Case: list is not empty General case: list is not empty
data Ø data Ø
Start Start:
1. If head==NULL // list is empty
1. newestNode.next=head //link to head
2. head=newestNode
2. head=newestNode //update head
3. Else //list is not empty. Search last node
End
4. Set curr=head
5. While(curr.next!= NULL)
6. curr=curr.next
7. End While // loop will terminate when reached to last
node
8. curr.next=newestNode // update the next link of last
node
9. End If
End
13 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019
Safdar Computer Science Department- CIIT Lahore
Insertion at Location
Location must be valid: Case 2: in middle
>=0 and <=size, assume index of first node=0 Let say 2
previous node node at target index
Case 1: index =0 (Insertion at start)
head data data data *
List can be empty
head Ø data Ø
data Ø
Case 3: index =size (Insertion at end)
List can be non-empty
data Ø
data Ø
*: Remaining list is not shown
14 Created by Saba Anwar, Reviewed and Edited by Asmara 24/10/2019
Safdar Computer Science Department- CIIT Lahore
Insertion at Location
Algorithm: INSERT_LOCATION(head, newestNode)
Input: head node and node to be inserted
Output: list with new node inserted
Steps:
Discussed in class
General case
Doubly linked list can be traversed from start to end and from end to start.
Ø data Ø Start
1. newestNode.next=head
2. If head!=NULL // list was not empty
List is not empty
3. head.prev=newestNode
4. Else // list was empty
head Ø data data Ø 5. tail=newestNode // update tail, as this is first node
6. End If
7. head=newestNode // head would always be updated
Ø data Ø tail
End
Ø data Ø
Ø data Ø
Ø data Ø tail
Ø data Ø
Ø
Ø
Discussed in class
Doubly Circular
Every node contains two links, one points to next node and one point to previous node in sequence.
next
next
next
next
previous
null
previous
previous
previous
16 17 10 20
null
previous
previous previous
next
next
null
previous
previous
previous
previous
20 10 17 16
null
previous previous previous previous