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,

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks, Queues, and Linked Lists
Chapter 24 Lists, Stacks, and Queues
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Stack & Queues COP 3502.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CS Data Structures II Review COSC 2006 April 14, 2017
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks, Queues & Deques CSC212.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
TCSS 342, Winter 2005 Lecture Notes
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Stacks, Queues, and Deques
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Stacks, Queues, and Deques
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Topic 3 The Stack ADT.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
September 05 Kraemer UGA/CSCI 2720 Lists – Part I CSCI 2720 Eileen Kraemer The University of Georgia.
1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Data structures Abstract data types Java classes for Data structures and ADTs.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
QUEUES What are Queues? Creating a Queue Enqueuing and Dequeuing Testing for an Empty Queue.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
0 of 36 Andries van Dam  /05/15 Reminder: TA Hour Policies If you hit a bug, you must be able to show significant effort of fixing it on your own.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
Click to edit Master text styles Stacks Data Structure.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Stacks and Queues Chapter 4.
CC 215 Data Structures Queue ADT
Stacks and Queues.
Stack and Queue APURBO DATTA.
HW-6 Deadline Extended to April 27th
CMSC 341 Lecture 5 Stacks, Queues
Pointers and Linked Lists
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks, Queues, and Deques
Presentation transcript:

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, 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 This is an informal definition. ADT also has a mathematical definition.

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

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 */ }

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 }

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

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

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(); }

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(); }

9 of push(1)push(2)push(3)pop() push(4)pop()

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

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.

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

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

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

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

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

17 of 37 Enqueuing and Dequeuing Before Dequeuing head of queue tail of queue After Dequeuing head of queue tail of queue ●Enqueuing: adds a node ●Dequeuing: removes a node

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 }

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... }

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); }

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(); }

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(); }

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

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

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

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

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

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!

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

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 () ] )][(

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 ()])][(

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… [(

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… [(

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 )][(

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 )][(

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 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.