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.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Sample PMT online… Browse 1120/sumII05/PMT/2004_1/ 1120/sumII05/PMT/2004_1/
Lists + CS3240, L. Grewe 1. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked.
Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the.
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.
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Chapter 5 ADTs Stack and Queue. 2 Goals Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
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
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.
Inheritance CS 308 – Data Structures “the mechanism by which one class acquires the properties of another class” the properties of another class”
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
1 Chapter 4 Stack and Queue ADT. 2 Stacks of Coins and Bills.
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 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 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
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.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
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.
1 Fall Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.
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.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
Chapter 7 Stacks Dr. Youssef Harrath
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
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.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
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.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has.
Inheritance CS 302 – Data Structures Section 2.4 (pp ) and Section 6.7.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.
Data Structures: Stacks Queues 1. 2 Stack ADT: What is a Stack? a stack is a varying-length, collection of homogeneous elements Insertion and Deletion.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
Data Structures Using C++1 Chapter 7 Stacks. Data Structures Using C++2 Chapter Objectives Learn about stacks Examine various stack operations Learn how.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
Data Structures Using Java1 Chapter 6 Stacks. Data Structures Using Java2 Chapter Objectives Learn about stacks Examine various stack operations Learn.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 Data Structures and Algorithms Stacks and Queues.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 6 Lists Plus Lecture 12. What is a Circular Linked List? A circular linked list is a list in which every node has a successor; the “last” element.
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.
Chapter 5 (Part 1) ADT Stack 1 Fall Stacks TOP OF THE STACK.
1 C++ Plus Data Structures Abstract Data Types Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by.
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.
C++ Plus Data Structures
Chapter 5 ADTs Stack and Queue.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
C++ Plus Data Structures
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
Stacks.
CSC 143 Stacks [Chapter 6].
CSI 1340 Introduction to Computer Science II
C++ Plus Data Structures
CSI 1340 Introduction to Computer Science II
Presentation transcript:

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 at the top of the stack. n A stack is a “last in, first out” or LIFO LIFO structure.

Stack ADT Operations Transformers n makeEmpty -- Sets stack to an empty state. n push (ItemType newItem) -- Error if stack is full; otherwise adds newItem to the top of the stack. n pop (ItemType & item) -- Error if stack is empty; otherwise removes the item at the top of the stack and returns it in item. Observers n isEmpty -- Determines whether the stack is currently empty. n isFull -- Determines whether the stack is currently full. 2

// SPECIFICATION FILE: StackType.h #include "ItemType.h" // for class ItemType definition using namespace std; const int N = 100; class StackType { public: StackType( ); // Default constructor. // POST: Stack is created and empty. StackType(int); // Paramterized constructor. // POST: Stack is created and empty. ~StackType ( ); //PRE: Stack is created //POST: Stack is destroyed void makeEmpty( ); // PRE: None. // POST: Stack is empty. bool isEmpty( ) const; // PRE: Stack has been initialized. // POST: Function value = (stack is empty) 3

bool isFull( ) const; // PRE: Stack has been initialized. // POST: Function value = (stack is full) void push( ItemType newItem ); // PRE: Stack has been initialized and is not full. // POST: If stack is full, an exception is thrown; // otherwise newItem is at the top of the // stack. void pop( ItemType& item ); // PRE: Stack has been initialized and is not empty. // POST: If stack if empty, an exception is thrown; // otherwise top element has been removed from // stack.item is a copy of removed element. private: ItemType * stackArray; int stackSize; int top; }; 4

Stack Client Code n printStack –iteratively –recursively n reverseStack –iteratively –recursively n reverseUnsortedList using Stack 5

DYNAMIC ARRAY IMPLEMENTATION class StackType Private Data: top 2 stackSize 5 stackArray stackArray [0] stackArray[1] stackArray[2] stackArray[3] stackArray[4] StackType makeEmpty pop push isFull isEmpty ~StackType

// IMPLEMENTATION FILE (Stack.cpp) #include “Stack.h” #include using namespace std; StackType::StackType( ) { stackSize = N; stackArray = new ItemType [stackSize]; top = -1; } StackType::StackType( int size ) { stackSize = size; stackArray = new ItemType [stackSize]; top = -1; } void StackType::makeEmpty( ) { top = -1; } 7

bool StackType::isEmpty( ) const { return ( top == -1 ); } bool StackType::isFull( ) const { return ( top == stackSize - 1 ); } StackType:: ~StackType ( ) { delete [ ] stackArray; } 8

void StackType::push ( ItemType newItem ) { if (!isFull()) { top++; stackArray[top] = newItem; } else { cout << “The stack is full.\n”; exit(1); } void StackType::pop ( ItemType& item ) { if (!isEmpty()) { item = stackArray[top]; top--; } else { cout << “The stack is empty.\n”; exit(1); } 9

charletter = ‘V’; StackType charStack; charStack.push(letter); charStack.push(‘C’); charStack.push(‘S’); if ( !charStack.isEmpty( )) charStack.pop(letter); charStack.push(‘K’); while (!charStack.isEmpty( )) charStack.pop(letter); Tracing Client Code Private data: top stackArray [ stackSize-1 ]. [ 2 ] [ 1 ] stackArray [ 0 ] letter ‘V’

charletter = ‘V’; StackType charStack; charStack.push(letter); charStack.push(‘C’); charStack.push(‘S’); if ( !charStack.isEmpty( )) charStack.pop(letter); charStack.push(‘K’); while (!charStack.isEmpty( )) charStack.pop(letter); End of Trace Private data: top stackArray [ stackSize-1 ]. [ 2 ] K [ 1 ] C stackArray [ 0 ] V letter ‘V’

12 Client Code n printStack (iterative or recursive) n copyStack (iterative) n reverseStack n Applications of stacks –Run-time activation of records –Post-fix, pre-fix and infix evaluation of expressions –Backtracking searches (mazes)

What is a Class Template? 16.3 n A class template allows the compiler to generate multiple versions of a class type by using type parameters. n The formal parameter appears in the class template definition, and the actual parameter appears in the client code. Both are enclosed in pointed brackets,.

StackType numStack; top 3 [stackSize-1]. [ 3 ] 789 [ 2 ] -56 [ 1 ] 132 stackArray [ 0 ] ACTUAL PARAMETER

StackType nameStack; top 3 [stackSize-1]. [ 3 ] Bradley [ 2 ] Asad [ 1 ] Rodrigo stackArray [ 0 ] Max 15 ACTUAL PARAMETER

// CLASS TEMPLATE DEFINITION #include "ItemType.h" // for ItemType if it is a class!!! const int N = 100; template // formal parameter list class StackType { public: StackType( ); StackType(int); ~StackType ( ); void makeEmpty( ); bool isEmpty( ) const; bool isFull( ) const; void push( ItemType newItem ); void pop( ItemType & item ); private: ItemType * stackArray; int stackSize; int top; }; 16

// SAMPLE CLASS MEMBER FUNCTIONS template // formal parameter list StackType ::StackType( ) { top = -1; } template // formal parameter list void StackType ::push ( ItemType newItem ) { if (!IsFull()) { top++; items[top] = newItem;// STATIC ARRAY IMPLEMENTATION } 17

Using class templates n The actual parameter to the template is a data type. Any type can be used, either built-in or user-defined. n When using class templates, both the specification and implementation should be located in the same file, instead of in separate.h and.cpp files. 18