CMSC 202 - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

CMSC 202

Description:

A queue is a linear collection of homogeneous data in which items added to the ... Matching parenthesis, braces and brackets in a C program. A challenge ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 20
Provided by: dennis139
Category:
Tags: cmsc | parenthesis

less

Transcript and Presenter's Notes

Title: CMSC 202


1
CMSC 202
  • Stacks and Queues

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

4
Whats 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 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

8
Using 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

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

12
Whats 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 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
16
Using 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
17
Whats 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.
Write a Comment
User Comments (0)
About PowerShow.com