Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.htmlhttp://www2.latech.edu/~box/ds/Stack/Stack.html.

Similar presentations


Presentation on theme: "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.htmlhttp://www2.latech.edu/~box/ds/Stack/Stack.html."— Presentation transcript:

1 Tutorial 5 Stack & Queue, Midtest

2 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 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.htmlhttp://www.cosc.canterbury.ac.nz/people/mukundan/dsal/LinkListAppl.html Stack TOP

3 Queue First In First Out (FIFO) Queue implemented as 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 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? FRONTBACK

4 Student Presentation T3MainBackup 1.Cai JingfangChng Jiajie, Colin Tan 2.Li HuanNur Liyana Bte Roslie 3.Zhang JianfeiTan Kar Ann 4.Tanvir IslamJessica Chin Zet Sze T4MainBackup 1.Choy Qian Ning, JLiew Hui Sun 2.Goh Khoon HiangLi Yawen 3.Hanyenkno AfiTan Peck Luan 4.Ng Xue Lin SherilynWong Suet Teng, Melissa T5MainBackup 1.Joyeeta BiswasOng Kian An 2.Teo Sim Yee StephanieTan Yan Hao 3.Wu ShujunWang Ruohan 4.Liu NaZheng Yang T6MainBackup 1.Zhang ChaoWang Shuling 2.Chua Yu Tong LauraRasheilla Bte Rajah 3.Koh Yi Ting BrendaLow Wei Chen Gerard J 4.SiddharthaGan Zhi Wei James 4 Rule: Student in “Main” column has priority to answer the assigned question If 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.

5 Question 1 (Applications) Show us 7 applications of Stacks Show us 7 applications of Queues Be creative! 5

6 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

7 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

8 Question 4 (Queue) What if Queue ADT is implemented using TailedLinkedList but 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 2 nd last inserted object” … 8

9 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

10 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…


Download ppt "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.htmlhttp://www2.latech.edu/~box/ds/Stack/Stack.html."

Similar presentations


Ads by Google