180 likes | 204 Views
Explore different forms of linked lists like singly linked, doubly linked, circular, and more. Understand operations such as insertion, deletion, and node comparison.
E N D
More on Linked List Yumei Huo Department of Computer Science College of Staten Island, CUNY Spring 2009 Linked list
Outline • Variants of Singly linked list • Doubly linked list Linked list
Outline • Variants of Singly linked list • Doubly linked list Linked list
Variants of Singly linked list • Singly linked circular list • Singly linked list with head node • Singly linked circular list with head node Linked list
E A B C D first Singly linked circular list • Link the last node back to the first Linked list
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
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
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
Outline • Variants of Singly linked list • Doubly linked list Linked list
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
Operations Comparison Linked list
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
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
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
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
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
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
Thank you! Linked list