Presentation is loading. Please wait.

Presentation is loading. Please wait.

0 of 37 Stacks and Queues Lecture 17. 1 of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,

Similar presentations


Presentation on theme: "0 of 37 Stacks and Queues Lecture 17. 1 of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,"— Presentation transcript:

1 0 of 37 Stacks and Queues Lecture 17

2 1 of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally, documentation tells us purpose, error conditions, what resources (such as classes and packages) the method needs, etc.. o we don’t know anything about its implementation - encapsulation o set of signatures and return types for an entire class designed to store and manage data is called an Abstract Data Type 1 (ADT) – in Java, ADT’s are supported by the interface feature of Java o In these linked list lectures, show both how to implement various list ADTs using linked nodes, and then use those ADTs with simple programs to demonstrate their use. ArrayList s and NodeLists are ADTs that adhere to Java’s list interface Note: full description of an ADT is sometimes called an API (Application Program Interface) o Term “Application Program Interface” coined by former undergraduate Ira Cotton in 1968. 1. This is an informal definition. ADT also has a mathematical definition.

3 2 of 37 Stacks ●Stack has special requirements for insertion and deletion called o push and pop ●Instead of being able to insert and delete nodes from anywhere in the list, can only add and delete nodes from top of Stack o LIFO (last in, first out) ●W e’ll implement a stack with a linked list and then use it in a simple demo app

4 3 of 37 Stack Constructor ●When generic Stack is instantiated, it contains an empty MyLinkedList ●You will fill in with whatever type of object your Stack will hold public class Stack { private MyLinkedList _list; public Stack() { _list = new MyLinkedList ; } /* other methods elided */ }

5 4 of 37 Methods of a Stack Add element to top of stack Remove element from top of stack. Returns whether stack has elements. Returns number of elements in stack public void push(Type el) { //elided } public Type pop () { //elided } public boolean isEmpty() { //elided } public int size() { //elided }

6 5 of 37 Pushing an Object ●When pushing an element, it is always added to front of Linked List ●Let’s see what this does... //in the Stack class... public Node push(Type newData) { return _list.addFirst(newData); } 123

7 6 of 37 Popping an Object //in the Stack class... public Type pop() { return _list.removeFirst(); } ●When popping an element, element is always removed from top of Stack so call removeFirst on the MyLinkedList ● remove returns element removed, and Stack in turn returns it ●Remember that removeFirst method of MyLinkedList first checks to see if list is empty ●Let’s see what this does... 1 3 2

8 7 of 37 isEmpty ●The Stack will be empty if the _list is empty ●Returns a boolean that is true if the Stack is empty and false otherwise //in the Stack class... public boolean isEmpty() { return _list.isEmpty(); }

9 8 of 37 size Size of Stack will be the size of the Linked List that it contains Remember size is updated whenever a Node is added to or deleted from _list during push and pop methods //in the Stack class... public int size() { return _list.size(); }

10 9 of 37 12 1 1 1 1 3 22 2 4 1 2 1 3 2 4 1 push(1)push(2)push(3)pop() push(4)pop()

11 10 of 37 First Example: Execution Stacks Each method has an Activation Record (AR) o contains an execution pointer to instruction to be executed next in method o also contains all local variables and parameters of the method When methods execute and call other methods, Java uses a Stack to track these calls o when a method calls another method, Java adds the activation record of the called method to the Stack o when the new method is finished, its AR is removed from the Stack, and the previous method is continued o method could be different or a recursively called clone

12 11 of 37 Execution Stacks AR of Method E AR of Method D AR of Method C AR of Method B AR of Method A Top of Stack A E B DC A calls B B calls C … etc.

13 12 of 37 ShoeBurger Demo! In honor of former headTA Andrew Shewlik

14 13 of 37 Bootstrapping ADT’s In effect, this stack ADT is implemented as a thin wrapper over a linked list ADT, but the user has no knowledge of that Could also implement it with arrays or ArrayList s, but that would be more inefficient due to the constant data movement We’ll use same technique to implement a queue

15 14 of 37 What are Queues? ●Similar to stacks, but elements are removed in different order o information retrieved in the same order it was stored o FIFO: First In, First Out (as opposed to stacks, which are LIFO: Last In, First Out) ●Examples: o standing on line at the checkout counter or movie theater o waitlist for TA hours

16 15 of 37 Enqueuing and Dequeuing Before Enqueuing 1 2 3 head of queue tail of queue 4 student to add After Enqueuing 1 2 3 4 head of queue tail of queue ●Enqueuing: adds a node ●Dequeuing: removes a node

17 16 of 37 Enqueuing and Dequeuing 1 student removed from queue Before Dequeuing head of queue 1 2 3 4 tail of queue 2 3 4 After Dequeuing head of queue tail of queue ●Enqueuing: adds a node ●Dequeuing: removes a node

18 17 of 37 Enqueuing and Dequeuing Before Dequeuing head of queue 1 2 3 4 tail of queue 2 3 4 After Dequeuing head of queue tail of queue ●Enqueuing: adds a node ●Dequeuing: removes a node

19 18 of 37 Our Queue ●Let’s use another data structure to help us make our Queue ●Contain a MyLinkedList within our Queue class o enqueue will add to the end of the MyLinkedList o dequeue will remove the first element in the MyLinkedList public class Queue { private MyLinkedList _list; public Queue() { _list = new MyLinkedList (); } // Other methods elided }

20 19 of 37 Our Queue ●Need following methods: o enqueue o dequeue public class Queue { private MyLinkedList _list; public Queue() { _list = new MyLinkedList (); } public void enqueue(Type newNode) { // code to follow... } public void dequeue() { // code to follow... }

21 20 of 37 Enqueue ●Just call _list ’s addLast method! ●This will add the node to end of _list public void enqueue(Type newNode){ _list.addLast(newNode); }

22 21 of 37 Dequeue ●We want first node in _list ●Use _list ’s removeFirst method! ●What if _list is empty? There will be nothing to dequeue. ●Our MyLinkedList class removeFirst() returns null in this case, so dequeue does as well public Type dequeue(){ return _list.removeFirst(); }

23 22 of 37 isEmpty() and size() As with Stacks, very simple methods; just delegate to MyLinkedList. public int size(){ return _list.size(); } public boolean isEmpty(){ return _list.isEmpty(); }

24 23 of 37 Exercise 1 (1/5) ●How can we use a stack to reverse a Linked List? ●Linked List: Cady, Aaron, Regina, Karen tail head

25 24 of 37 Exercise 1 (2/5) ●Solution: o while Linked List is not empty, remove from Linked List and push elements onto Stack o then, while stack is not empty, pop elements from stack and add to Linked List

26 25 of 37 Exercise 1 (3/5) while(!_list.isEmpty()){ stack.push(_list.removeFirst()); } Stack KarenReginaAaronCady tail head Null

27 26 of 37 Exercise 1 (4/5) Stack Cady Aaron Regina Karen tailhead while(!stack.isEmpty()){ _list.addLast(stack.pop()); } tail Null head tail

28 27 of 37 Exercise 2 (1/2) ●Check for balanced parentheses in a given string ●Balanced: [()()]{[()]} ●Not balanced: [(])

29 28 of 37 Exercise 2 (2/2) ●Go through every character, if it is a starting bracket, push it onto the stack ●If it is a closing bracket, pop from the stack ●The bracket you pop should be the opening bracket that corresponds to the closing bracket you are looking at o if it is not, return false ●If you get through every character and you haven’t returned false, check if the stack is empty ●If it is, the brackets are balanced!

30 29 of 37 Problem 2 Pseudocode for each bracket in string : if stack is empty return true if it is a starting bracket: push it onto stack if the popped character is not the matching opening bracket: return false if it is a closing bracket: pop from the stack

31 30 of 37 for each character: if it is a starting bracket: push it onto stack if it is a closing bracket: pop from the stack if the popped character is not the matching opening bracket: return false if stack is empty return true [ Stack () ] )][(

32 31 of 37 for each bracket in string: if it is a starting bracket: push it onto stack if it is a closing bracket: pop from the stack if the popped character is not the matching opening bracket: return false if stack is empty return true [ Stack ()])][(

33 32 of 37 for each character: if it is a starting bracket: push it onto stack if it is a closing bracket: pop from the stack if the popped character is not the matching opening bracket: return false if stack is empty return true [ Stack ( )] Match! Keep going… [(

34 33 of 37 for each character: if it is a starting bracket: push it onto stack if it is a closing bracket: pop from the stack if the popped character is not the matching opening bracket: return false if stack is empty return true [ Stack )] Match! Keep going… [(

35 34 of 37 for each character: if it is a starting bracket: push it onto stack if it is a closing bracket: pop from the stack if the popped character is not the matching opening bracket: return false if stack is empty return true [ Stack )][(

36 35 of 37 for each character: if it is a starting bracket: push it onto stack if it is a closing bracket: pop from the stack if the popped character is not the matching opening bracket: return false if stack is empty return true Stack )][(

37 36 of 37 Announcements DoodleJump Early handin is tomorrow at 10:00PM On Time is Sunday at 11:59PM Late is Tuesday at 11:59PM Tetris is released Sunday. o Instead of design questions, Tetris will have live design checks where a TA will ask you questions directly. We will send an email on Sunday with instructions on how to sign up for a slot. HW3 is out Tuesday. It will cover topics from this data structure and algorithms section of the course.


Download ppt "0 of 37 Stacks and Queues Lecture 17. 1 of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,"

Similar presentations


Ads by Google