Introduction to Stacks Chapter 2. Objectives Introduce abstract data types. Discuss implementation types. – Static – Dynamic – Contiguous Introduce the.

Slides:



Advertisements
Similar presentations
The unorganized person’s data structure
Advertisements

1 Stacks Chapter 4. 2 Objectives You will be able to: Describe a stack as an ADT. Build a dynamic-array-based implementation of stacks. Build a linked-list.
Chapter 2 INTRODUCTION TO STACKS. Outline 1. Stack Specifications 2. Implementation of Stacks 3. Application: A Desk Calculator 4. Application: Bracket.
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.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
True or false A variable of type char can hold the value 301. ( F )
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Data Structures Chapter 2 Stacks Andreas Savva. 2 Stacks A stack is a data structure in which all insertions and deletions of entries are made at one.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Object Oriented Data Structures
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
1 4. Stacks Introduction Consider the 4 problems on pp (1) Model the discard pile in a card game (2) Model a railroad switching yard (3) Parentheses.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
Templates Zhen Jiang West Chester University
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,
Stacks. 2 What Are Stacks ? PUSHPOP 0 MAX Underflow Overflow.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.
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.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
Chapter 13 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Recursive Functions for Tasks(13.1) Recursive Functions.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 14 Recursion.
Programming Principles Chapter 1. Objectives Discuss the program design process. Introduce the Game of Life. Discuss object oriented design. – Information.
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.
1 4. Stacks Introduction Consider the 4 problems on pp (1) Model the discard pile in a card game (2) Model a railroad switching yard (3) Parentheses.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Chapter 2 INTRODUCTION TO STACKS 1. Stack Specifications 2. Implementation of Stacks 3. Application: A Desk Calculator 4. Application: Bracket Matching.
Data Structures & Algorithms
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
CHP-3 STACKS.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
Fall 2006 METU EEEEE 441 S. Ece (GURAN) SCH MIDT EE 441 Data Structures Lecture 6 Stacks and Queues.
1 Data Structures CSCI 132, Spring 2016 Notes 6 Applications using Stacks.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Recursion.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
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.
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.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Stacks Stacks.
Basic Elements of C++.
Stacks Chapter 7 introduces the stack data type.
Linked Stacks and Queues
Stacks Chapter 4.
Algorithms and Data Structures
Stack.
Monday, February 26, 2018 Announcements… For Today…
Basic Elements of C++ Chapter 2.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Presentation transcript:

Introduction to Stacks Chapter 2

Objectives Introduce abstract data types. Discuss implementation types. – Static – Dynamic – Contiguous Introduce the Stack abstract data type. – Stack methods – LIFO strucutres – Stack frames Introduce enumerations and the enum keyword. Discuss public vs. private in designing a class. Introduce encapsulation. Implement a contiguous stack. Discuss applications of stacks. – System stack – RPN calculator – Matching brackets Discuss refinement process.

Homework Overview Written (max 12 points) – 2.1 E1 (a,b,c,d)(2 pts each) – 2.3 E1 (a,b)(5 pts) Programming (max 20 points) – 2.1 E2(5 pts) – 2.2 E2(15 pts) – 2.2 P2(5 pts) – 2.3 P2(5 pts) Group Project (12 points) – 2.2 E1 (a,b,c)(12 pts)

Data Structures An abstract data type is a description of the operations that a structure must support. – It does not specify how it is implemented. We may implement the structure in several different ways. – The implementations should be interchangeable. A static implementation’s size is fixed when the program compiles. – Generally faster code and easier to program – Examples: arrays, objects A dynamic implementation’s size can change. – More versatile – Examples: vector, list, etc. A contiguous implementation is one where all the data is stored at the same location in computer memory. This is necessarily static.

Stack Abstract Data Type Stack – think of a stack of books. We add or remove items at the top. This is a “last-in, first-out” (LIFO) structure. We push items onto the stack and pop items from the stack. C++ contains support for a stack container adaptor ( #include ). A stack would be useful in writing a program that reverses a list of numbers.

System Stack Your computer system uses a stack to keep track of function calls. Every time a function is called, a record representing the function (and all its local variables) is pushed onto the system stack. When the function terminates, it is popped of the stack. The top of the stack is the currently running function. LIFO is the correct model here. – When a function terminates and it is removed from the stack, the function that called it will be the new top.

STL Stacks #include using namespace std; int main() /* Pre: The user supplies an integer n and n decimal numbers. Post: The numbers are printed in reverse order. Uses: The STL class stack and its methods. */ { int n; double item; stack numbers; // declares and initializes a stack of numbers cout << "Type in an integer n followed by n decimal numbers." << endl << "The numbers will be printed in reverse order." << endl; cin >> n; for (int i = 0; i < n; i++){ cin >> item; numbers.push(item); } cout << endl << endl; while (!numbers.empty()){ cout << numbers.top() << " "; numbers.pop(); } cout << endl; }

Stack Abstract Data Type Methods supported: –push – add an entry –pop – remove an entry –top – value of the top entry –empty – is the stack empty? We can store any type of data in a stack. Templates could be useful here.

Stack Frames A stack frame is a representation of the contents of a stack at a given instant. stack operationstack frame s.push(‘A’); s.push(‘B’); s.pop();

Homework – Section 2.1 (page 56) Written – E1 (a, b, c, d) (written on paper) (2 pts each) Programming – E2 ( code) (5 pts)

Stack Header #include using namespace std; typedef char Stack_entry; // change type for different applications const int maxstack = 10; // small value for testing enum Error_code {success, overflow, underflow}; // The possible error conditions void print_error(const Error_code); /* Pre: None. Post: The value of the Error_code is printed out if it is success, overflow or underflow. */ class Stack { public: Stack(); // constructor bool empty() const; /* Pre: None. Post: If the Stack is empty, true is returned. Otherwise false is returned. */

Stack Header Error_code pop(); /* Pre: None. Post: If the Stack is not empty, the top of the Stack is removed. If the Stack is empty, an Error_code of underflow is returned.*/ Error_code top(Stack_entry &item) const; /* Pre: None. Post: If the Stack is not empty, the top of the Stack is returned in item. If the stack is empty an Error_code of underflow is returned. */ Error_code push(const Stack_entry &item); /* Pre: None. Post: If the Stack is not full, item is added to the top of the Stack. If the Stack is full, an Error_code of overflow is returned and the Stack is left unchanged. */ private: int count; Stack_entry entry[maxstack]; };

Enum Keyword The line enum Error_code {success, overflow, underflow}; creates a new data type called Error_code. Variable created using this type can only have three possible values: success, overflow, and underflow. We could just use values like 0, 1, and 2, but this makes the code more readable. We also do not need to remember that 1 means overflow.

Public Methods/Private Data Notice that the methods that modify and access the data are public. – We could change the implementation and, as long as these public methods still fulfill their described jobs, code that uses the structure should still compile and run. The data itself is private. – It can only be accessed using the public methods. – This part may change depending on the implementation.

Contiguous Stack Implementation #include "stack.h" void print_error(const Error_code err) /* Pre: None. Post: The value of the Error_code is printed out if it is success, overflow or underflow. */ { if (err == success) cout << "success"; if (err == overflow) cout << "overflow"; if (err == underflow) cout << "underflow"; } Error_code Stack::push(const Stack_entry &item) /* Pre: None. Post: If the Stack is not full, item is added to the top of the Stack. If the Stack is full, an Error_code of overflow is returned and the Stack is left unchanged. */ { Error_code outcome = success; if (count >= maxstack) outcome = overflow; else entry[count++] = item; return outcome; }

Contiguous Stack Implementation Error_code Stack::pop() /* Pre: None. Post: If the Stack is not empty, the top of the Stack is removed. If the Stack is empty, an Error_code of underflow is returned.*/ { Error_code outcome = success; if (count == 0) outcome = underflow; else --count; return outcome; } Error_code Stack::top(Stack_entry &item) const /* Pre: None. Post: If the Stack is not empty, the top of the Stack is returned in item. If the stack is empty an Error_code of underflow is returned. */ { Error_code outcome = success; if (count == 0) outcome = underflow; else item = entry[count - 1]; return outcome; }

Contiguous Stack Implementation bool Stack::empty() const /* Pre: None. Post: If the Stack is empty, true is returned. Otherwise false is returned. */ { bool outcome = true; if (count > 0) outcome = false; return outcome; } Stack::Stack() /* Pre: None. Post: The stack is initialized to be empty.*/ { count = 0; }

Encapsulation Notice that the methods are public functions with not preconditions. We do not want the code to crash or behave poorly if the methods are used in an unexpected manner. This is a form of defensive programming.

Homework – Section 2.2 (page 64) Programming – E2 ( code) (15 pts) – P2 ( code) (5 pts) Group Project – 2.2 E1 (a,b,c) ( code)(12 pts)

RPN Calculator A calculator that uses RPN (Reverse Polish Notation) maintains a stack of numbers. Numbers can be pushed directly onto the stack by the user. When the user selects a unary operator (sine, cosine, negation, etc.) the top entry is popped off the stack, the operation applied and the result push back on the stack. When the user selects a binary operation (+,-,*,/,^, etc.) the top two numbers are popped off the stack, the operation applied and the result push back onto the stack

RPN Example: Calculate ( )*5.3 The following is a series of stack frames showing the operation of RPN. Note the “top” of the stack is traditionally shown on the bottom in RPN. stack operationstack frame push 5.3 push 0.5 push 2.1 operation + operation * Note, with RPN we can avoid using parentheses. – People who spend the time getting comfortable with it and who can find and RPN calculator often prefer this convention.

RPN Program int main() /*Post: The program has executed simple arithmetic commands entered by the user. Uses: The class Stack and the functions introduction, instructions, do_command, and get_command. */ { Stack stored_numbers; introduction(); instructions(); while (do_command(get_command(), stored_numbers)); }

RPN Calculator char get_command() /* Post: Returns a command (?, =, +, -, *, /, q) entered by the user */ { char command; bool waiting = true; cout :"; while (waiting){ cin >> command; command = tolower(command); if (command == '?' || command == '=' || command == '+' || command == '-' || command == '*' || command == '/' || command == 'q') waiting = false; else { cout << "Please enter a valid command:" << endl << "[?]push to stack [=]print top" << endl << "[+][-][*][/] are arithmetic operations" << endl << "[Q]uit." << endl; } } return command; }

RPN Calculator bool do_command(char command, Stack &numbers) /* Pre: The first parameter specifies a valid calculator command. Post: The command specified by the first parameter has been applied to the Stack of numbers given by the second parameter. A result of true is returned unless command == 'q'. Uses: The class Stack. */ { double p, q; switch (command) { case '?': cout << "Enter a real number: " << flush; cin >> p; if (numbers.push(p) == overflow) cout << "Warning: Stack full, lost number" << endl; break; case '=': if (numbers.top(p) == underflow) cout << "Stack empty" << endl; else cout << p << endl; break;

RPN Calculator case '+': if (numbers.top(p) == underflow) cout << "Stack empty" << endl; else { numbers.pop(); if (numbers.top(q) == underflow) { cout << "Stack has just one entry" << endl; numbers.push(p); } else { numbers.pop(); if (numbers.push(q + p) == overflow) cout << "Warning: Stack full, lost result" << endl; } } break; // Add options for further user commands. case 'q': cout << "Calculation finished.\n"; return false; } return true; }

Homework – Section 2.3 (page 69) Written – E1 (written on paper) (5 pts) Programming – P1 (nothing to turn in) – P2 ( code) (5 pts)

Matching Brackets Another application of stacks is matching brackets in code (or English). Is the line {a=(b[0)+1];} legal? It has the matched pairs of brackets. – For every { there is a }. – For every ( there is a ). – For every [ there is a ]. Just counting the brackets is insufficient. When we encounter an opening bracket we push it onto a stack. When we encounter a closing bracket we pop the matching opening bracket off the stack. – If there is not a matching opening bracket then there is a syntax error.

Stack Abstract Data Type A stack of elements of type T is a finite sequence of elements of T, together with the following operations: 1. Create the stack, leaving it empty. 2. Test whether the stack is Empty. 3. Push a new entry onto the top of the stack, provided the stack is not full. 4. Pop the entry off the top of the stack, provided the stack is not empty. 5. Retrieve the Top entry from the stack, provided the stack is not empty.

Abstract Data Types An abstract data type allows us to think about algorithms without worrying about the details of the implementation. – We can change the implementation without changing the algorithm. It can clarify our thinking about the problem. – Using a stack is conceptually different than using a general array, even though a stack can be implemented using an array

Refinement When considering a programming problem it is helpful to use a refinement process. Abstract level – how do the data relate to each other, what operations are needed. Data structures level – what data structure and implementation should we use (e.g. we want to use a contiguous stack). Implementation level – exactly how will the methods of the stack be implemented. Application level – write the code that uses the data structure.