Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.

Slides:



Advertisements
Similar presentations
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Advertisements

COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
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.
What is a Queue? A queue is a FIFO “first in, first out” structure.
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.”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
Implementing a Stack as a Linked Structure CS 308 – Data Structures.
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.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
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.”
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)
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.
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 AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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 -
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
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,
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.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
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.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
Stacks And Queues Chapter 18.
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.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
CC 215 Data Structures Queue ADT
C++ Plus Data Structures
Stacks and Queues.
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
CMSC 341 Lecture 5 Stacks, Queues
Chapter 5 ADTs Stack and Queue.
Chapter 16-2 Linked Structures
Stacks, Queues, and Deques
Chapter 19: Stacks and Queues.
C++ Plus Data Structures
Chapter 16 Linked Structures
Stacks CS-240 Dick Steflik.
C++ Plus Data Structures
CSI 1340 Introduction to Computer Science II
Queues.
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Presentation transcript:

Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200

Outline Stack Queue Comparison Array-based Implementation Linked Implementation Queue Comparison Fall 2013 Yanjun Li CS2200

Stacks of Coins and Bills Fall 2013 Yanjun Li CS2200

Stacks of Boxes and Books TOP OF THE STACK Fall 2013 Yanjun Li CS2200

Stacks Logical level What do these composite objects all have in common ? Fall 2013 Yanjun Li CS2200

Stacks Stack An abstract data type in which elements are added and removed from only one end. A “last in, first out” (LIFO) structure. Fall 2013 Yanjun Li CS2200

Stacks Logical level What operations would be appropriate for a stack? Fall 2013 Yanjun Li CS2200

Stacks Transformers Observers Push Pop IsEmpty IsFull Top change state observe state What about an iterator? Fall 2013 Yanjun Li CS2200

Stacks Application Level For what type of problems would stacks be useful? LIFO: A stack is great for reversing data. Operating system function calls Finding palindromes Expression evaluation & syntax parsing Fall 2013 Yanjun Li CS2200

Stack Applications Operating system function calls void DrawSquare(int x, int y, int edge) { DrawLine(x, y, edge, HORIZONTAL); DrawLine(x, y, edge, VERTICAL); DrawLine(x+edge, y, edge, VERTICAL); DrawLine(x, y+edge, edge, HORIZONTAL); } int main () DrawSquare(1,2,3); return 0; Fall 2013 Yanjun Li CS2200

Stack Applications Finding palindromes (Lab!) Fall 2013 Yanjun Li CS2200

Stack Applications Help Converting Decimal to Binary (pseudocode) Read (number) Loop (number > 0) 1) digit = number modulo 2 (number%2) 2) print (digit) 3) number = number / 2 // from Data Structures by Gilbert and Forouzan Problem: The binary numbers are printed backwards. 19 becomes 11001 instead of 10011 Solution: push each binary number onto the stack and pop the digit out of the stack and print it at the end. Fall 2013 Yanjun Li CS2200

Stacks class StackType { public: StackType(); ~StackType(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType item); void Pop( ); ItemType Top() const; Fall 2013 Yanjun Li CS2200

Stacks Implementation Level Array-based Implementation Linked Structure Fall 2013 Yanjun Li CS2200

Array-Based Implementation private: int top; ItemType items[MAX_ITEM]; }; [0] [1] [2] …. [M..] stack .items .top Fall 2013 Yanjun Li CS2200

Array-Based Implementation Give a series of operations that could produce this situation Fall 2013 Yanjun Li CS2200

Array-Based Implementation To what do we initialize top ? 0 or -1 Do we increment or store first? 0: store and increment -1: increment and store Which is better? (what does top represent?) StackType::StackType() { top = -1; } Fall 2013 Yanjun Li CS2200

Array-Based Implementation Before we code, we must consider error conditions Stack overflow The condition that results from trying to push an element on to a full stack Exception class : FullStack Stack underflow The condition that results from trying to pop an empty stack Exception class: EmptyStack Fall 2013 Yanjun Li CS2200

Array-Based Implementation //pre: Stack has been initialized. //post: function value = (stack is empty) bool StackType::IsEmpty() const { return ( top == -1); } //pre: stack has been initialized. //post: function value = (stack is full) bool StackType:: IsFull() const return ( top == MAX_ITEM-1); What does const mean? Fall 2013 Yanjun Li CS2200

Array-Based Implementation //pre: stack has been initialized and is not full //post: newItem is at the top of the stack. void StackType::Push(ItemType newItem) { top++; items[top] = newItem; } Fall 2013 Yanjun Li CS2200

Array-Based Implementation //pre: stack has been initialized. //post: if stack is full, throw an exception; //Else newItem is at the top of the stack. void StackType::Push(ItemType newItem) { if (IsFull()) throw FullStack(); top++; items[top] = newItem; } What is FullStack( ) ? Fall 2013 Yanjun Li CS2200

Array-Based Implementation //pre: stack has been initialized and is not empty. //post: top element has been removed from stack. void StackType::Pop() { top--; } //post: A copy of the top element is returned. ItemType StackType:: Top() const return (items[top]); Fall 2013 Yanjun Li CS2200

Array-Based Implementation //pre: stack has been initialized. //post: if stack is empty, throw an exception; else top element //has been removed from stack. void StackType::Pop() { if (IsEmpty()) throw EmptyStack(); top--; } //post: if stack is empty, throw an exception; else a copy of the //top element is returned. ItemType StackType::Top() const if (IsEmpty()) throw EmptyStack(); return (items[top]); What is EmptyStack ? Fall 2013 Yanjun Li CS2200

Array-Based Implementation Which functions have to be changed if we dynamically allocate the array items ? Do we need to add any new functions? Fall 2013 Yanjun Li CS2200

Test Plan Clear-box strategy to check each operation. Push() & Pop() while it is empty or full. Fall 2013 Yanjun Li CS2200

Linked Implementation The logical level (public part of the class declaration) stays the same; class StackType { public: StackType(); ~StackType(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType item); void Pop( ); ItemType Top() const; Fall 2013 Yanjun Li CS2200

Linked Implementation The implementation level (private part of the class declaration) changes private: NodeType* topPtr; }; ‘D’ .info .next Pointer to the next node in the stack .topPtr Stack Can we “borrow” code from UnsortedType for Push and Pop ? Fall 2013 Yanjun Li CS2200

Linked Implementation //pre: the stack is not full //post: the new item is added on top of the stack void StackType::Push(ItemType newItem) { NodeType* location; location = new NodeType; location->info = newItem; location>next = topPtr; topPtr = location; } newItem .info .next .info .next ‘D’ location .topPtr Fall 2013 Yanjun Li CS2200

Linked Implementation //pre: the stack is not empty //post: the top item is removed from the top of the //stack void StackType::Pop() { NodeType* tempPtr; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; } Does this work for stacks of one item? More than Fall 2013 Yanjun Li CS2200

Linked Implementation More than one item Fall 2013 Yanjun Li CS2200

Linked Implementation One item Fall 2013 Yanjun Li CS2200

Linked Implementation What about the constructor, destructor, and observer functions? We can borrow all but Top() from class UnsortedType List //pre: the stack is not empty //post: the item on the top of the stack is //returned. ItemType StackType::Top() { return topPtr->info; } Fall 2013 Yanjun Li CS2200

Other Member Functions Constructor Destructor Free all the node spaces. isEmpty isFull Fall 2013 Yanjun Li CS2200

Array vs. Linked Structure A serious drawback of array-based implementation: the size of a stack must be determined when a stack object is declared. Size is not enough or Space is wasted. Fall 2013 Yanjun Li CS2200

Big-O Comparison Time Array-Based Implementation Linked Implementation Class constructor Class destructor O(1) O(n) IsFull() IsEmpty() Push() Pop() Fall 2013 Yanjun Li CS2200

Queues Fall 2013 Yanjun Li CS2200

Queues Fall 2013 Yanjun Li CS2200

Queues What do these composite objects all have in common? Fall 2013 Yanjun Li CS2200

Queues Queue An abstract data type in which elements are added to the rear and removed from the front; a “first in, first out” (FIFO) structure Fall 2013 Yanjun Li CS2200

Queues What operations would be appropriate for a queue? Fall 2013 Yanjun Li CS2200

Queues Transformers Observers MakeEmpty Enqueue Dequeue IsEmpty IsFull change state observe state What about an iterator? Fall 2013 Yanjun Li CS2200

Queues For what type of problems would stacks be useful? Print server maintains a queue of print jobs. Disk driver maintains a queue of disk input/output requests. Scheduler (e.g., in an operating system) maintains a queue of processes awaiting a slice of machine time. Fall 2013 Yanjun Li CS2200

Queues class QueueType Logical Level { public: QueueType(int max); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType item); //add newItem to the rear of the queue. void Dequeue(ItemType& item); //remove front item from queue Logical Level Fall 2013 Yanjun Li CS2200

Array-Based Implementation private: ItemType* items; // Dynamically allocated array int maxQue; // Whatever else we need }; Implementation level Fall 2013 Yanjun Li CS2200

Array-Based Implementation One data structure: An array with the front of the queue fixed in the first position Enqueue A, B, C, D Dequeue Move elements down What’s wrong with this design? Fall 2013 Yanjun Li CS2200

Array-Based Implementation Another data structure: An array where the front floats What happens if we add X, Y, and Z ? Fall 2013 Yanjun Li CS2200

Array-Based Implementation We can let the queue wrap around in the array; i.e. treat the array as a circular structure Fall 2013 Yanjun Li CS2200

Array-Based Implementation (a) Initial conditions front = 2 rear = 2 (b) queue.Dequeue(item) front = 3 A [0] [1] [2] [3] [4] Empty Queue Full Queue How can we tell the difference? Fall 2013 Yanjun Li CS2200

Array-Based Implementation A third data structure:Front indicates the slot preceding the front item;it is reserved and not used Empty Queue Full Queue Fall 2013 Yanjun Li CS2200

Array-Based Implementation private: int front; int rear; int maxQue; ItemType* items; }; Complete implementation level To what do we initialize front and rear ? Fall 2013 Yanjun Li CS2200

Array-Based Implementation QueueType::QueueType( int max) { maxQue = max + 1; front = maxQue - 1; rear = maxQue - 1; items = new ItemType[maxQue]; } QueueType::QueueType( ) { maxQue = 500 + 1; QueueType::~QueueType( ) { delete [] items; Why is the array declared max + 1 ? Fall 2013 Yanjun Li CS2200

Array-Based Implementation //Pre: the queue is not full //Post: newItem is at the rear of the queue void QueueType::Enqueue(ItemType newItem) { rear = (rear + 1) % maxQue; items[rear] = newItem; } Fall 2013 Yanjun Li CS2200

Array-Based Implementation //pre: the queue is not empty //post: the front of the queue has been removed // and a copy returned in item void QueueType::Dequeue(ItemType& item) { front = (front + 1) % maxQue; item = items[front]; } Fall 2013 Yanjun Li CS2200

Array-Based Implementation //returns true if the queue is empty bool QueueType::IsEmpty( ) { return ( rear == front ); } //returns true if the queue is full bool QueueType::IsFull( ) return ( (rear + 1) % maxQue == front ) Fall 2013 Yanjun Li CS2200

Linked Implementation Data structure for linked queue What data members are needed? Fall 2013 Yanjun Li CS2200

Linked Implementation Enqueue(newItem) Set newNode to the address of newly allocated node Set Info(newNode) to newItem Set Next(newNode) to NULL Set Next(rear) to newNode If queue is empty Set front to newNode else Fall 2013 Yanjun Li CS2200

Linked Implementation Dequeue(item) Set tempPtr to front Set item to Info(front) Set front to Next(front) if queue is now empty Set rear to NULL Deallocate Node(tempPtr) Fall 2013 Yanjun Li CS2200

Storage Requirements What does this table tell you ? Fall 2013 Yanjun Li CS2200

Reference Reproduced from C++ Plus Data Structures, 4th edition by Nell Dale. Reproduced by permission of Jones and Bartlett Publishers International. Fall 2013 Yanjun Li CS2200