190 likes | 204 Views
CMSC 202. Stacks and Queues. What’s a Queue?. A queue is a linear collection of homogeneous data in which items added to the queue must be placed at the end of the queue and items removed from the queue are removed from the front. An Analogy Bank Teller Line.
E N D
CMSC 202 Stacks and Queues
What’s a Queue? A queue is a linear collection of homogeneous data in which items added to the queue must be placed at the end of the queue and items removed from the queue are removed from the front.
An AnalogyBank Teller Line • When you walk into the bank you go to the end of the line. • When the teller is ready to help the next person, the person at the beginning of the line is removed from the line. • Each person then moves one position closer to the beginning of the line. • Eventually, you will be the first person in the line and the teller will call you up causing you to be removed from the line.
What’s it used for? • Items waiting to be printed • Representing a waiting line in a simulation (traffic light, cafeteria, grocery store checkout, etc.) • Passing information from one process to another in the order it arrived
The Queue ADT • Description of the data • A linear collection of homogeneous data • A description of the operations • Add an element to the end of the queue. This is known as enqueueing. • Remove an element from the front of the queue. This is known as dequeueing. • A queue is a First-In, First-Out (FIFO) data structure • What other operations might be helpful?
Queue Implementation Since the Queue contains “homogeneous” data, it should be implemented as a class template. That way, we can create queues that contain any data type or object. Enqueueing seems to be a special kind of inserting. Dequeueing seems to be a special kind of removing. What run-time errors may occur?
Queue.H template < class T> class Queue { public: // constructor(s), destructor, operator= enqueue ( ); // what is/are the parameter(s) and //return type? dequeue ( ); // what is/are the parameter(s) and //return type? private: // data representation };
Using the Queue object main ( ) { Queue<int> iQ; iQ.enqueue ( 7 ); iQ.enqueue ( 42 ); int x = iQ.dequeue ( ); // x = 7; cout << iQ.dequeue ( ) << endl; // prints 42; x = iQ.dequeue ( ); // is an error }
What’s the data representation? • A queue seems to be very much like a list (in the abstract sense), except with different (or maybe additional) insert and remove functionality.
What’s a stack • A stack is a linear collection of homogeneous items, in which an item to be added to the stack must be placed on top of the stack and and an item that removed from the stack must be removed from the top.
Stack AnalogyCafeteria Trays • When you go to the UC or dining hall for some scrumptious food, you probably use a tray. Which one do you pick-up? The one on the top of the pile. • When the cafeteria work brings out new trays, where does he put them? On top of the pile. • Trays are both added and and removed from the top of the pile.
What’s it used for?? • Remembering where you were – function call return addresses • Reversing the order of “stuff”
The Stack ADT • Description of the data • A linear collection of homogeneous data • A description of the operations • Add an element to the top (front) of the stack. This is known as pushing. • Remove an element from the top (front) of the stack. This is known as popping. • A stack is a Last-In, First-Out (LIFO) data structure • What other operations might be helpful?
Stack Implementation Since the Stack contains “homogeneous” data, it should be implemented as a class template. That way, we can create stacks that contain any data type or object. pushing seems to be a special kind of inserting. poping seems to be a special kind of removing. . What run-time errors may occur?
Stack.H template < class T> class Stack { public: // constructor(s), destructor, operator= push ( ); // what is/are the parameter(s) and //return type? pop ( ); // what is/are the parameter(s) and //return type? private: // data representation };
Using the Stack object main ( ) { Stack<int> iS; iS.push ( 7 ); iS.push ( 42 ); int x = iS.pop ( ); // x = 42; cout << iS.pop ( ) << endl; // prints 7; x = iS.pop ( ); // is an error }
What’s the data representation? • A stack seems to be very much like a list (in the abstract sense), except with different (or maybe additional) insert and remove functionality.
Stack Example Matching parenthesis, braces and brackets in a C++ program.
A challenge • Explain how to simulate a Queue (ie. A FIFO data structure) using two stacks.