QUEUES - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

QUEUES

Description:

QUEUES QUEUE OBJECT Introduction queue is another simple data structure that is easy to manipulate useful for programmers because it can be used to model real-world ... – PowerPoint PPT presentation

Number of Views:219
Avg rating:3.0/5.0
Slides: 13
Provided by: Jaso1160
Category:
Tags: queues | circular | queue

less

Transcript and Presenter's Notes

Title: QUEUES


1
QUEUES
2
QUEUE OBJECT
  • Introduction
  • queue is another simple data structure that is
    easy to manipulate
  • useful for programmers because it can be used to
    model real-world situations
  • 1) simulation of a bank line
  • 2) simulation of auto mechanic shop bey
  • queues work on the FIFO principle
    (first-in-first-out)
  • 1) use an array to implement the queue/list
  • 2) need two indexes
  • front where we take from the list
  • back where we add to the list
  • 3) also, a number to keep track of the current
    size of list

3
INTRO TO QUEUES (cont.)
  • 4) when first value added, front and rear point
    to same place
  • 5) each additional add rear marker moved down
    one
  • 6) each remove
  • front marker moved down one
  • problem - must make sure not to add past the end
    of the array
  • one solution make the array circular (when reach
    the end, reset rear to point to slot 0)
  • another solution make the array dynamic
  • (solution we will use here)

4
Conceptual Design of a Queue
  • so, if we design the Queue as an object
  • Queue Object
  • Knows
  • pointer to space in memory containing the
    list
  • the front
  • the rear
  • the current size ( items in the queue)
  • Can Do
  • Retrieve an item (take out of front of
    queue)
  • Add an item (add to end of queue)
  • Tell which item is in front (without
    removing)
  • Clear the queue
  • Tell if the queue is empty

5
  • typedef int Item
  • typedef Item ItemPtr
  • class Queue
  • public
  • static const int DEFAULTSIZE 30
  • Queue()
  • Queue(const int QueueSize)
  • Queue()
  • Item First()
  • Item Remove()
  • int Size()
  • void Insert(const Item anItem)
  • bool IsEmpty() const
  • void Clear()
  • private
  • ItemPtr MyQueue
  • int Front
  • int Rear
  • int Total

6
MORE MEMBER FUNCTIONS
  • The Default Constructor - creates a queue
    dynamically of the DEFAULTSIZE
  • calls the Clear() function which sets the values
    of front and rear and sets total to 0
  • Queue Queue()
  • MyQueue new ItemDEFAULTSIZE
  • ArraySize DEFAULTSIZE
  • Clear()
  • The Constructor - creates a Queue of QueueSize
    size
  • Queue Queue(const int QueueSize)
  • ArraySize QueueSize
  • MyQueue new ItemQueueSize
  • Clear()

7
MORE MEMBER FUNCTIONS
  • The Deconstructor - simply deletes the queue
    from the heap
  • Queue Queue()
  • delete MyQueue
  • The IsFull Function - returns true if total is gt
    DEFAULTSIZE
  • bool Queue IsFull() const
  • return (total gt ArraySize)
  • The IsEmpty Function - will return true if the
    queue is empty (no items in it)
  • bool Queue IsEmpty() const
  • return (Total 0)

8
MORE MEMBER FUNCTIONS
  • The Size Function - will return the number of
    items currently in queue
  • int Queue Size() const
  • return(Total)
  • The Insert Function - takes an item as input and
    adds it to the queue
  • void Queue Insert(const Item anItem)
  • if (Total gt ArraySize)
  • cout ltlt "Queue is Full"
  • else
  • if (Rear ArraySize -1)
  • Rear 0
  • else
  • Rear
  • MyQueuerear anItem
  • Total

9
MORE MEMBER FUNCTIONS
  • The Remove Function - returns the item in the
    front of the queue and moves the Front marker
    over one
  • Item Queue Remove()
  • Item temp
  • if (IsEmpty())
  • cout ltlt "Cannot remove - queue empty"
    ltlt endl
  • return 999
  • else
  • temp MyQueueFront
  • if (Front ArraySize -1)
  • Front 0
  • else
  • Front
  • Total--
  • return temp

10
MORE MEMBER FUNCTIONS
  • The First Function - gives back the first item in
    queue but does not remove it
  • Item Queue First() const
  • if (IsEmpty())
  • cout ltlt "No items in queue " ltlt endl
  • return -999
  • else
  • return(MyQueueFront)
  • The Clear Function - clears the queue by
    resetting indexes and counters values are still
    in the queue but they are ignored by the program
  • void Queue Clear()
  • Front 0
  • Rear ArraySize - 1
  • Total 0

11
AN EXAMPLE OF USING THIS QUEUE
  • int main()
  • Queue TheQueue(3)
  • int number, count 0
  • do
  • cout ltlt "enter a number to add to the
    queue "
  • cin gtgt number
  • TheQueue.Insert(number)
  • count
  • while (count lt 3)
  • int number_in_queue TheQueue.Size()
  • int value_in_queue, evencout 0
  • for (int i 1 i lt number_in_queue i)
  • value_in_queue TheQueue.Remove()
  • if (value_in_queue 2 0)
  • evencout
  • cout ltlt "Total even numbers in queue " ltlt
    evencount ltlt endl

12
QUESTIONS?
  • Read section on LINKED QUEUES
  • (P. 412 425)
Write a Comment
User Comments (0)
About PowerShow.com