Copyright © 2001-2009 Curt Hill Stacks An Useful Abstract Data Type.

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks Chapter 11.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue.
Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
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.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Topic 15 Implementing and Using Stacks
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
Stacks.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
Infix, Postfix, Prefix.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
Postfix notation. About postfix notation Postfix, or Reverse Polish Notation (RPN) is an alternative to the way we usually write arithmetic expressions.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
Lecture 11 Sept 26, 2011 Goals convert from infix to postfix.
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.
Class 4: Stacks. cis 335 Fall 2001 Barry Cohen What is a stack? n A stack is an ordered sequence of items, of which only the last (‘top’) item can be.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Stack  A stack is a linear data structure or abstract data type for collection of items, with the restriction that items can be added one at a time and.
Objectives of these slides:
Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures.
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Stack Applications.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
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.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
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 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
CHP-3 STACKS.
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.
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.
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.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Stacks Stack Abstract Data Type (ADT) Stack ADT Interface
Review Use of Stack Introduction Stack in our life Stack Operations
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.
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
Stacks.
CSC 172 DATA STRUCTURES.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
Stack application: postponing data usage
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
Infix to Postfix Conversion
Stacks and Queues 1.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Stacks.
Topic 15 Implementing and Using Stacks
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

Copyright © Curt Hill Stacks An Useful Abstract Data Type

Examples Come look at my desk Sock drawers Cafeteria plates Function data spaces –If a calls b and b calls c and c calls d –Their local variables are a stack (aka the invocation stack) Copyright © Curt Hill

Names Push down stacks –Push down lists –Push down queues Last In First Out Queue –LIFO One of several sequential ADTs Copyright © Curt Hill

Why use? Why does a cafeteria use them? Used for storage of things temporarily Things are all the same size Do not care about how long each thing is stored –Response or arrival times do not matter Easiest list to maintain All the work is at one end Copyright © Curt Hill

What Operations? Two main operations Push and Pop Push places a new item on the stack Pop removes it Others: –Determination of empty or number of items –Initialization Only the top of the stack is accessible –Top is most recently pushed item that has not been popped Copyright © Curt Hill

Example and Exercise Suppose the following operations: push 12 push 6 push 9 pop push 2 push 14 pop pop What is the resulting stack? Copyright © Curt Hill

Result Top on left After the first three pushes: After first pop: 6 12 After two more pushes: After second pop: After last pop: 6 12 Copyright © Curt Hill

Implementation Like many ADTs a stack may have several different possible implementations The underlying data structure could be –An array –A vector –Linked list Others are possible but less reasonable Each might need additional methods Copyright © Curt Hill

Interface and Implementation The same interface may exist with different implementations We will now look at an interface for an integer stack –Just the public part How this would be implemented would be in the private part and method implementations Copyright © Curt Hill

Integer Stack Code C++: class Stack { public: void push(int); int pop(); bool isEmpty(); Stack(); ~Stack(); private:... }; // Stack class Copyright © Curt Hill

Array stacks Variables: int st[STACKMAX]; int top; An additional method should be added: bool isFull(); Copyright © Curt Hill

Initialization Initialization is only setting top Set to zero –Indexes first unused item Set to -1 –Indexes first used item Which is better? The way of the C family is set to zero Other languages may choose either Copyright © Curt Hill

Push Now easy void push(int item){ if (top < STACKMAX) st[top++] = item; } // Push Error handling is also a design issue –What do we do with a stack that is full and a request for a push? Copyright © Curt Hill

Pop Also easy int pop(){ if(top>0) return st[top--]; } // Pop Again error handling needs some thought Copyright © Curt Hill

Capacity Several possibilities bool isEmpty(){ return top>0; } bool isFull(){ return top >= STACKMAX; } int size(){ return top; } Copyright © Curt Hill

Algebraic expressions Order of algebraic expressions There are several ways that an algebraic expression may be shown Everyone is familiar with infix, which is standard algebraic notation Operands surround operators –3 + 2 –5 + 3 * 4 –Requires precedence and parentheses There are also prefix and postfix Copyright © Curt Hill

The Others Prefix –Operator followed by operands –+ 3 2 –+ * Postfix –Operands followed by operator –3 2 + –5 4 3 * + –aka Reverse Polish Notation (RPN) –aka Suffix Copyright © Curt Hill

Why do I care? The older HP calculators used to use RPN Computers do not have a parenthesis, so every infix expression must be converted to postfix –The natural notation for computers Load the operands into a register and then execute the operation Copyright © Curt Hill

Conversion of infix to postfix Operators must have a priority Starting at the lowest priority this is: –end of expression –(–( –=–= –+ - (addition and subtraction) –* / (multiplication and division) –- (unary negation) Copyright © Curt Hill

Algorithm Overview Input for the infix expression Output for postfix expression Stack for operators Algorithm follows Copyright © Curt Hill

Algorithm Repeat until no more inputs What to do if you find a: –Operand Copy to output –Operator ( Push on regardless –Any operator but ) while priority(topstack) >= priority(current) write(pop()) push(current) –Operator ) Pop and write until ( found Discard ) Copyright © Curt Hill

Example –Examples 2+3*(4-5*6) (3+4*5)*(7-6) Copyright © Curt Hill

RPN calculator Evaluate an expression While not eof do For an operand push it on stack For an operator –Pop one or two things off of stack –Do the operation –Push result on stack Copyright © Curt Hill

Evaluate Infix Combining the two algorithms –Converting to RPN –Executing RPN Notes –When you write an operand then push on second stack –When you write an operator - execute it in stack fashion - pop off two, execute, push on result –When you are done the stack should contain only one item, which you pop and print Copyright © Curt Hill

Code generation for a compiler Stack machine is same as a RPN calculator Register machine (register-memory instructions) Convert to RPN Each output will be a load to a register Copyright © Curt Hill

Conclusion Stacks are the easiest to maintain linear structure All the action is at just one end –No iteration, no indexing Used as temporary storage when holding time does not matter Underlying data structure could be –Array –List –Vector Copyright © Curt Hill