Title: CMSC 202
1CMSC 202
2Whats 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.
3An 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.
4Whats 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
5The 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?
6Queue 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?
7Queue.H
- template lt class Tgt
- 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
8Using the Queue object
- main ( )
-
- Queueltintgt iQ
- iQ.enqueue ( 7 )
- iQ.enqueue ( 42 )
- int x iQ.dequeue ( ) // x 7
- cout ltlt iQ.dequeue ( ) ltlt endl // prints 42
- x iQ.dequeue ( ) // is an error
9Whats 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.
10Whats 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.
11Stack 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.
12Whats it used for??
- Remembering where you were function call return
addresses - Reversing the order of stuff
13The 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?
14Stack 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?
15Stack.H
template lt class Tgt 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
16Using the Stack object
main ( ) Stackltintgt iS iS.push ( 7
) iS.push ( 42 ) int x iS.pop ( ) // x
42 cout ltlt iS.pop ( ) ltlt endl // prints
7 x iS.pop ( ) // is an error
17Whats 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.
18Stack Example
- Matching parenthesis, braces and brackets in a
C program.
19A challenge
- Explain how to simulate a Queue (ie. A FIFO data
structure) using two stacks.