1 / 18

More on Linked List

More on Linked List. Yumei Huo Department of Computer Science College of Staten Island, CUNY Spring 2009. Outline. Variants of Singly linked list Doubly linked list. Outline. Variants of Singly linked list Doubly linked list. Variants of Singly linked list. Singly linked circular list

wlois
Download Presentation

More on Linked List

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More on Linked List Yumei Huo Department of Computer Science College of Staten Island, CUNY Spring 2009 Linked list

  2. Outline • Variants of Singly linked list • Doubly linked list Linked list

  3. Outline • Variants of Singly linked list • Doubly linked list Linked list

  4. Variants of Singly linked list • Singly linked circular list • Singly linked list with head node • Singly linked circular list with head node Linked list

  5. E A B C D first Singly linked circular list • Link the last node back to the first Linked list

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

  9. Outline • Variants of Singly linked list • Doubly linked list Linked list

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

  11. Operations Comparison Linked list

  12. Delete a node pointed by p p q Tail Head A B C D q Tail Head D A B C q Tail Head A B D Doubly Linked List p q Head A B C D q Head D A B C q Head A B D Singly Linked List Linked list

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

  14. Delete a node pointed by p in singly linked list p q Head A B C D q Head D A B C 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; Linked list

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

  16. Insert a new element ‘X’ before the node pointed by p in a doubly linked list p t Head Tail A B C p t Head Tail A B C q X q p t Head Tail A B X C 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; Linked list

  17. Insert a new element ‘X’ before the node pointed by p in a singly linked list p t Head A B C p t Head A B C q X q p t 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; Linked list

  18. Thank you! Linked list

More Related