Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS 240 35.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Sample PMT online… Browse 1120/sumII05/PMT/2004_1/ 1120/sumII05/PMT/2004_1/
Stacks Chapter 11.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
CS 240Chapter 7 - QueuesPage 29 Chapter 7 Queues The queue abstract data type is essentially a list using the FIFO (first-in-first-out) policy for adding.
Chapter 7: Queues QUEUE IMPLEMENTATIONS QUEUE APPLICATIONS CS
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.
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.
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
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.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
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 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 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,
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
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.
Chapter 7 Stacks II CS Data Structures I COSC 2006
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.
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,
Chapter 7 Stacks Dr. Youssef Harrath
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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)
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.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
Data Structures – Week #3 Stacks. 9.Mart.2012Borahan Tümer, Ph.D.2 Outline Stacks Operations on Stacks Array Implementation of Stacks Linked List Implementation.
Chapter 3: Lists, Stacks, and Queues Abstract Data Types Lists and Sorted Lists CS 340 Page 34 Stacks and Stack Applications Queues and Queue Applications.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
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.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Data Structures Using C++1 Chapter 7 Stacks. Data Structures Using C++2 Chapter Objectives Learn about stacks Examine various stack operations Learn how.
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.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
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.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Data Structures Using C++ 2E
Homework 4 questions???.
Stacks.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
Data Structures – Week #3
Visit for more Learning Resources
Chapter 16-2 Linked Structures
Stacks Chapter 5 Adapted from Pearson Education, Inc.
5.4 Additional Stack Applications
Stacks CS-240 Dick Steflik.
Data Structures – Week #3
5.3 Implementing a Stack Chapter 5 – The Stack.
Presentation transcript:

Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS

CS The stack abstract data type is essentially a list using the LIFO (last-in-first-out) policy for adding and removing elements. The principal stack operations: Create an empty stack. Copy an existing stack. Destroy a stack. Determine whether a stack is empty. Add a new element to a stack. Remove the most recently added element from a stack. Retrieve the most recently added element from a stack.

CS When running a program that uses functions, a stack is used to keep track of the function calls, including the status of the variables in each function. Stack Application: Run-Time Stack void swap(int &u, int &v) { int temp = u; u = v; v = temp; } void swap(int &u, int &v) { int temp = u; u = v; v = temp; } void reorder(int &a, int &b, int &c) { if ((b <= a) && (b <= c)) swap(a,b); else if ((c <= a) && (c <= b)) swap(a,c); if (c <= b) swap(b,c); } void reorder(int &a, int &b, int &c) { if ((b <= a) && (b <= c)) swap(a,b); else if ((c <= a) && (c <= b)) swap(a,c); if (c <= b) swap(b,c); } a: 20 b: 30 c: 10 void main() { int x = 20; int y = 30; int z = 10; if ((x > y) || (y > z)) reorder(x,y,z); cout << x << y << z; } void main() { int x = 20; int y = 30; int z = 10; if ((x > y) || (y > z)) reorder(x,y,z); cout << x << y << z; } u: 20 v: 10 temp:20 x: 20 y: 30 z: 10 u: 10 v: 20 a: 10 c: 20 x: 10 z: 20 void swap(int &u, int &v) { int temp = u; u = v; v = temp; } void swap(int &u, int &v) { int temp = u; u = v; v = temp; } u: 30 v: 20 temp:30 u: 20 v: 30 b: 20 y: 20 c: 30 z: 30

CS Stack Application: Infix-to-Postfix Conversion Following these rules, then, the infix expression * ( / 2 ) is converted into the postfix expression / + * - When this is found in the infix expression... … do this! Beginning of infix expression Push a # onto the stack Operand Append it to the postfix expression Right parenthesis Repeatedly pop the stack, appending each entry to the postfix expression, until a left parenthesis is popped (but not output) End of infix expression Repeatedly pop the stack, appending each entry to the postfix expression Left parenthesis Push it onto the stack * or / operator Repeatedly pop the stack, appending all popped * and / operators to the end of the postfix expression, until something else is popped; push this last item back onto the stack, followed by the new * or / that was encountered + or - operator Repeatedly pop the stack, appending all popped +, -, *, and / operators to the end of the postfix expression, until something else is popped; push this last item back onto the stack, followed by the new + or - that was encountered

CS Stack Application: Postfix Expression Evaluation Following these rules, then, with the postfix expression / + * - yields: When this is encountered in the postfix expression... … do this! Operand Push it onto the stack Operator Pop the stack twice, perform the operation on the two popped operands, and push the result back onto the stack End of postfix expression Pop the stack once; the popped value is the final result

CS Stack Application: Graphical Transformations When graphically manipulating 2D and 3D objects, it’s often convenient to use a stack to manipulate them at the origin and then translate them to their appropriate locations. translate rotate scale translate rotate scale translate rotate translate By carefully applying the transformations in the correct order (via the stack), the image is altered in the desired fashion. translate scale rotate translate

CS Stack Implementation Alternatives An Array Implementation Positives Avoids pointers (uses top index) Trivial implementation  Negatives × Size must be declared in advance A Linked List Implementation Positives Dynamically allocates exactly the right amount of memory Straightforward (if not quite trivial) implementation  Negatives × Those wonderful pointers           

CS Linked List Implementation of Stack // Class declaration file: stack.h // Linked List implementation of the // stack ADT – inherits from LinkedList. #ifndef STACK_H #include "LinkedList.h" class stack : protected LinkedList { public: // Class constructors stack(); stack(const stack &s); // Member functions bool isEmpty(); void push(const elementType &item); elementType pop(); elementType retrieve(); }; #define STACK_H #endif // Class declaration file: stack.h // Linked List implementation of the // stack ADT – inherits from LinkedList. #ifndef STACK_H #include "LinkedList.h" class stack : protected LinkedList { public: // Class constructors stack(); stack(const stack &s); // Member functions bool isEmpty(); void push(const elementType &item); elementType pop(); elementType retrieve(); }; #define STACK_H #endif The stack class “inherits” from the LinkedList class, so all LinkedList members are accessible to any stack. This derived class has a “protected” access specifier, indicating that the public and protected members of LinkedList are considered protected in the stack class. If the access specifier were “private”, then the public and protected members of LinkedList are considered private in the derived class. If the access specifier were “public”, then the public and protected members of LinkedList are considered public and protected (respectively) in the derived class. This derived class has a “protected” access specifier, indicating that the public and protected members of LinkedList are considered protected in the stack class. If the access specifier were “private”, then the public and protected members of LinkedList are considered private in the derived class. If the access specifier were “public”, then the public and protected members of LinkedList are considered public and protected (respectively) in the derived class. Let’s assume that the getNode and head members in LinkedList were declared protected, not private! Let’s also assume that the elementType typedef occurred in the LinkedList definition! Let’s assume that the getNode and head members in LinkedList were declared protected, not private! Let’s also assume that the elementType typedef occurred in the LinkedList definition!

CS // Class implementation file: stack.cpp // Linked List implementation of the // stack ADT – inherits from LinkedList. #include "Stack.h" #include "LinkedList.h" #include // Default constructor: // // Inherited from LinkedList. // stack:: stack(): LinkedList() {} // Copy constructor: // // Inherited from LinkedList. // stack:: stack(const stack &s): LinkedList(s) {} // Empty function: returns a boolean // // value that indicates whether or // // not the stack is empty. // bool stack:: isEmpty() { return head == NULL; } // Class implementation file: stack.cpp // Linked List implementation of the // stack ADT – inherits from LinkedList. #include "Stack.h" #include "LinkedList.h" #include // Default constructor: // // Inherited from LinkedList. // stack:: stack(): LinkedList() {} // Copy constructor: // // Inherited from LinkedList. // stack:: stack(const stack &s): LinkedList(s) {} // Empty function: returns a boolean // // value that indicates whether or // // not the stack is empty. // bool stack:: isEmpty() { return head == NULL; } // Push function: inserts item at // // the top of the stack. // void stack:: push(const elementType &elt) { nodePtr newHead = getNode(elt); assert(newHead != NULL); newHead->next = head; head = newHead; return; } // Pop function: removes and returns the // // top stack entry (if there is one). // elementType stack:: pop() { elementType elt; nodePtr oldHead; assert(head != NULL); oldHead = head; elt = head->item; head = head->next; delete oldHead; return elt; } // On_top function: returns (w/o removing) // // the top stack entry (if there is one). // elementType stack:: retrieve() { elementType elt; assert(head != NULL); elt = head->item; return elt; } // Push function: inserts item at // // the top of the stack. // void stack:: push(const elementType &elt) { nodePtr newHead = getNode(elt); assert(newHead != NULL); newHead->next = head; head = newHead; return; } // Pop function: removes and returns the // // top stack entry (if there is one). // elementType stack:: pop() { elementType elt; nodePtr oldHead; assert(head != NULL); oldHead = head; elt = head->item; head = head->next; delete oldHead; return elt; } // On_top function: returns (w/o removing) // // the top stack entry (if there is one). // elementType stack:: retrieve() { elementType elt; assert(head != NULL); elt = head->item; return elt; }