Date post: | 23-Dec-2015 |
Category: |
Documents |
Upload: | arron-lane |
View: | 223 times |
Download: | 0 times |
Tutorial 5Linked 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?
– 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 ...
Node N
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 ...
Node N
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 2
Node ...
Node N
Head
Node 1 Node 2
Node ...
Node N
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 2
Node ...
Node N
Head
Node 1 Node 2
Node ...
Node N
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 and
ask you something about such variations in the mid/final exam…– Make sure you understand these concepts!
Dummy Node 1
Node ...
Node N
Head
• 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
Stack
Node 1 Node 2
Node ...
Node N
Head
ADT Stack using SLL as underlying data structure
Node 1
Node 2
Node ...
Node N
Head isTop of Stack
Node 0 Top: efficientPush: efficientPop: efficient
• 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
Queue
Node 1 Node 2
Node ...
Node N
HeadFront of Queue
Tail =Rear of Queue
Node N+1
Back: efficientEnqueue: efficient
Front: efficientDequeue: efficient
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
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
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