5.3 Implementing a Stack Chapter 5 – The Stack.

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

The unorganized person’s data structure
CS 240Chapter 6 - StacksPage 21 Chapter 6 Stacks The stack abstract data type is essentially a list using the LIFO (last-in-first-out) policy for adding.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
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.
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
CHAPTER 3 Stacks. Chapter Objectives  To learn about the stack data type and how to use its four methods:  push  pop  peek  empty  To understand.
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 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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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,
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 16 Stacks and Queues Saurav Karmakar Spring 2007.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Chapter 3 Stacks.
CHAPTER 3 Stacks MIDTERM OCTOBER 17 IN LAB. Chapter Objectives  To learn about the stack data type and how to use its four methods:  push  pop  peek.
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.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
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.
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,
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.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
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.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
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.
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.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
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.
Stacks Chapter 5.
MIDTERM OCTOBER 17 IN LAB Chapter 3 Stacks.
Homework 4 questions???.
Stacks Chapter 7 introduces the stack data type.
Stacks.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
Stack.
Chapter 5 Stacks.
Wednesday, February 28, 2018 Announcements… For Today…
Pointers and Linked Lists
Stacks.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
5.1 The Stack Abstract Data Type
5.4 Additional Stack Applications
MIDTERM OCTOBER 11 IN LAB Chapter 3 Stacks.
Stack and Queues Stack implementation using Array
Stacks and Queues 1.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks.
Jordi Cortadella and Jordi Petit Department of Computer Science
Stacks Chapter 5.
Chapter 6 – Queues and Deques
Abstract Data Types Stacks CSCI 240
5.1 The Stack Abstract Data Type
LINEAR DATA STRUCTURES
Presentation transcript:

5.3 Implementing a Stack Chapter 5 – The Stack

Attendance Quiz #15 Stacks

Attendance Quiz #16 Stacks

Tip #17: KISS is Better! What does the following code fragment do? Stacks What does the following code fragment do? a ^= b; b ^= a; You're right! Swaps a and b. REALLY fast. Generic. No temp. BUT??? Is it worth the obfuscation? int temp = b; b = a; a = temp; More often than not, clarity and readability are better than cleverness and pretentious display of ability.

5.3, pgs. 325-331 5.3 Implementing a Stack Adapter Classes and the Delegation Pattern Revisiting the Definition File stack.h Implementing a Stack as a Linked Data Structure Comparison of Stack Implementations 5.3, pgs. 325-331

The Stack ADT A stack is similar to a restricted list. Stacks A stack is similar to a restricted list. The standard library defines the stack as a template class that takes any of the sequential containers (vector, list, or deque. Implementations use sequential containers: A contiguous container (such as a vector), A linked container (such as special-purpose single-linked list.) Stack has-a container

vector<Item_Type> for stack<Item_Type> Vector Stack Stacks vector<Item_Type> for stack<Item_Type> #ifndef MY_STACK_H_ #define MY_STACK_H_ #include <vector> template <typename T> class Stack { private: std::vector<T> myStack; public: /** Construct an empty stack */ Stack() {} /** Push item on stack */ void push(const T& item) { myStack.push_back(item); } /** Return top of stack */ T& top() { return myStack.back(); } /** Pop top of stack */ void pop() { myStack.pop_back(); } /** Return TRUE if stack is empty */ bool empty() const { return myStack.size() == 0; } /** Return number of stack entries */ size_t size() const { myStack.size(); } }; #endif // MY_STACK_H_ All sequential containers provide the functions empty, size, back (equivalent to stack::top), push_back (equivalent to stack::push) and pop_back (equivalent to stack::pop), so we could use any of these containers. A stack is an adapter class because it adapts the functions available in another class to the interface its clients expect by giving different names to essentially the same operations. The stack class uses the delegation pattern by making the functions in the underlying container class do its work..

Linked List Stack Stacks We can also implement a stack using a linked list of nodes push() inserts a node at the head of the list. top_of_stack = new Node(item, top_of_stack); top_of_stack->next references the old top of stack top() returns top_of_stack->data. pop() resets top_of_stack to the value stored in the next field of the list head and then deletes the old top of the stack (pointed to by old_top). When the stack is empty, top_of_stack == NULL.

list<Item_Type> for stack<Item_Type> Linked List Stack Stacks list<Item_Type> for stack<Item_Type> #ifndef MY_STACK_H_ #define MY_STACK_H_ #include <list> template <typename T> class Stack { private: std::list<T> myStack; public: /** Construct an empty stack */ Stack() {} /** Push item on stack */ void push(const T& item) { myStack.push_back(item); } /** Return top of stack */ T& top() { return myStack.back(); } /** Pop top of stack */ void pop() { myStack.pop_back(); } /** Return TRUE if stack is empty */ bool empty() const { return myStack.empty(); } /** Return number of stack entries */ size_t size() const { myStack.size(); } }; #endif // MY_STACK_H_ A stack is an adapter class for linked lists because it adapts the functions available in another class to the interface its clients expect by giving different names to essentially the same operations.

Comparing Stack Implementations Stacks The linked list implementation is similar to the implementation provided by the C++ standard library. By delegating the operations to an underlying sequential container, we avoid having to implement these operations ourselves. By using the functions push_back, pop_back, and back, we are assured of constant time (O(1)) performance, since the C++ standard requires that implementations of the sequential containers provide these operations in (amortized) constant time. Making a copy of the stacked item (or reference) is not considered – probably a wash. All stack operations using a sequential container (such as a vector, linked list, or deque) are O(1).

Comparing Stack Implementations Stacks Use of the standard containers has a space penalty. To achieve the amortized constant performance for push_back, and so forth, the vector and deque allocate additional space. The vector will allocate an array twice the current size whenever it needs to allocate additional space. (The deque also allocates additional space.) While using a linked data structure has the advantage of using exactly as much storage as is needed for the stack, you do need to allocate storage for the links. The standard list class is a double-linked list; thus each Node contains pointers to both a next and a previous entry (not needed for the stack.) Because all insertions and deletions are at one end, the flexibility provided by a double-linked data structure is under-utilized.

Stack Errors Stack underflow. Stack overflow. Stack error handling. Stacks Stack underflow. Pop(), Top() Stack overflow. Push() Stack error handling. Try-catch throw correct errors Testing Equivalence Classes Test top, pop, push, empty for each: Empty stack Stack with one element Stack with some elements, probably call pop/push twice or more often Stack full Push N distinct elements, pop N times and ensure the same elements are retrieved in reverse order

5.4, pgs. 332-347 5.4 Additional Stack Applications Case Study: Evaluating Postfix Expressions Case Study: Converting from Infix to Postfix Case Study: Part 2: Converting Expressions with Parentheses 5.4, pgs. 332-347

Postfix and infix notation Stacks Postfix and infix notation Expressions normally are written in infix form, but Easier for a computer to evaluate an expression in postfix form since No need to group sub-expressions in parentheses or Worry about operator precedence. Infix Expression Postfix Expression Value 4 * 7 4 7 * 28 4 * (7 + 2) 4 7 2 + * 36 (4 * 7) - 20 4 7 * 20 - 8 3 + ((4 * 7) / 2) 3 4 7 * 2 / + 17 Evaluate postfix expressions by Saving operands on a stack until their associated operator is scanned, Pop the operands and compute the result, Push result back onto the stack.

Evaluating Postfix Expressions Stacks 4 4 7 * 20 - 1. Empty the operand stack 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 4

Evaluating Postfix Expressions Stacks 4 4 7 7 * 20 - 1. Empty the operand stack 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 7 4

Evaluating Postfix Expressions Stacks * 4 4 7 7 * 20 - 1. Empty the operand stack 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 7 4

Evaluating Postfix Expressions Stacks 28 4 4 7 7 * 20 - 1. Empty the operand stack 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result

Evaluating Postfix Expressions Stacks 4 4 7 7 * 20 20 - 1. Empty the operand stack 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 28

Evaluating Postfix Expressions Stacks 4 4 7 7 * 20 20 - 1. Empty the operand stack 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 28

Evaluating Postfix Expressions Stacks - 4 4 7 7 * 20 - 20 28 1. Empty the operand stack 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 20 28

Evaluating Postfix Expressions Stacks 8 4 4 7 7 * 20 - 1. Empty the operand stack 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 8

Evaluating Postfix Expressions Stacks = 4 4 7 7 * 20 - 1. Empty the operand stack 2. while there are more tokens 3. get the next token 4. if the first character of the token is a digit 5. push the token on the stack 6. else if the token is an operator 7. pop the right operand off the stack 8. pop the left operand off the stack 9. evaluate the operation 10. push the result onto the stack 11. pop the stack and return the result 8

Evaluating Postfix Expressions Stacks Test driver: creates a Postfix_Evaluator object reads one or more expressions and reports the result catches the Syntax_Error exception. exercises each path by using each operator exercises each path through eval by trying different orderings and multiple occurrences of operators Tests for syntax errors: an operator without any operands a single operand an extra operand an extra operator a variable name the empty string

Evaluate the following postfix expressions: 2 3 6 * + 9 – 4 5 + 3 * 7 - 1 2 3 4 + + + 2 8 + 7 3 % * 3 6 + 1 + 2 5 * 4 + 8 7 + * = __________