Presentation is loading. Please wait.

Presentation is loading. Please wait.

More on Linked List Yumei Huo Department of Computer Science

Similar presentations


Presentation on theme: "More on Linked List Yumei Huo Department of Computer Science"— Presentation transcript:

1 More on Linked List Yumei Huo Department of Computer Science
Sequences 2019/7/24 More on Linked List Yumei Huo Department of Computer Science College of Staten Island, CUNY Spring 2009 2019/7/24 Linked list

2 Outline Variants of Singly linked list Doubly linked list 2019/7/24

3 Outline Variants of Singly linked list Doubly linked list 2019/7/24

4 Variants of Singly linked list
Singly linked circular list Singly linked list with head node Singly linked circular list with head node 2019/7/24 Linked list

5 Singly linked circular list
Link the last node back to the first A B C D first E 2019/7/24 Linked list

6 Print all the elements in a singly linked circular list
B C D first E Node * tmp=first; if (tmp != 0) { cout << tmp->data; while (tmp->next !=first) tmp=tmp->next; } 2019/7/24 Linked list

7 Singly linked list with head node
Add an additional node, called head node, at the front first Head node Empty list A B C first Head node Singly linked list with head node 2019/7/24 Linked list

8 Singly linked circular list with head node
first Head node Empty list A B C first Head node Circular list with head node 2019/7/24 Linked list

9 Outline Variants of Singly linked list Doubly linked list 2019/7/24

10 Doubly Linked List Nodes implement Position and store: data
link to the previous node link to the next node prev next data node Tail Head A B C D 2019/7/24 Linked list

11 Operations Comparison
Singly Linked List Doubly Linked List Determine the length O(n) Find the kth element O(k) Search for a given element Delete the kth element Insert a new element just after the kth Delete a node pointed by p O(1) Insert a new element before the node pointed by p 2019/7/24 Linked list

12 Delete a node pointed by p
q Head Tail A B C D q Tail Head A B C D q Tail Head A B D Doubly Linked List p q Head A B C D q Head A B C D q Head A B D Singly Linked List 2019/7/24 Linked list

13 Delete a node pointed by p in a doubly linked list
q Head Tail A B C D q Tail Head A B C D q Tail Head A B D Doubly Linked List p->prev->next=p->next; p->next->prev=p->prev; delete p; 2019/7/24 Linked list

14 Delete a node pointed by p in singly linked list
q Head A B C D q Head A B C D q Head A B D Singly Linked List //search p’s previous node q, assume p exists and is not the 1st node //(what if p is the 1st node?): Node * q=head; while (q->next != p) q=q->next; //let q point to p’s next node q->next=p->next; delete p; 2019/7/24 Linked list

15 Insert a new element ‘X’ before the node pointed by p
Head A B C Tail t p Head A B C Tail X q t q p Head A B X C Tail Doubly Linked List t p Head A B C p t Head A B C q X t q p Head A B X C Singly Linked List 2019/7/24 Linked list

16 Sequences 2019/7/24 Insert a new element ‘X’ before the node pointed by p in a doubly linked list t p Head A B C Tail t p Head A B C Tail X q t q p Head A B X C Tail Doubly Linked List //create a new node containing ‘X’: Node *q = new Node; q->data=‘X’; //insert q before p: q->prev=p->prev; q->next=p; p->prev->next=q; p->prev=q; 2019/7/24 Linked list

17 Insert a new element ‘X’ before the node pointed by p in a singly linked list
Head A B C p t Head A B C q X t q p Head A B X C Singly Linked List //create a new node containing ‘X’: Node * q= new Node; Q->data = ‘X’; //search p’s previous node t, assume p exists and is not the 1st node //(what if p is the 1st node?): Node * t= head; while (q->next != p) q=q->next; //insert q between t and p q->next=p; t->next=q; 2019/7/24 Linked list

18 Thank you! 2019/7/24 Linked list


Download ppt "More on Linked List Yumei Huo Department of Computer Science"

Similar presentations


Ads by Google