Stack A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - The last (most recent) item inserted,

Slides:



Advertisements
Similar presentations
A queue is a linear, homogeneous, container that stores and dispenses its content in a FIFO manner. FIFO - First In First Out The first (most distant)
Advertisements

COMPSCI 105 S Principles of Computer Science 13 Stacks.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Topic 15 Implementing and Using Stacks
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
Stacks CS 308 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Topic 15 Implementing and Using Stacks
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures.
1 CSC 222: Computer Programming II Spring 2005 Stacks and recursion  stack ADT  push, pop, peek, empty, size  ArrayList-based implementation, java.util.Stack.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CSE 143 Lecture 5 Stacks and Queues slides created by Marty Stepp
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
6 Stack ADTs  Stack concepts  Stack applications  Stack ADTs: requirements, contracts  Implementations of stacks: using arrays and linked-lists  Stacks.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
A queue is a linear, homogeneous, container that stores and dispenses its content in a FIFO manner. FIFO - Fast In First Out The first (most distant) item.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
Click to edit Master text styles Stacks Data Structure.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
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.
Programming Abstractions
Stacks (and Queues).
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Stacks and Queues.
Revised based on textbook author’s notes.
Stack as an ADT.
Stack A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - The last (most recent) item inserted,
Stack A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - The last (most recent) item inserted,
Stacks.
Cinda Heeren / Geoffrey Tien
CSE 373: Data Structures and Algorithms
Stacks Chapter 4.
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
Stacks and Queues.
Building Java Programs Chapter 14
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues.
Building Java Programs
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
STACK By:- Rajendra ShakyawalP.G.T. Computer Science KV-No.1, AFS, Tambaram, Chennai.
Stacks and Queues.
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
Building Java Programs
Building Java Programs
Stacks and Queues CLRS, Section 10.1.
CSC 143 Stacks [Chapter 6].
COMPUTER 2430 Object Oriented Programming and Data Structures I
Topic 15 Implementing and Using Stacks
Introduction to Stacks
Introduction to Stacks
Stacks and Queues.
Presentation transcript:

Stack A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - The last (most recent) item inserted, and not yet removed, will be the first (next) item dispensed. Real World Examples a spring-loaded cafeteria tray dispenser.  a driveway wide that is one-car wide The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

Stack Stack Operations An Abstract Picture push insert a new item into the stack pop remove one item from the container top inspect one item from the container, but don’t remove it An Abstract Picture The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

Stack - a Better Abstraction (i.e. the class specification) Domain sequence of ItemType Constructor public Stack() post: this == sequence{} Query methods public boolean isEmpty() post: result == (this->size() == 0) public ItemType top() pre: ! isEmpty() post: result == this->first() Update methods public void push(ItemType z) post: this == this@pre->prepend(z) public void pop() pre: ! isEmpty() (throws java.util.EmptyStackException) post: this == this@pre->subSequence( 2, this@pre->size() ) Stack<ItemType> The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

Stack<ItemType> Example Stack<ItemType> + Stack() + boolean isEmpty() + void push(ItemType z) + void pop() + ItemType top() Stack<String> s1, s2; String str; s1 = new Stack<String>(); s1.push( “humble” ); s1.push( “bumble” ); s1.push( “mumble” ); s1.pop(); s1.push( “tumble” ); str = (String) s1.top(); s1.push( “rumble” ); s1.push( “crumble” ); s1.push( s1.top() ); s2 = new Stack(); while ( !s1.isEmpty() ) { s2.push( s1.top() ); } The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

Stack Implementations How would you represent ... ... a bounded stack? ... an unbounded stack? Would you use inheritance or aggregation? The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

public class Stack<ItemType> /** Class Invariant * theArray.length > 0 and 0 <= topNdx <= theArray.length * informally: theArray stores the items of the stack and topNdx-1 is the index * of the top of the stack while 0 is the index of the stack’s bottom */ public class Stack<ItemType> private Object[] theArray; private int topNdx; /** pre: s > 0 * post: theArray.length == s * and topNdx == 0*/ public Stack(int s) { theArray = new Object[s]; topNdx = 0; } /** post: result == (topNdx==0) */ public boolean isEmpty() { return topNdx == 0; //continued … The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

public void pop() { public Object top() { topNdx--; /** pre: topNdx < theArray.length * post: topNdx == topNdx@pre + 1 * and theArray[topNdx-1] == z */ public void push(ItemType z) { theArray[topNdx] = z; topNdx++; } /** pre: topNdx > 0 * post: topNdx == topNdx@pre - 1 */ public void pop() { topNdx--; * post: result == theArray[topNdx-1] */ public Object top() { return theArray[topNdx-1]; The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

Stack Applications Each method call causes an activation record (local variables & parameters) to be pushed upon the run-time stack. When a method returns its activation record is popped off the run-time stack. Each time the compiler encounters a { it is pushed upon the parse stack. Each time the compiler encounters a } it is pops the matching } from the parse stack. The finite state machine can be augmented by adding a stack. Such a device is called a push-down automaton. The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

Evaluating RPN Expressions Stacks are useful for evaluating many kinds of expressions. Reverse Polish notation In Reverse Polish Notation (RPN) operands occur before their associated operator, and operators occur as soon as possible. (Also called _____________.) Rules: 1) Operands occur left-to-right in the same order as infix expression 2) Operation follows immediately after its operand/subexpression Examples Infix RPN 7 - 3 7, 3, - 8 * 4 + 5 1 + (2*3) - 4 ((9+8)*7)-(6*(5+(4/3))) The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

RPN Evaluation Algorithm Scan expression left to right, and… For each operand - push For each operator - replace two top operands by their subexpression value (Note: top of stack is right operand, second in stack is left.) Example 9, 8, +, 7, *, 6, 5, 4, 3, /, +, *, - OperandStack The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.