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.

Slides:



Advertisements
Similar presentations
Stacks.
Advertisements

Chapter 5.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Stacks. Queues. Double-Ended Queues. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010 Goodrich, Tamassia.
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Applications of Stacks Direct applications Delimiter matching Undo sequence in a text.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Stacks.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
Implementing and Using Stacks
Stacks1 CS2468 Data Structures and Data Management Lecturer: Lusheng Wang Office: B6422 Phone:
Object Oriented Data Structures
Topic 3 The Stack ADT.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stacks. 2 What Are Stacks ? PUSHPOP 0 MAX Underflow Overflow.
Stacks © 2010 Goodrich, Tamassia1Stacks. 2 Abstract Data Types (ADTs)  An abstract data type (ADT) is an abstraction of a data structure  An ADT specifies:
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Abstract Data Type (ADT) & Stacks
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
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)
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
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,
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Dynamic Arrays and Stacks CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
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.
Dynamic Arrays and Stacks Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Parasol Lab, Dept. CSE, Texas A&M University
Welcome to CSCE 221 – Data Structures and Algorithms
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.
Click to edit Master text styles Stacks Data Structure.
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 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stack: Last In First Out (LIFO).–Used in procedure calls, to compute arithmetic expressions.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Stacks (and Queues).
Stacks Stacks.
CSCI 3333 Data Structures Stacks.
Stacks.
Stacks and Queues.
Queues Queues Queues.
CMSC 341 Lecture 5 Stacks, Queues
Stacks.
Stacks.
Stacks 12/7/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Recall What is a Data Structure Very Fundamental Data Structures
Stacks and Queues DSA 2013 Stacks n Queues.
Stacks.
Stacks.
Lecture 8: Stacks, Queues
Stacks and Linked Lists
Presentation transcript:

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 that when a program retrieves an item from a stack, the last item inserted into the stack is the first one retrieved. Similarly, the first item inserted is the last one retrieved (FILO). Stack ADT

first plate in, last plate out Last plate in, first plate out Stack of Plates The stack is a common data structure for representing things that need to maintained in a particular order. For instance, when a function calls another function, which in turn calls a third function, it's important that the third function return back to the second function rather than the first. Stack ADT

There are two kinds of stack data structure - a) static, i.e. they have a fixed size, and are implemented as arrays. b) dynamic, i.e. they grow in size as needed, and implemented as linked lists. Static and Dynamic Stacks

A stack has two primary operations, called push and pop. a) The push operation causes a value to be stored (pushed) onto the stack. e.g. if we have an empty integer stack with maximum capacity of 3 items, we can perform the following 3 push operations - push(5); push(10); push(15); push() and pop()

The pop operation retrieves (removes) an item from the stack. If we execute 3 consecutive pop operations on the stack as shown above, we get the following results - push() and pop()

Exceptions Attempting the execution of an operation of an ADT may sometimes cause an error condition, called an exception. Exceptions are said to be “thrown” by an operation that cannot be executed. In the Stack ADT, operation pop cannot be performed if the stack is empty In the Stack ADT, operation push cannot be performed if the stack is full Attempting the execution of pop on an empty stack, or a push on a full stack, throws an EmptyStackException

Stack Operations Therefore, in order to simulate a stack functionality, the following operations need to be implemented. – Main stack operations: push(object o): inserts element o pop(): removes and returns the last inserted element – Auxiliary stack operations: top(): returns a reference to the last inserted element without removing it size(): returns the number of elements stored isEmpty(): returns a Boolean value indicating whether no elements are stored

Applications of Stacks Direct applications – Page-visited history in a Web browser – Undo sequence in a text editor – Saving local variables when one function calls another, and this one calls another, and so on. Indirect applications – Auxiliary data structure for algorithms – Component of other data structures

C++ Run-time Stack The C++ run-time system keeps track of the chain of active functions with a stack When a function is called, the run- time system pushes on the stack a frame containing – Local variables and return value – Program counter, keeping track of the statement being executed When a function returns, its frame is popped from the stack and control is passed to the method on top of the stack main() { int i = 5; foo(i); } foo(int j) { int k; k = j+1; bar(k); } bar(int m) { … } bar PC = 1 m = 6 foo PC = 3 j = 5 k = 6 main PC = 2 i = 5

Array-based Stack A simple way of implementing the Stack ADT uses an array We add elements from left to right A variable keeps track of the index of the top element S 012 t … Algorithm size() return t + 1 Algorithm pop() if isEmpty() then throw EmptyStackException else t  t  1 return S[t + 1]

Array-based Stack (cont.) The array storing the stack elements may become full A push operation will then throw a FullStackException – Limitation of the array- based implementation – Not intrinsic to the Stack ADT S 012 t … Algorithm push(o) if t = S.length  1 then throw FullStackException else t  t + 1 S[t]  o

Performance and Limitations Performance – Let n be the number of elements in the stack – The space used is O(n) – Each operation runs in time O(1) Limitations – The maximum size of the stack must be defined a priori, and cannot be changed – Trying to push a new element into a full stack causes an implementation-specific exception

Writing a program that uses an STL stack

Using a Stack in a Program Requirement – Reverse a phrase that is input by the user Steps in the process of creating the program 1. Write a test 2. Write an algorithm 3. Write the program 4. Test the program Run an automated test – one way to test your program

1. Write a Test Reverse "Go dog" Reverse "Madam, I’m Adam"

2. Write an Algorithm //ALGORITHM main() // Get the string s1 // Reverse the string // Print the reversed string

2. Write an Algorithm //ALGORITHM main() // Get the string s1 // Use a stack to reverse the string // reverseString(s1) // Print the reversed string

2. Write an Algorithm ALGORITHM reverseString(s1) Input: a string Output: the string reversed

2. Write an Algorithm ALGORITHM reverseString(s1) Input: a string Output: the string reversed string s2 stack st for i = 0 to i = s1.length - 1 st.push(s1[i]) while not st.empty() s2 += st.top() st.pop() return s2

Implementing a Data Structure as a Class A Stack Example

Stack ADT intsize() Return the number of elements in the stack boolisEmpty() Indicate whether the stack is empty voidpush( Object element ) Insert element at the top of the stack Objecttop() Return the top element on the stack without removing it; an error occurs if the stack is empty. Objectpop() Remove and return the top element on the stack; an error occurs if the stack is empty

To begin… Open a header file and name it “ArrayStack.h” Write the keyword “class” followed by the name of the class class ArrayStack { }; Add the curly braces, and remember the semicolon

Step 1 – Declare Public Operations Declare member functions for all the public operations that are called for by the ADT (See p. 157 for the stack ADT.) class ArrayStack { public: int size(); bool isEmpty(); void push( const char& c ); char& top(); void pop(); }; Access specifier All member functions after “ public: ” are accessible by any client in your program wherever there is an object of this class.

Step 2 – Declare Data Members Next, decide how to hold the data that the data structure will contain. For example, you could put it into either an array or a linked list. class ArrayStack { private: int capacity;//Maximum capacity of the array char *pMyArray;//Pointer to an array public: int size(); bool isEmpty(); void push(const char& c); char& top(); void pop(); }; Access specifier All members after “ private: ” are only accessible to the member functions of this class. Information hiding Data members are always “ private ” so that the object’s client cannot change the data by accessing it directly. Data can be accessed only by using the “ public ” member functions.

Step 3 – Define the Operations Now, decide how to implement the operations class ArrayStack { private: int topIndex; int capacity; //Maximum capacity of the array char *pMyArray; //Pointer to an array public: int size(); bool isEmpty(); void push(const char& c); char& top() { return pMyArray[topIndex]; } void pop(); };

Step 4 – Add Exceptions etc. Add robustness – use exceptions Avoid memory problems – Implement a copy constructor – Implement operator=