Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Lists Chapter 8 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Sample PMT online… Browse 1120/sumII05/PMT/2004_1/ 1120/sumII05/PMT/2004_1/
The unorganized person’s data structure
Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Data Structures & Algorithms
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.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
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.
Topic 3 The Stack ADT.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
Implementations of the ADT Stack Chapter 7 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 6 Stacks CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
Stacks and Queues Introduction to Computing Science and Programming I.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
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.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
Stacks And Queues Chapter 18.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Chapter 6 A Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 A-2 The Abstract Data Type: Developing an ADT During the Design of a Solution.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
COSC160: Data Structures: Lists and Queues
CSCI 3333 Data Structures Stacks.
Stacks.
Hassan Khosravi / Geoffrey Tien
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Stack and Queue APURBO DATTA.
Stack.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Pointers and Dynamic Variables
Pointers and Linked Lists
Implementations of the ADT Stack
Chapter 19: Stacks and Queues.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks and Queues.
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Abstract Data Type Abstract Data Type as a design tool
Array-Based Implementations
Stacks CS-240 Dick Steflik.
Stacks and Queues.
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
Abstract Data Types Stacks CSCI 240
Lecture 9: Stack and Queue
Stack Implementations
CHAPTER 3: Collections—Stacks
Presentation transcript:

Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Introduction to the Stack ADT Stack: a LIFO (last in, first out) data structure Examples: plates in a cafeteria serving area return addresses for function calls

Specifications for the ADT Stack A stack of cafeteria plates Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Abstract Data Type: Stack A finite number of objects Not necessarily distinct Having the same data type Ordered by when they were added Stack uses LIFO principle Last In First Out We have identified the following operations: See whether stack is empty. Add a new item to stack. Remove from the stack item added most recently. Get item that was added to stack most recently. Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Stack Abstract Data Type Functions: push: add a value at the top of the stack pop: remove a value from the top of the stack peek: show the top of the stack, but don’t remove it. isEmpty: true if the stack currently contains no elements

Stack Basics Stack is usually implemented as a list, with additions and removals taking place at one end of the list The active end of the list implementing the stack is the top of the stack Stack types: Static – fixed size, often implemented using an array Dynamic – size varies as needed, often implemented using a linked list

An Array Based Implementation FIGURE 7-1 Using an array to store a stack’s entries: (a) a preliminary sketch; (b) implementation details Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Static Stack Implementation Uses an array of a fixed size Bottom of stack is at index 0. A variable called top tracks the current top of the stack const int STACK_SIZE = 3; char s[STACK_SIZE]; int top = 0; top is where the next item will be added

Array Implementation Example This stack has max capacity 3, initially top = 0 and stack is empty. K E G K E E push('E'); push('K'); push('G'); top is 1 top is 2 top is 3

Stack Operations Example After three pops, top is 0 and the stack is empty E K E pop(); (remove G) pop(); (remove K) pop(); (remove E)

Stack Operations Example Push(3) top Push(4) Peek() Pop()

Class Implementation { private: char *s; int capacity, top; public: class STACK { private: char *s; int capacity, top; public: void push(char x); char pop(); char peek(); bool isEmpty(); STACK(int stackSize); STACK(); ~STACK() }; int StackIsEmpty = 1009; int StackFullException = 1010; See IntStack.h, IntStack.cpp, pr18-01.cpp

Array Implementation char s[STACK_SIZE]; int top=0; To check if stack is empty: bool isEmpty() { if (top == 0) return true; else return false; }

Array Implementation To add an item to the stack void push(char x) { if (top == STACK_SIZE()) {throw(StackFullException);} // throw an exception s[top] = x; top++; }

Array Implementation To remove an item from the stack char pop() { if (isEmpty()) {throw(StackIsEmptyException);}// throw an exception top--; return s[top]; }

Array Implementation To look at the top item in the stack char peek() { if (isEmpty()) {throw(StackIsEmptyException);}// throw an exception return s[top]; }

Simple Uses of a Stack Reverse a list of characters Postfix calculator Deck of cards!! Program structure …. Lots more Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Using Stacks with Algebraic Expressions Evaluating postfix expressions The effect of a postfix calculator on a stack when evaluating the expression 2 * (3 + 4) Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

End Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013