Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Introduction to Stacks & Queues.

Slides:



Advertisements
Similar presentations
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
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.
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 Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
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.
Queue Overview Queue ADT Basic operations of queue
A stack is a data linear data structure in which addition of new element or deletion of an existing element always takes place at the same end. This.
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.
Stack and Queue COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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 and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
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.
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.”
Stacks and queues Basic operations Implementation of stacks and queues Stack and Queue in java.util Data Structures and Algorithms in Java, Third EditionCh04.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Data Structures Using C++
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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.
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.
Data Structures and Algorithm Analysis Dr. Ken Cosh Stacks ‘n’ Queues.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 6. Dictionaries(3): Binary Search Trees.
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)
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R3. Priority Queues.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
1 Binary Search Trees. 2 Binary Search Trees Binary Search Trees The Binary Search Tree (BST) Search, Insertion and Traversal of BST Removal of nodes.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2a. Simple Containers: The Stack.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
CSCE 210 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
CSCE 210 Data Structures and Algorithms
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
CC 215 Data Structures Queue ADT
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Queues.
Chapter 19: Stacks and Queues.
CSC 143 Queues [Chapter 7].
Pointers & Dynamic Data Structures
Queues.
Data Structures & Programming
Presentation transcript:

Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Introduction to Stacks & Queues

Prof. Amr Goneid, AUC2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays

Prof. Amr Goneid, AUC3 1. Introduction to the Stack Data Structure  A simple data container consisting of a linear list of elements  Access is by position (order of insertion)  All insertions and deletions are done at one end, called top  Last In First Out (LIFO) structure  Two basic operations: push: add to top, complexity is O(1) pop: remove from top, complexity is O(1)

Prof. Amr Goneid, AUC4 Example

5 Example top ++toptop push pop top top--

Prof. Amr Goneid, AUC6 Run-time stack used in function calls Page-visited history in a Web browser Undo sequence in a text editor Removal of recursion Reversal of sequences Checking for balanced symbols Some Stack Applications

Prof. Amr Goneid, AUC7 Stack Class Operations construct: construct an empty stack stackIsEmpty  bool : return True if stack is empty stackIsFull  bool : return True if stack is full push(el) : add element (el) at the top pop(el): retrieve and remove the top element stackTop(el): retrieve top element without removing it

Prof. Amr Goneid, AUC8 The stack may be implemented as a dynamic array. The capacity (MaxSize) will be input as a parameter to the constructor (default is 128) The stack ADT will be implemented as a template class to allow for different element types. 2. Array Based Stack Class Definition

Prof. Amr Goneid, AUC9 // File: Stackt.h // Stack template class definition. // Dynamic array implementation #ifndef STACKT_H #define STACKT_H template class Stackt { public: Stackt (int nelements = 128);// Constructor ~Stackt ();// Destructor A Stack Class Definition

Prof. Amr Goneid, AUC10 // Member Functions void push(Type );// Push void pop(Type &);// Pop void stackTop(Type &) const;// retrieve top bool stackIsEmpty() const;// Test for Empty stack bool stackIsFull() const;// Test for Full stack private: Type *stack;// pointer to dynamic array int top, MaxSize; }; #endif // STACKT_H #include "Stackt.cpp" A Stack Class Definition

Prof. Amr Goneid, AUC11 // File: Stackt.cpp // Stack template class implementation #include using namespace std; // Constructor with argument, size is nelements, default is 128 template Stackt ::Stackt(int nelements) { MaxSize = nelements; stack = new Type[MaxSize]; top = -1; } A Stack Class Implementation

Prof. Amr Goneid, AUC12 // Destructor template Stackt ::~Stackt() { delete [ ] stack;} A Stack Class Implementation

Prof. Amr Goneid, AUC13 // Push template void Stackt ::push(Type v) { if(stackIsFull()) cout << "Stack Overflow"; else stack[++top] = v; } A Stack Class Implementation

Prof. Amr Goneid, AUC14 // Pop template void Stackt ::pop(Type &v) { if(stackIsEmpty()) cout << "Stack Underflow"; else v = stack[top--]; } A Stack Class Implementation

Prof. Amr Goneid, AUC15 // Retrieve stack top without removing it template void Stackt ::stackTop(Type &v) const { if(stackIsEmpty()) cout << "Stack Underflow"; else v = stack[top]; } A Stack Class Implementation

Prof. Amr Goneid, AUC16 // Test for Empty stack template bool Stackt ::stackIsEmpty() const { return (top < 0); } // Test for Full stack template bool Stackt ::stackIsFull() const { return (top >= (MaxSize-1)); } A Stack Class Implementation

Prof. Amr Goneid, AUC17 int main() // Testing the Stackt Class {// Reverse a string and stack copy Stackt s1; char c; string instring = "Testing Stack Class"; string outstring = ""; cout << instring << endl; int L = instring.length(); cout << "Pushing characters on s1\n"; for (int i = 0; i < L; i++) s1.push(instring.at(i)); cout << "Copying s1 to s2\n"; Stackt s2 = s1; A Driver Program to Test Class

Prof. Amr Goneid, AUC18 cout << "Popping characters from s1\n"; while(!s1.stackIsEmpty()) { s1.pop(c); outstring = outstring + c; } cout << outstring << endl; cout <<"s1 is now empty. Trying to pop from empty s1\n"; s1.pop(c); cout << "Now popping contents of s2" << endl; while(!s2.stackIsEmpty()) { s2.pop(c); cout << c;} cout<< endl; return 0; } A Driver Program to Test Class

Prof. Amr Goneid, AUC19 Output: Testing Stack Class Pushing characters on s1 Copying s1 to s2 Popping characters from s1 ssalC kcatS gnitseT s1 is now empty. Trying to pop from empty s1 Stack Underflow Now popping contents of s2 ssalC kcatS gnitseT Press any key to continue A Driver Program to Test Class

Prof. Amr Goneid, AUC20 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays

Prof. Amr Goneid, AUC21 1. introduction to the Queue Data Structure  A simple data container consisting of a linear list of elements  Access is by position (order of insertion)  Insertions at one end (rear), deletions at another end (front)  First In First Out (FIFO) structure  Two basic operations: enqueue: add to rear, complexity is O(1) dequeue: remove from front, complexity is O(1)

Prof. Amr Goneid, AUC22 An Illustration

Prof. Amr Goneid, AUC23 Enqueue and Dequeue o When last array element is reached, we move back to start o The queue is viewed as a circular array o To enqueue: rear = (rear + 1) % size o To dequeue: front = (front + 1) % size o Both rear and front advance clockwise

Prof. Amr Goneid, AUC24 Simulation of waiting lines Simulation of serviceable events Job scheduling Input/Output Buffering Multiprogramming Some Queue Applications

Prof. Amr Goneid, AUC25 Queue Class Operations construct: construct an empty queue queueIsEmpty  bool : return True if queue is empty queueIsFull  bool : return True if queue is full enqueue(el) : add element (el) at the rear dequeue(el): retrieve and remove the front element queueFront(el): retrieve front without removing it queueLength  int : return the current queue length

Prof. Amr Goneid, AUC26 The queue may be implemented as a dynamic array. The capacity (MaxSize) will be input as a parameter to the constructor (default is 128) The queue ADT will be implemented as a template class to allow for different element types. 2. Array Based Queue Class Definition

Prof. Amr Goneid, AUC27 // File: Queuet.h // Queue template class definition // Dynamic array implementation #ifndef QUEUET_H #define QUEUET_H template class Queuet { public: Queuet (int nelements = 128);// Constructor ~Queuet ();// Destructor A Queue Class Definition

Prof. Amr Goneid, AUC28 // Member Functions void enqueue(Type );// Add to rear void dequeue(Type &);// Remove from front void queueFront(Type &) const;// Retrieve front bool queueIsEmpty() const;// Test for Empty queue bool queueIsFull() const;// Test for Full queue int queueLength() const;// Queue Length private: Type *queue;// pointer to dynamic array int front, rear, count, MaxSize; }; #endif // QUEUET_H #include "Queuet.cpp" A Queue Class Definition

Prof. Amr Goneid, AUC29 // File: Queuet.cpp // Queue template class implementation #include using namespace std; // Constructor with argument, size is nelements, default is 128 template Queuet ::Queuet(int nelements) { MaxSize = nelements; queue = new Type[MaxSize]; front = 1; rear = 0; count = 0; } A Queue Class Implementation

Prof. Amr Goneid, AUC30 // Destructor template Queuet ::~Queuet() { delete [ ] queue;} A Queue Class Implementation

Prof. Amr Goneid, AUC31 // Add to rear template void Queuet ::enqueue(Type v) { if(queueIsFull()) cout << "Queue is Full"; else {rear = (rear + 1) % MaxSize; queue[rear] = v; count++; } } A Queue Class Implementation

Prof. Amr Goneid, AUC32 // Remove from front template void Queuet ::dequeue(Type &v) { if(queueIsEmpty()) cout << "Queue is Empty"; else {v = queue[front]; front = (front + 1) % MaxSize; count--; } } A Queue Class Implementation

Prof. Amr Goneid, AUC33 // Retrieve front without removing it template void Queuet ::queueFront(Type &v) const { if(queueIsEmpty()) cout << "Queue is Empty" << endl; else v = queue[front]; } A Queue Class Implementation

Prof. Amr Goneid, AUC34 // Test for Empty queue template bool Queuet ::queueIsEmpty() const { return (count == 0); } // Test for Full queue template bool Queuet ::queueIsFull() const { return (count == MaxSize); } // Queue Length template int Queuet ::queueLength() const { return count; } A Queue Class Implementation

Prof. Amr Goneid, AUC35 // File: QueuetAppl.cpp // Test if a string is a palindrome #include using namespace std; #include "Stackt.h" #include "Queuet.h" bool palindrome(string w); A Driver Program to Test Class

Prof. Amr Goneid, AUC36 int main() { string w; cout << "Enter a string:" << endl; getline(cin,w); cout << w << endl; if (palindrome(w)) cout << "Palindrome" << endl; else cout << "NOT Palindrome" << endl; return 0; } A Driver Program to Test Class

Prof. Amr Goneid, AUC37 bool palindrome(string w) { Stackt s; Queuet q; int L = w.length(); char c,v; for (int i = 0; i < L; i++) { c = w.at(i); s.push(c); q.enqueue(c); } while(!q.queueIsEmpty()) { q.dequeue(c); s.pop(v); if(c != v) return false; } return true; } A Driver Program to Test Class

Prof. Amr Goneid, AUC38 Output: Enter a string: Palindrome Press any key to continue Enter a string: NOT Palindrome Press any key to continue A Driver Program to Test Class