Stack Data Structure, Reverse Polish Notation, Homework 7

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 - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
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.
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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 CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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.
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,
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,
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Topic 15 Implementing and Using Stacks
Objectives of these slides:
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Topic 3 The Stack ADT.
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.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
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.
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
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.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover 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.
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.
For more notes and topics VISIT: IMPLEMENTATION OF STACKS eITnotes.com.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 19: Stacks and Queues (part 2)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
CHP-3 STACKS.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
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.
Data Structures Using C++ 2E
Revised based on textbook author’s notes.
MEMORY REPRESENTATION OF STACKS
Data Structures Interview / VIVA Questions and Answers
The Selection Structure
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Cinda Heeren / Geoffrey Tien
Stacks.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
Algorithms and Data Structures
PART II STACK APPLICATIONS
Stack.
CMSC 341 Lecture 5 Stacks, Queues
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Conditions and Ifs BIS1523 – Lecture 8.
CSC 1052 Stacks 1/11/2019.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks Data structure Elements added, removed from one end only
Stacks.
Topic 15 Implementing and Using Stacks
Stacks Chapter 5.
Stacks CS-240 Dick Steflik.
Stack.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
CHAPTER 3: Collections—Stacks
Presentation transcript:

Stack Data Structure, Reverse Polish Notation, Homework 7 CS 240 – Lecture 15 Stack Data Structure, Reverse Polish Notation, Homework 7

Data Structure – Stack You should already have somewhat of an idea of what a stack is from CS 210 and our discussion of the memory stack. Formally, a stack is a data structure in which elements are stored in a logical pile where the only accessible member is the top element. The basic implementation of a stack exposes three functions: pop, remove and receive the top element of the stack. push, place a new element on top of the stack. empty, checks whether or not the underlying pile has no elements remaining.

Data Structure – Stack Implementation To implement the stack, we need to reserve some memory for where we're going to put elements. We're going to assume a stack of integers, but the concepts going forward will apply to all types. int pile[STACK_LIMIT]; An array is the most natural way to allocate memory for multiple objects. We'll also need to keep track of where the top of the stack is, so we'll need an index for it. int stacktop = 0; this stack has a maximum capacity

Stack – push Implementation Now that we have an underlying pile, we can start implementing the basic functionality of the stack. The push function needs to take a element as an argument so that it can add it to the stack. push also needs to indicate whether or not it succeeds as a Boolean. Why? If the stack is at maximum capacity, the code needs to know that the function failed to do what it was supposed to. int push(int element) { if (stacktop == STACK_LIMIT) return 0; pile[stacktop++] = element; return 1; }

Stack – pop Implementation Now that we can store elements in the stack, we need a way for retrieving them. The pop function doesn't need an argument since we're taking from the data structure, rather than giving it. Also, we somehow need to receive a value from the pop function to indicate the element we've taken from it. int pop() { return pile[--stacktop]; } What's wrong with this?

Stack – pop Alternate Implementation Since pop can also fail if the stack is empty, we need to implement it in a way that it can return a Boolean indicating success or failure. But since we need to receive a value from it, we'll need to pass a variable by reference so that pop can set the value for us. int pop(int* element) { if (empty()) return 0; *element = pile[--stacktop]; return 1; } This implementation allows us to determine when pop has failed and prevent stacktop from indexing memory that it shouldn't.

Stack – empty Implementation We actually used the implementation of empty in the better definition of pop. The role of empty is simply to check the state of the stack: is it empty? Since there's no way this function can fail, its return value is all we need. int empty() { return stacktop == 0; }

Stack – peek Implementation Many implementations of the stack include a peek functionality. Peeking a stack is the act of looking at the top element but not removing it. We can implement this using similar code to pop just by not changing the top position of the stack. int peek(int* element) { if (empty()) return 0; *element = pile[stacktop - 1]; return 1; } Alternatively, you can implement this function using calls to pop and push.

Stack – Other Implementations We now have a fully-coded implementation of a stack in C. However, the way we've implemented the stack in the slides so far has a few downsides. The stack is always the same size. This takes up constant, large (relatively) amounts of memory. The stack is finite in size. Even though you have more memory on your machine, the stack cannot store more data than your implementation-specified limit. There are other ways to implement the stack that make use of dynamic memory allocation techniques. We'll discuss those next class.

Reverse Polish Notation – Postfix Notation Reverse Polish Notation or more commonly postfix notation is a way of ordering arithmetic expressions. In C, we use what are called infix notation, which is when the operator comes between the operands. We worked with this briefly in Homework 6. Postfix notation presents operands before the operator, and as a result uniquely determines the order of operations by the expression alone. 2 x * 3 y * + Infix notation requires either parenthesis or a well understood order of operations (PEMDAS). (2 * x) + (3 * y) + 2 + 3 + 4 * 5 / 4 + 4

Reverse Polish Evaluation Algorithm Here is the pseudocode for evaluating a Reverse Polish expression: for each token in the expression if it's an operand store it if it's an operator if there aren’t two operands, error remove the last two operands you stored apply the operator to them store the result if it's neither, it's an error. if there's more or less than one value left in the stack, it's an error otherwise, the remaining value is the result for the entire expression

Reverse Polish Evaluation Algorithm In the algorithm, we only ever use the most recently stored values when doing the operator evaluations. This means that a stack data structure looks like our top choice for storing operands as we go. Storing operands will be done with push. Extracting operands to use with the operators will be done with a pull. Operators don't go onto the stack. Checking if there's only one element on the stack is a little more difficult. Either you can implement the stack with an additional size function. Or you can pop and element and then check if the stack is empty.

Reverse Polish Notation Example Input: 123 21 + 567 432 - * As Algebraic: (123 + 21) * (567 - 432) Stack States: 123 21 144 567 432 135 19440 Empty 123 144 567 144 144

Homework 7 – Building a Postfix Calculator While the pseudocode for the Postfix calculator all fits on one slide, for Homework 7, we're going to implement it as a large project. There's going to be a few separate modules where you're going to implement the functionality of the calculator: The calc module This will be responsible for taking elements from the stack and applying an operator to them. Similar to what we did in Homework 6. The stack module This will be responsible for storing the stack functionality that will hold our operands. The io module This will be responsible for pulling operands and operators from standard input. The main module This is the code that facilitates running the overall program.

Homework 7 – calc Module The calc module will have one main function: eval. void eval(char); This function should take as an argument an operator and calculates the result of that operator on the last two elements of the stack. Then puts the result back on the stack. To use of the stacks functions, make sure to use the extern keywork in the calc module to declare the needed external functions. Since this function should work for any of our supported operators. Make use of a switch like in Homework 6. In addition to the operators included in Homework 6, also include the unary not operator (~).

Homework 7 – stack Module The stack module should include the necessary parts of the stack data structure. It should have at least the basic functionality (push, pop, empty) implemented. Since those functions depend on a global pile array and global stack top index, those should also be declared and initialized. However, it's important to protect these global variables from other modules. Use the static keyword to make the pile array and the stack top index invisible to other modules.

Homework 7 – io Module The io module is responsible for handling IO. This module is written for you, for the most part. It contains three functions: parseoperator Extracts an operator from input through call-by-reference. If it fails, returns false. Otherwise returns true. parseoperand Extracts an operand from input through call-by-reference. endofstream Checks if the end of input has been reached. If you feel the need to add anymore functions related to output, add it here.

Homework 7 – main Module The main module is the primary driver of the whole application. In the main function is where you'll be implementing the general algorithm. while the io module says we're not at end of stream if trying to read an operand from the io module works push it in the stack module if trying to read an operator from the io module works evaluate it with the calc module if it's neither worked but it’s not end of file, it's an error. if the stack module only has one element in it, print the result otherwise, print an error.