tutorial 5 stack & queue, midtest

10
Tutorial 5 Stack & Queue, Midtest

Upload: eve-vinson

Post on 31-Dec-2015

27 views

Category:

Documents


4 download

DESCRIPTION

Tutorial 5 Stack & Queue, Midtest. Stack. Last In First Out (LIFO) Stack implemented using Array with top pointer http://www2.latech.edu/~box/ds/Stack/Stack.html Stack implemented using Single Link List with head pointer Probably the best implementation to date? - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Tutorial 5 Stack & Queue,  Midtest

Tutorial 5Stack & Queue, Midtest

Page 2: Tutorial 5 Stack & Queue,  Midtest

• Last In First Out (LIFO)• Stack implemented using Array with top pointer

– http://www2.latech.edu/~box/ds/Stack/Stack.html

• Stack implemented using Single Link List with head pointer– Probably the best implementation to date?– http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/LinkListAppl.html

Stack

TOP

Page 3: Tutorial 5 Stack & Queue,  Midtest

Queue

• First In First Out (FIFO)• Queue implemented as Circular Array

– http://maven.smith.edu/~streinu/Teaching/Courses/112/Applets/Queue/myApplet.html

• Queue implemented as Single Link List with Tail Pointer – Head pointer (easy for insert/delete) for dequeue– Tail pointer (only easy for insert) for enqueue– This is because of the pointer directions– Probably the best implementation to date?

FRONT BACK

Page 4: Tutorial 5 Stack & Queue,  Midtest

Student Presentation• T3 Main Backup

1. Cai Jingfang Chng Jiajie, Colin Tan

2. Li Huan Nur Liyana Bte Roslie

3. Zhang Jianfei Tan Kar Ann

4. Tanvir Islam Jessica Chin Zet Sze

• T4 Main Backup1. Choy Qian Ning, J Liew Hui Sun

2. Goh Khoon Hiang Li Yawen

3. Hanyenkno Afi Tan Peck Luan

4. Ng Xue Lin Sherilyn Wong Suet Teng, Melissa

• T5 Main Backup1. Joyeeta Biswas Ong Kian An

2. Teo Sim Yee Stephanie Tan Yan Hao

3. Wu Shujun Wang Ruohan

4. Liu Na Zheng Yang

• T6 Main Backup1. Zhang Chao Wang Shuling

2. Chua Yu Tong Laura Rasheilla Bte Rajah

3. Koh Yi Ting Brenda Low Wei Chen Gerard J

4. Siddhartha Gan Zhi Wei James

4

Rule: Student in “Main” column has priority to answer the assigned questionIf the student sick/disappear/absent/cannot solve, etc2… the “backup” will take over.

If both students are attending the session,I will create variants of the question for the “backup” students.

Page 5: Tutorial 5 Stack & Queue,  Midtest

Question 1 (Applications)

• Show us 7 applications of Stacks• Show us 7 applications of Queues

• Be creative!

5

Page 6: Tutorial 5 Stack & Queue,  Midtest

Question 2 (Stack: Reorder)

• Get algorithm to reorder items using one stack!• Requires thinking on Stack operations: push, pop, top/seek• Derive patterns!

1. Target Sequence 1 = {1, 2, 3, 5, 4}

2. Target Sequence 2 = {2, 1, 4, 3, 5}

3. Target Sequence 3 = {5, 2, 4, 3, 1}

4. Target Sequence 4 = {1, 4, 3, 2, 5}

6

Only Target Sequence 3 is impossible.

See StackTest.java for details

Page 7: Tutorial 5 Stack & Queue,  Midtest

Question 3 (Stack for Sorting)

• Find algorithm to sort items using two stacks!• Requires thinking on Stack operations: push, pop, top/seek• Hint: try sorting these simple numbers using two stacks:

– {7, 5} {5, 7}– {1, 5, 3} {1, 3, 5}– {4, 3, 2, 1} {1, 2, 3, 4}

• Derive patterns and write your algorithm!• Hint: During the sorting process, one stack will have numbers in

ascending order and the other in descending order.• See StackSort.java for details

7

Page 8: Tutorial 5 Stack & Queue,  Midtest

Question 4 (Queue)

• What if Queue ADT is implemented using TailedLinkedListbut in different way!

• Think about the pros and cons of this strategy!

• The ADT Queue “dequeue” operation is performed by “delete head” and ADT Queue “enqueue” operation is performed by “insert from tail” in TailedLinkedList.

• Modified operations:– Enqueue: insert from head straightforward– Dequeue: delete from tail hard, because adjusting tail pointer is difficult!

• No pro, this modified operations are bad!– If you are presented with this kind of question during exam, do not hesitate to

answer “there is no pro” rather than leaving your answer blank!– If you insist, you can say that this modified operations can easily tell you “what

object is the 2nd last inserted object” …

8

Page 9: Tutorial 5 Stack & Queue,  Midtest

Student Presentation for Tut 6

• Check your email!– If I send you an email, it means that you are assigned to do next week’s

questions…

9

Page 10: Tutorial 5 Stack & Queue,  Midtest

Midterm Test Tips

• Priority:– Java Revisit: low– ADT: Likely embedded in Linked List/Stack/Queue question– Linked List: we spend two weeks here, most likely this appear as the only short

question during midterm test– Stack and Queue: definitely appear

• Try:– Mixing and matching various data structure implementations: variant of linked

list, implementing stacks or queues using funny data structures, etc…