Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.

Slides:



Advertisements
Similar presentations
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Advertisements

Stacks Chapter 11.
COSC 2006 Chapter 7 Stacks III
COMPSCI 105 S Principles of Computer Science 13 Stacks.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
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
Stacks.
16-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
28-Jun-15 Recursion. 2 Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example:
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Topic 15 Implementing and Using Stacks
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures 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
19-Aug-15 Simple Recursive Algorithms. 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking.
Topic 3 The Stack ADT.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
1 CSC 222: Computer Programming II Spring 2005 Stacks and recursion  stack ADT  push, pop, peek, empty, size  ArrayList-based implementation, java.util.Stack.
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.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
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)
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
Stacks  Introduction  Applications  Implementations  Complex Applications.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
A grammar for arithmetic expressions involving the four basic operators and parenthesized expressions. Parenthesized expressions have the highest precedence.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately.
Data Structures & Algorithms
CHP-3 STACKS.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
CSC 205 – Java Programming II Lecture 30 April 3, 2002.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
CS162 - Topic #10 Lecture: Recursion –The Nature of Recursion –Tracing a Recursive Function –Work through Examples of Recursion Programming Project –Discuss.
Click to edit Master text styles Stacks Data Structure.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
BCA II Data Structure Using C
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 Chapter 6.
Stacks Stacks.
MEMORY REPRESENTATION OF STACKS
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Stacks.
Stacks Chapter 4.
Stack.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks.
Topic 15 Implementing and Using Stacks
Stacks Chapter 5.
CS210- Lecture 3 Jun 6, 2005 Announcements
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Stacks.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Stacks

What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed from the stack is taken from the “top” of the stack Things are removed in the reverse order from that in which they were inserted

Abstract Data Types An Abstract Data Type (ADT) is a “thing” consisting of: –Some kind of data –All the operations defined for working with that data An ADT is abstract in the sense that nothing is said about how the ADT is implemented The advantages of using an ADT are: –Simplicity: The user does not know, and does not need to know, how the ADT is implemented –Flexibility: The author of the ADT is free to improve the implementation, so long as existing operations are unchanged –Efficiency: By not providing certain operations, the ADT may be made much more efficient What if the ADT doesn’t have the operations you need? –Maybe you are using the wrong ADT –Maybe the definition of the ADT should be expanded

Stack operations Create an empty stack stack.push(object) –Adds the object to the top of the stack; the item pushed is also returned as the value of push object = stack.pop(); –Removes the object at the top of the stack and returns it object = stack.peek(); –Returns the top object of the stack but does not remove it from the stack stack.empty() –Returns true if there is nothing in the stack

Some uses of stacks Stacks are used for: –Any sort of nesting (such as parentheses) –Evaluating arithmetic expressions (and other sorts of expression) –Implementing function or method calls –Keeping track of previous choices (as in backtracking) –Keeping track of choices yet to be made

A balancing act ( [ ] ( { ( ) } [ ( ) ] ) ) is balanced; ( [ ] ( { ( ) } [ ( ) ) ] ) is not Simple counting is not enough to check balance You can do it with a stack: going left to right, –If you see a (, [, or {, push it on the stack –If you see a ), ], or }, pop the stack and check whether you popped the corresponding (, [, or { –When you reach the end, check that the stack is empty

Expression evaluation Almost all higher-level languages let you evaluate expressions, such as 3*x+y or m=m+1 The simplest case of an expression is one number (such as 3 ) or one variable name (such as x ) –These are expressions In many languages (including Java), = is considered to be an operator –Its value is (typically) the value of the left-hand side, after the assignment has occurred –Hence, statements such as if((x = a + b) > 0)... are legal You may occasionally need to evaluate expressions in your own program, without benefit of a compiler

Performing calculations To evaluate an expression, such as 1+2*3+4, you need two stacks: one for operands (numbers), the other for operators: going left to right, –If you see a number, push it on the number stack –If you see an operator, While the top of the operator stack holds an operator of equal or higher precedence: –pop the old operator –pop the top two values from the number stack and apply the old operator to them –push the result on the number stack push the new operator on the operator stack –At the end, perform any remaining operations

Example: 1+2*3+4 1 : push 1 on number stack + : push + on op stack 2 : push 2 on number stack * : because * has higher precedence than +, push * onto op stack 3 : push 3 onto number stack + : because + has lower precedence than * : – pop 3, 2, and * –compute 2*3=6, and push 6 onto number stack –push + onto op stack 4 : push 4 onto number stack end : pop 4, 6 and +, compute 6+4=10, push 10 ; pop 10, 1, and +, compute 1+10=11, push (at the top of the stack) is the answer

Handling parentheses When you see a left parenthesis, (, treat it as a low-priority operator, and just put it on the operator stack When you see a right parenthesis, ), perform all the operations on the operator stack until you reach the corresponding left parenthesis; then remove the left parenthesis

Handling variables There are two ways to handle variables in an expression: –When you encounter the variable, look up its value, and put its value on the operand (number) stack This simplifies working with the stack, since everything on it is a number –When you encounter a variable, put the variable itself on the stack; only look up its value later, when you need it This allows you to have embedded assignments, such as 12 + (x = 5) * x

Handling the = operator The assignment operator is just another operator –It has a lower precedence than the arithmetic operators –It should have a higher precedence than ( To evaluate the = operator: –Evaluate the right-hand side (this will already have been done, if = has a low precedence) –Store the value of the right-hand side into the variable on the left-hand side You can only do this if your stack contains variables as well as numbers –Push the value onto the stack

Associativity Most operators, including arithmetic +, -, *, and / are left-associative: leftmost operators are done first –For example, means (10-5)-2, not 10-(5-2) Some operators, including = and exponentiation, are right-associative: rightmost operators are done first –For example, x=y=z+1 means x=(y=z+1), not (x=y)=z+1

Evaluating associative operators Left-associative operators, such as + and / : –Don’t stack a left associative operator on top of an operator with the same precedence –For example, don’t push a + on top of a - –Instead, pop and apply the old operator before putting the new operator on the stack Right-associative operators, such as = : –Put the new operator on the stack, if the current top-of- stack operator has the same or lower precedence As always, never push an operator on top of a higher-precedence operator

Some things that can go wrong The expression may be ill-formed: When you go to evaluate the second +, there won’t be two numbers on the stack When you are done evaluating the expression, you have more than one number on the stack (2 + 3 You have an unmatched ( on the stack 2 + 3) You can’t find a matching ( on the stack The expression may use a variable that has not been assigned a value

Stacks in Java Stacks are used for local variables (including parameters) void methodA() { int x, y; // puts x, y on stack y = 0; methodB(); y++; } void methodB() { int y, z; // puts y, z on stack y = 5; return; // removes y, z } x y y z

Supporting recursion static int factorial(int n) { if (n <= 1) return 1; else return n * factorial(n - 1); } If you call x = factorial(3), this enters the factorial method with n=3 on the stack | factorial calls itself, putting n=2 on the stack | | factorial calls itself, putting n=1 on the stack | | factorial returns 1 | factorial has n=2, computes and returns 2*1 = 2 factorial has n=3, computes and returns 3*2 = 6

Factorial (animation 1) x = factorial(3) static int factorial(int n) { //n=3 int r = 1; if (n <= 1) return r; else { r = n * factorial(n - 1); return r; } } n=3 r=1 r is put on stack with value 1 All references to r use this r All references to n use this n 3 is put on stack as n Now we recur with 2...

Factorial (animation 2) r = n * factorial(n - 1); static int factorial(int n) {//n=2 int r = 1; if (n <= 1) return r; else { r = n * factorial(n - 1); return r; } } n=3 r=1 r is put on stack with value 1 Now using this r And this n Now we recur with 1... n=2 r=1 2 is put on stack as n

Factorial (animation 3) r = n * factorial(n - 1); static int factorial(int n) { int r = 1; if (n <= 1) return r; else { r = n * factorial(n - 1); return r; } } n=3 r=1 r is put on stack with value 1 Now using this r And this n n=2 r=1 n=1 r=1 1 is put on stack as n Now we pop r and n off the stack and return 1 as factorial(1)

Factorial (animation 4) r = n * factorial(n - 1); static int factorial(int n) { int r = 1; if (n <= 1) return r; else { r = n * factorial(n - 1); return r; } } n=3 r=1 Now using this r And this n Now we pop r and n off the stack and return 1 as factorial(1) n=2 r=1 n=1 r=1 n=1 1

Factorial (animation 5) r = n * factorial(n - 1); static int factorial(int n) { int r = 1; if (n <= 1) return r; else { r = n * factorial(n - 1); return r; } } n=3 r=1 Now using this r And this n n=2 r=1 2 * 1 is 2 ; Pop r and n; Return 2 r=1 n=2 2 1

Factorial (animation 6) x = factorial(3) static int factorial(int n) { int r = 1; if (n <= 1) return r; else { r = n * factorial(n - 1); return r; } } n=3 r=1 Now using this r And this n 3 * 2 is 6 ; Pop r and n; Return 6 2 r=1 n=3 6

Summary Stacks are useful for working with any nested structure, such as: –Arithmetic expressions –Nested statements in a programming language –Any sort of nested data

The End