120 likes | 604 Views
Tutorial 5 Linked List Variations, Stack, & Queue. 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?
E N D
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 1 Node 2 Node N Node ... Head
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 1 Node 2 Node N Node ... Head Tail
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 1 Node 1 Node 2 Node 2 Node N Node N Node ... Node ... Head Tail
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 1 Node 1 Node 2 Node 2 Node N Node N Node ... Node ... Head Head
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 andask you something about such variations in the mid/final exam… • Make sure you understand these concepts! Dummy Node 1 Node N Node ... Head
Stack • Behavior: Last In First Out (LIFO) • Stack implemented using Array with top pointer • http://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.html Node 1 Node 2 Node N Node ... Head ADT Stack using SLL as underlying data structure Top: efficient Push: efficient Pop: efficient Node 0 Head isTop of Stack Node 1 Node 2 Node ... Node N
Queue • Behavior: First In First Out (FIFO) • Queue implemented using Circular Array • http://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 Node 1 Node 2 Node N Node N+1 Node ... HeadFront of Queue Tail =Rear of Queue Front: efficient Dequeue: efficient Back: efficient Enqueue: efficient
Student Presentation • T5: • >< • Ramanathan Anu • >< • Sal Visoth • T10: • Puranik Pranay • Sistla • Suresh • >< • T13: • Mai Xiangjun • Nikhil • Oh Kok Wei • Peck Shan Ren • T9: • >< • Nguyen Sy Nguyen • Sandhya • >< • T17: • Kalpit Jain • >< • Lee Jia Yi • Lee Yue Ting
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?
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 emphasisas 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