slides created by Alyssa Harding

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 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.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
TCSS 342, Winter 2005 Lecture Notes
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.
CSE 143 Lecture 7 Stacks and Queues reading: Stuart Reges notes on website slides created by Marty Stepp
Building Java Programs
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
Stack and Queue.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Stacks and Queues Introduction to Computing Science and Programming I.
CSE 143 Lecture 5 Stacks and Queues slides created by Marty Stepp
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.
Clicker questions 11/21/13 CSE 1102 Fall neighbors, (i,j) = (2,2) [0][0] [i][j]
CSE 143 Lecture 9 Interfaces; Stacks and Queues slides created by Marty Stepp
1 Stacks and Queues Starring: IndexOutOfBOundsException Co-Starring: NoSuchElementException.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
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.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Stacks (and Queues).
Sections 3.4 Formal Specification
Building Java Programs
Elementary Data Structures
QueueStack CS1020.
Stacks and Queues.
Stacks.
CSE 116/504 – Intro. To Computer Science for Majors II
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Stacks and Queues.
Building Java Programs Chapter 14
Building Java Programs
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues.
CMSC 341 Lecture 5 Stacks, Queues
Stacks and queues.
Building Java Programs Chapter 14
Stacks and Queues.
slides created by Marty Stepp
Building Java Programs
Building Java Programs
Building Java Programs Chapter 14
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Stacks and Queues CLRS, Section 10.1.
CSC 143 Stacks [Chapter 6].
COMPUTER 2430 Object Oriented Programming and Data Structures I
slides created by Marty Stepp
Based on slides by Alyssa Harding & Marty Stepp
More Data Structures (Part 1)
Stacks CS-240 Dick Steflik.
Suggested self-checks: Section 7.11 #1-11
slides created by Marty Stepp
slides created by Alyssa Harding
Lecture 8: Stacks, Queues
Generics, Stack, Queue Based on slides by Alyssa Harding
slides created by Marty Stepp
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
Stacks and Queues.
Lecture 9: Stack and Queue
Data Structures & Programming
Presentation transcript:

slides created by Alyssa Harding CSE 143 Lecture 5 Stack, Queue slides created by Alyssa Harding http://www.cs.washington.edu/143/

Stacks Stacks are a Last In First Out (LIFO) structure // add value to the top of the stack void push(E value) // remove and return the value at the top E pop() // return the size int size() // return whether the stack is empty boolean isEmpty()

Queues Queues are a First In First Out (FIFO) structure // add value to the back of the queue void enqueue(E value) // remove and return the value at the front E dequeue() // return the size int size() // return whether the queue is empty boolean isEmpty()

Stacks and Queues We’re using the Stack<E> and Queue<E> interfaces We’re using the ArrayStack<E> and LinkedQueue<E> implementations Stacks and Queues use generics, so we can store any type of Object in them

Stack and Queue client We might want to fill a Queue with values: public static Queue<Integer> fillQueue(int n) { Queue<Integer> q = new LinkedQueue<Integer>(); for (int i = 0; i < n; i++) { q.enqueue(i); } return q; } Note that the variable declaration and return value use the interface type

Stack and Queue client We might want to move values from a Queue to a Stack: public static void queueToStack( Queue<Integer> q, Stack<Integer> s) { while ( q.isEmpty() == false ) { int n = q.dequeue(); s.push(n); } } q.isEmpty() is a already a boolean expression, so this test has bad “boolean zen”

Stack and Queue client We might want to move values from a Queue to a Stack: public static void queueToStack( Queue<Integer> q, Stack<Integer> s) { while ( !q.isEmpty() ) { int n = q.dequeue(); s.push(n); } } Better!

Stack and Queue client We might want to sum the values in a Queue: public static int sum(Queue<Integer> q) { int sum = 0; while ( !q.isEmpty() ) { sum += q.dequeue(); } return sum; } But this destroys the Queue and leaves it empty!

Stack and Queue client We might want to sum the values in a Queue: public static int sum(Queue<Integer> q) { int sum = 0; for (int i = 0; i < q.size(); i++) { int n = q.dequeue(); sum += n; q.enqueue(n); } return sum; } So we store values back in the Queue and loop the correct number of times.

Stack and Queue client We might also want to fill a Stack: public static Stack<Integer> fillStack(int n) { Stack<Integer> s = new ArrayStack<Integer>(); for (int i = 0; i < n; i++) s.push(i); return s; } We can copy the Queue code and change it to use a Stack.

Stack and Queue client We might also want to move values from a Stack to a Queue: public static void stackToQueue( Stack<Integer> s, Queue<Integer> q) { while ( !s.isEmpty() ) { int n = s.pop(); q.enqueue(n); } } We can copy the Queue code and change it to use a Stack.

Stack and Queue client We might want to sum the values in a Stack: public static int sum(Stack<Integer> s) { int sum = 0; for (int i = 0; i < s.size(); i++) { int n = s.pop(); sum += n; s.push(n); } return sum; } But if we copy the Queue code, we push and pop the same value over and over.

Stack and Queue client We might want to sum the values in a Stack: public static int sum(Stack<Integer> s) { int sum = 0; Queue<Integer> q = new LinkedQueue<Integer>(); for (int i = 0; i < s.size(); i++) { int n = s.pop(); sum += n; q.enqueue(n); } queueToStack(q,s); return sum; } Even though we store values in a Queue, i increases and s.size() decreases, so we only examine half the values

Stack and Queue client We might want to sum the values in a Stack: public static int sum(Stack<Integer> s) { int sum = 0; Queue<Integer> q = new LinkedQueue<Integer>(); while ( !s.isEmpty() ) { int n = s.pop(); sum += n; q.enqueue(n); } queueToStack(q,s); return sum; } Now the sum is correct, but our resulting Stack is reversed! This happens when we transfer from a Stack to Queue and back.

Stack and Queue client We might want to sum the values in a Stack: public static int sum(Stack<Integer> s) { int sum = 0; Queue<Integer> q = new LinkedQueue<Integer>(); while ( !s.isEmpty() ) { int n = s.pop(); sum += n; q.enqueue(n); } queueToStack(q,s); stackToQueue(s,q); queueToStack(q,s); return sum; } Now both the sum and the Stack are correct.

Stack and Queue client We might want to find the minimum value in a Stack: public static int findMin(Stack<Integer> s) { int min = 0; Queue<Integer> q = new LinkedQueue<Integer>(); while ( !s.isEmpty() ) { int n = s.pop(); if ( n < min ) min = n; q.enqueue(n); } queueToStack(q,s); stackToQueue(s,q); queueToStack(q,s); return min; } What if the values in the Stack are all greater than 0? This would still return 0 as the minimum.

Stack and Queue client We might want to find the minimum value in a Stack: public static int findMin(Stack<Integer> s) { Queue<Integer> q = new LinkedQueue<Integer>(); int min = s.pop(); s.push(min); while ( !s.isEmpty() ) { int n = s.pop(); if ( n < min ) min = n; q.enqueue(n); } queueToStack(q,s); stackToQueue(s,q); queueToStack(q,s); return min; } Instead, we start our minimum at a value that is in the Stack.

Preconditions Remember the precondition of Stack’s pop method? The Stack cannot be empty when we call pop Since we call pop in our method without checking that the Stack isn’t empty, our method has this precondition too

Preconditions How are we going to deal with this? We need to return a value, but what would we return if we were passed an empty Stack? We can comment this precondition, but that doesn’t stop clients from using our method incorrectly

Exceptions Java lets us throw exceptions in our methods: throw new <exception type here>(); In this case, we want to throw an exception if the method was given an illegal argument, so we throw an IllegalArgumentException: if ( s.isEmpty() ) throw new IllegalArgumentException();

Exceptions Always comment the type and cause of any exceptions you throw: // throws IllegalArgumentException if the // stack is empty