Download presentation
Presentation is loading. Please wait.
1
Tutorial 5 Linked List Variations, Stack, & Queue
2
Linked List Variations (1) Two weeks ago, we have reviewed “Single Linked List” –Revisited in Stack data structure later There exists several Linked List Variations: –With or without Tail Pointer? –One link or double links per node? –Circular or not? –And combinations of these… Next slides: some diagrams of the variations asked in Q1, then one of you will explain the differences (max hops, memory space)! Node 1Node 2 Node... Node N Head
3
Linked List Variations (2) Single Linked List with Tail Pointer –Other than head, we also have tail pointer pointing to the last item –Pro: Can visit the tail very fast or add new item from tail –Cons: Slow to delete the tail… –Revisited in Queue data structure later Node 1Node 2 Node... Node N HeadTail
4
Linked List Variations (3) Circular Single Linked List –Tail.Next = Head at tail, the link cycles back to the head –Pro: Can visit all node from any node, good for Round Robin stuffs –Cons: Slow to delete the tail… –Can choose to remember Tail only, save one pointer… Node 1Node 2 Node... Node N Head Node 1Node 2 Node... Node N Tail
5
Linked List Variations (4) Double Linked List –Two pointers per node: forward/backward –Pro: Can go backwards –Pro: If you have tail pointer, you can also delete tail efficiently! –Cons: Extra pointer is an overhead… –Can be circular or not…, with or without tail… Node 1Node 2 Node... Node N Head Node 1Node 2 Node... Node N Head
6
Linked List Variations (5) Single Linked List with dummy head node –Pro: Simplify Single Linked List code –Cons: Same as standard Single Linked List We will discuss their pro/cons in Q1 Be careful, the lecturers can vary these combinations and ask you something about such variations in the mid/final exam… –Make sure you understand these concepts! DummyNode 1 Node... Node N Head
7
Behavior: Last In First Out (LIFO) Stack implemented using Array with top pointer –http://www2.latech.edu/~box/ds/Stack/Stack.htmlhttp://www2.latech.edu/~box/ds/Stack/Stack.html (Perhaps) the best implementation? –Using Single Link List with head pointer only –http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/LinkListAppl.htmlhttp://www.cosc.canterbury.ac.nz/people/mukundan/dsal/LinkListAppl.html Stack Node 1Node 2 Node... Node N Head ADT Stack using SLL as underlying data structure Node 1 Node 2 Node... Node N Head is Top of Stack Node 0 Top: efficient Push: efficient Pop: efficient
8
Behavior: First In First Out (FIFO) Queue implemented using Circular Array –http://maven.smith.edu/~streinu/Teaching/Courses/112/Applets/Queue/myApplet.htmlhttp://maven.smith.edu/~streinu/Teaching/Courses/112/Applets/Queue/myApplet.html Better: use Single Linked List with Tail Pointer –Head pointer for dequeue/front, Tail pointer for enqueue/back We set these roles because pointer directions in SLL (arrows to the right) imply that only insertion is efficient at tail (thus only ok for enqueue) and both insertion/deletion are efficient at head (but since we already use tail for enqueue, head is for dequeue) –Or use Circular Single Linked List: save 1 pointer: Head = Tail.next ADT Queue using SLL with tail pointer as underlying data structure, circular SLL also ok Queue Node 1Node 2 Node... Node N Head Front of Queue Tail = Rear of Queue Node N+1 Back: efficient Enqueue: efficient Front: efficient Dequeue: efficient
9
Student Presentation T5: 1.>< 2.Ramanathan Anu 3.>< 4.Sal Visoth T10: 1.Puranik Pranay 2.Sistla 3.Suresh 4.>< T13: 1.Mai Xiangjun 2.Nikhil 3.Oh Kok Wei 4.Peck Shan Ren T9: 1.>< 2.Nguyen Sy Nguyen 3.Sandhya 4.>< T17: 1.Kalpit Jain 2.>< 3.Lee Jia Yi 4.Lee Yue Ting 9
10
Follow Up Questions Q1 –What other “creative” linked lists that you can think of? Q2: –Using stack to do this is a bit overkill… –Can you help Mr Scramble WITHOUT using stack? It is actually much easier Q3: –Again, using stack to do this is also overkill –Can you do this WITHOUT using stack? It is actually much easier Q4: –What if you reverse all the links? (reversed single linked list) With head pointer only? With both head and tail pointers? 10
11
Midterm Test Tips Analyzing the topics: –C/C++: Likely used as part of question in the essay But may appear as standalone question in MCQ –ADT: Likely embedded in Linked List/Stack/Queue question in essay –List ADT: Vector versus Linked List idea, we spend two weeks here, most likely this appear as the essay? If you want to put a bet, this is your best bet… –Stack and Queue: likely appear but perhaps? less emphasis as this just recently taught… Try: –Mixing and matching various data structure implementations: Funny variants of linked list Implementing stacks or queues using funny data structures, etc… –Try implementing queue using “circular vector”, it is tricky
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.