CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.

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.
Stacks, Queues, and Linked Lists
Problem of the Day  At what times do the minute and hour hands on an analog clock line up?
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
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.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Stacks, Queues, and Deques
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
1 Lecture 25 Abstract Data Types –II Overview  A stack Interface in Java  StackEmptyException  Stack ADT(A Simple Array-Based Implementation  Queue.
CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  also accepted,
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.
Stacks.
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Stacks1 CS2468 Data Structures and Data Management Lecturer: Lusheng Wang Office: B6422 Phone:
Stacks, Queues, and Deques
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Stacks, Queues, and Deques
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Abstract Data Type (ADT) & Stacks
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Problem of the Day  What do you get when you cross a mountain climber and a grape?
Problem of the Day  What do you get when you cross a mountain climber and a grape?
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
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.
LECTURE 24: STACK ADTS CSC 212 – Data Structures.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CSC 205 – Java Programming II Lecture 30 April 3, 2002.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
“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.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
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.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Section 3.7 Linked-Based Implementations
Sections 3.4 Formal Specification
Stacks Stacks.
CSCI 3333 Data Structures Stacks.
Stacks.
Stacks.
CSC 205 – Java Programming II
Stacks.
Stacks Abstract Data Types (ADTs) Stacks
Stacks, Queues, and Deques
CS210- Lecture 3 Jun 6, 2005 Announcements
A type is a collection of values
Presentation transcript:

CSC 212 Stacks & Queues

Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc  Why? Many students struggling with Java  This is a programming class  Only way to get better: PRACTICE

Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test them  Can make it much less work  Please, please, please use this as an excuse to practice your Java Next homework will be assigned Thursday

Abstract Data Types (ADTs) We will discuss ADTs for remainder of class ADTs do NOT specify implementation ADTs do guarantee minimum functionality for data type  Only discuss publicly available functions  What part(s) of Java could be used to do this?

Abstract Data Types (ADTs) ADTs define more than functionality  What data is stored  How the data will be handled  What errors can occur and be handled How?

Stock Trading ADT Suppose we want to design a stock trading ADT What functionality should be defined? What errors could occur? How should they be handled?

Stacks Stack is like a PEZ® dispenser: Objects get “pushed” onto stack  Objects pushed on top of stack  Objects can be pushed at any time Objects get “popped” from stack  Popping object removes it from stack  Only top object on stack can be popped

Stacks So, Stack ADT defines two vital methods:  void push(Object obj)  Object pop() Also defines other useful functionality:  int size()  returns number of elements on stack  boolean isEmpty()  returns if stack is empty  Object top()  returns top entry from Stack w/o removing it

Stacks We can define our own stack interface like this: public interface Stack { public int size();// returns elements in the stack public boolean isEmpty(); // return if the stack is empty public Object top()// return the top element throws StackEmptyException; // if the stack is empty public void push(Object obj); // push obj onto the stack public Object pop() // return and remove top stack element throws StackEmptyException; // if the stack is empty } Why do push(), pop(), and top() return objects of type Object?

Stacks Java stores “call frames” using a stack  Records parameters References for object parameters, actual values for primitive  Maintains storage space for temporary results  Also location being executed

findMin 1. public static int findMin(int[] a, int n) { 2. if (n == (a.length - 1)) 3. return a[n]; 4. else { 5. int recurMin = findMin(a, n+1); 6. if (a[n] < recurMin) 7. return a[n]; 8. else 9. return recurMin; 10. } 11. }

Array-based Stack Could implement stacks using arrays  Requiring a maximum size N for our stack (e.g., N = 1000) Stack then includes  N-element array, s,  Array index of the top element, t

Array-based Stack Pseudo-code: int size(): return t +1 boolean isEmpty(): return t < 0 Object top(): if isEmpty() then throw StackEmptyException else return s [t ]

Array-based Stack Arrays start at 0, so initialize t to -1 Should we throw any new exceptions in addition to our stack interface?

Array-based Stack More pseudo-code: push(obj): if size() = N then throw StackFullException t  t + 1 s[t]  obj Object pop(): if isEmpty() then throw StackEmptyException e  s[t ] s[t ]  null t  t -1 return e

Array-based Stack What is input size for each of these methods? What is running time for each method?

Array-based Stack Arrays make simple and efficient implementation Note: N is upper bound on stack size  If N is too small, cannot hold all items Makes stack class unusable  If N is too large, will waste memory Can lead to significant performance penalty

More group work Write code to reverse elements in an array using a stack What is the complexity of this method?