Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 CMSC 202 Stacks and Queues

2 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.

3 An Analogy Bank Teller Line 1.When you walk into the bank you go to the end of the line. 2.When the teller is ready to help the next person, the person at the beginning of the line is removed from the line. 3.Each person then moves one position closer to the beginning of the line. 4.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.

4 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

5 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?

6 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?

7 Queue.H template 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 };

8 Using the Queue object main ( ) { Queue 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 }

9 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.

10 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.

11 Stack Analogy Cafeteria 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.

12 What’s it used for?? Remembering where you were – function call return addresses Reversing the order of “stuff”

13 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?

14 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?

15 Stack.H template 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 };

16 Using the Stack object main ( ) { Stack 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 }

17 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.

18 Stack Example Matching parenthesis, braces and brackets in a C++ program.

19 A challenge Explain how to simulate a Queue (ie. A FIFO data structure) using two stacks.


Download ppt "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."

Similar presentations


Ads by Google