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.

Slides:



Advertisements
Similar presentations
The unorganized person’s data structure
Advertisements

Chapter 2 INTRODUCTION TO STACKS. Outline 1. Stack Specifications 2. Implementation of Stacks 3. Application: A Desk Calculator 4. Application: Bracket.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Introduction to Stacks & Queues.
Data Structures Chapter 3 Queues Andreas Savva. 2 Queues A data structure modeled after a line of people waiting to be served. A data structure modeled.
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.
Data Structures Chapter 4 Linked Stacks and Queues Andreas Savva.
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.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
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.
Object Oriented Data Structures
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.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
What is a Stack? n Logical (or ADT) level: A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only.
Templates Zhen Jiang West Chester University
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
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - Templates Outline 11.1 Introduction 11.2 Function Templates 11.3 Overloading Function Templates.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
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.
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.
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 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Introduction to Stacks Chapter 2. Objectives Introduce abstract data types. Discuss implementation types. – Static – Dynamic – Contiguous Introduce the.
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
CS240 Computer Science II Function and Class Templates (Based on Deitel) Dr. Erh-Wen Hu.
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.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
Chapter 2 INTRODUCTION TO STACKS 1. Stack Specifications 2. Implementation of Stacks 3. Application: A Desk Calculator 4. Application: Bracket Matching.
1 Data Structures CSCI 132, Spring 2014 Lecture 19 Lists.
Data Structures & Algorithms
Kruse/Ryba ch041 Linked Stacks and Queues Pointers and Linked Structures Linked Stacks Linked Stacks with Safeguards Linked Queues Application: Polynomials.
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.
Chapter 6 LISTS AND STRINGS 1. List Definition 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Doubly Linked 3. Linked.
1 Queues Queue API Application: Radix Sort Implementation: Using Deque Using Deque Circular Array Circular Array Priority Queue Priority Queue API Implementation.
Exam Review 2 Chapter 5 – 9 CSC212 FG CS Dept, CCNY.
1 Data Structures CSCI 132, Spring 2016 Notes 6 Applications using Stacks.
Stack Data Structure By Marwa M. A. Elfattah. Stack - What A stack is one of the most important non- primitive linear data structure in computer science.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
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.
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Stacks Stacks.
CSCI 3333 Data Structures Stacks.
Stacks Chapter 7 introduces the stack data type.
Stacks.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CSC 143 Stacks [Chapter 6].
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Jordi Cortadella and Jordi Petit Department of Computer Science
Visit for more Learning Resources
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Stacks.
Presentation transcript:

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 end, called the top of the stack. 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. The last entry which is inserted is the first one that will be removed. (Last In First Out : LIFO) The last entry which is inserted is the first one that will be removed. (Last In First Out : LIFO)

3 Application of Stacks Page visited history in a Web-browser Page visited history in a Web-browser Undo sequence in a text editor Undo sequence in a text editor Program run-time environment Program run-time environment

4 A B C D E F The Stack Pushing and popping a stack Push box A onto the stack Push box B onto the stack Pop a box from the stack Push box C onto the stack Push box D onto the stack Push box E onto the stack Push box F onto the stack

5 Implementation of Stacks Some Basic Operations for Stacks Create a stack, leaving it empty. Create a stack, leaving it empty. Test whether the stack is Empty. Test whether the stack is Empty. Retrieve the Top entry from the stack, provided the stack is not empty. Retrieve the Top entry from the stack, provided the stack is not empty. Pop the entry off the top of the stack, provided the stack is not empty. Pop the entry off the top of the stack, provided the stack is not empty. Push a new entry onto the top of the stack, provided that the stack is not full. Push a new entry onto the top of the stack, provided that the stack is not full.

6 Basic Operations false Create() = Empty = true 5 Top = Errorunderflow

7 Basic Operations Push 2, = Pop = Errorunderflow Push 7, = Erroroverflow

8 Standard Template Library (STL) The STL provides convenient implementations for many common data structures. The STL provides convenient implementations for many common data structures. We can include the STL stack implementation into our programs with the directive: We can include the STL stack implementation into our programs with the directive: #include #include And then we can define initially empty stack objects and apply push, pop, top and empty. And then we can define initially empty stack objects and apply push, pop, top and empty. In C++, a template constructor allows us to create data structures whose entities have different types: In C++, a template constructor allows us to create data structures whose entities have different types: stack numbers;

9 Example using the STL stack #include int main( ) /* Pre: The user supplies an integer n and n real 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 << ”Enter an integer n followed by n real numbers” << endl; cin >> n; for (int i = 0; i < n; i++) { cin >> item; numbers.push(item); } cout << endl << endl << ”Numbers in reverse: ”; while (!numbers.empty( )) { cout << numbers.top( ) << ” ”; numbers.pop( ); }

10 Information Hiding The code in a client program should not depend on a particular choice of implementation. The code in a client program should not depend on a particular choice of implementation. We may first decide to represent a stacks one way and then we may decide that another is better. We may first decide to represent a stacks one way and then we may decide that another is better. If we use information hiding by writing separate functions for manipulating stacks, then only the declarations will need to be changed. If we use information hiding by writing separate functions for manipulating stacks, then only the declarations will need to be changed.

11 Basic Operations for Stacks Create a stack, leaving it empty. Create a stack, leaving it empty. Test whether the stack is Empty. Test whether the stack is Empty. Test whether the stack is Full. Test whether the stack is Full. Return the Size (number of entries) of the stack. Return the Size (number of entries) of the stack. Retrieve the Top entry from the stack, provided the stack is not empty. Retrieve the Top entry from the stack, provided the stack is not empty. Pop the entry off the top of the stack, provided the stack is not empty. Pop the entry off the top of the stack, provided the stack is not empty. Push a new entry onto the top of the stack, provided that the stack is not full. Push a new entry onto the top of the stack, provided that the stack is not full. Print all the entries of the stack. Print all the entries of the stack.

12 The Stack Class methods: Stack (constructor) empty full size top pop push print Data members class Stack

13 Stack implementation - Array typedef double Stack_entry; enum Error_code {success,overflow,underflow}; const int max = 100; class Stack { public: Stack(); bool empty() const; bool full() const; int size() const; Error_code top(Stack_entry &) const; Error_code pop(); Error_code push(const Stack_entry &); void print() const; private: int count; Stack_entry entry[max]; };

14 Data Structure - Array 0... [0][1] [2] [max-1] Empty Stack: 1 *... [0][1] [2] [max-1] Push the first entry: n items in the stack: n *****... [0][1] [2] [max-1] [n-1] [n]

15 Create Stack Stack::Stack() // Pre:None. // Post:Initialize Stack to be empty { } We use a constructor to initialize a new created stack as empty [0][1] [2] [max-1] [n-1] [n]countentry

16 Empty bool Stack::empty() const // Pre:None. // Post:A result of true is returned if the Stack //is empty, otherwise false is returned. { } 0... [0][1] [2] [max-1] [n-1] [n]countentry

17 Full bool Stack::full() const // Pre:None. // Post:A result of true is returned if the Stack //is full, otherwise false is returned. { } max *******... [0][1] [2] [max-1] [n-1] [n]countentry

18 Size int Stack::size() const // Pre:None. // Post:Return the number of entries in the stack. { } n *****... [0][1] [2] [max-1] [n-1] [n]countentry

19 Top Error_code Stack::top(Stack_entry &item) const // Pre:None. // Post:The top of a nonempty Stack is copied to item. //A code of underflow is returned if the Stack is empty. { } n *****... [0][1] [2] [max-1] [n-1] [n]countTop entry

20 Pop 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 and the Stack is left unchanged. { } n *****... [0][1] [2] [max-1] [n-1] [n]count n-1 entry

21 Push 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. { } n *****... [0][1] [2] [max-1] [n-1] [n]count * n+1 entry

22 Print void Stack::print() const // Pre:None. // Post:Display the entries of the Stack. { }