CS 163 Lecture-Module #16:
Queues 1

what is a Queue?

Koffman Wolfgang Chapter 6
(LaFore Figure 4.7 page 135)

like Stack, Queue is data-structure for holding things
can add things to queue and it will hold them
used e.g. for scheduling processes, breadth-first search, ...
but by contrast with Stack, when remove elements from Queue,
instead of getting most recently added item,
get  item in Queue.

queue starts empty:
                i.e. [ ]
insert(10):
                i.e. [10]
then  insert(20)  so queue appears as follows:
                i.e. [10,20]
then  insert(30)  so queue appears as follows:
                i.e. [10,20,30]
then  insert(40)  so queue appears as follows:
                i.e. [10,20,30,40]
then start  remove()ing:
first item removed is oldest item: 
then queue appears as follows:
                i.e. [20,30,40]
next item removed is oldest remaining item: 
then queue appears as follows:
                i.e. [30,40]
next item removed is oldest remaining item: 
then queue appears as follows:
                i.e. [40]
then  insert(50), then  insert(60), then  70, then  80
then queue appears as follows:
                i.e. [40,50,60,70,80]
next item removed is oldest remaining item: 
then queue appears as follows:
                i.e. [50,60,70,80]
and so on

by contrast with Stack-property LIFO ,
Queue-property FIFO 

terms LIFO and FIFO used throughout CMPSC/CMPEN
e.g. when discussing architecture/hardware, operating systems, ...

Queue might appear more natural
but implementing Queue may be more complicated than Stack —
compare Stack implementation we've seen to Queue implementation here
and then you tell me

names of operations are:
void insert(x)  a.k.a.  enqueue()  to add  Object x  `at back' of Queue.
Object remove()  a.k.a.  dequeue()  to remove item `from front' of Queue.
boolean isEmpty()  to check whether Queue is empty.
our textbook also has:
Object peekFront()  to get item at front of Queue without removing it from Queue.

implementation pages 138-39 Listing 4.4 employing array and element-count
presented here (below) a little differently:
here using abbreviations "r" for "rear" and "f" for "front"
and using "A" instead of "10", "B" instead of "20", etc.
and with slightly different Queue-operations invoked
initially:
     r:
        +---+---+---+---+---+
        |   |   |   |   |   |           i.e. [ ]
        +---+---+---+---+---+
         f:

maxSize: 5


insert(j) {

  if ( (r == (maxSize - 1)) )
    r = -1;
  queArray[ ++r ] = j;

         r:
        +---+---+---+---+---+
        | A |   |   |   |   |           i.e. [A]
        +---+---+---+---+---+
         f:

  }


insert(j) {

  if ( (r == (maxSize - 1)) )
    r = -1;
  queArray[ ++r ] = j;

             r:
        +---+---+---+---+---+
        | A | B |   |   |   |           i.e. [A,B]
        +---+---+---+---+---+
         f:

  }


remove() {
  long temp = queArray[ f++ ];

             r:
        +---+---+---+---+---+
        |   | B |   |   |   |           i.e. [B]
        +---+---+---+---+---+
             f:

  if ( (f == maxSize) )
    f = 0;

  return  temp;

  }


remove() {
  long temp = queArray[ f++ ];

             r:
        +---+---+---+---+---+
        |   |   |   |   |   |           i.e. [ ]
        +---+---+---+---+---+
                 f:

  if ( (f == maxSize) )
    f = 0;

  return  temp;

  }


isEmpty() {
  return  (nItems == 0);
  }


insert(j) {

  if ( (r == (maxSize - 1)) )
    r = -1;
  queArray[ ++r ] = j;

                 r:
        +---+---+---+---+---+
        |   |   | C |   |   |           i.e. [C]
        +---+---+---+---+---+
                 f:

  }


insert(D);

                     r:
        +---+---+---+---+---+
        |   |   | C | D |   |           i.e. [C,D]
        +---+---+---+---+---+
                 f:


insert(E);

                         r:
        +---+---+---+---+---+
        |   |   | C | D | E |           i.e. [C,D,E]
        +---+---+---+---+---+
                 f:


insert(j) {

  if ( (r == (maxSize - 1)) )
    r = -1;

    r
        +---+---+---+---+---+
        |   |   | C | D | E |           i.e. [C,D,E]
        +---+---+---+---+---+
                 f:

  queArray[ ++r ] = j;

         r:
        +---+---+---+---+---+
        | F |   | C | D | E |           i.e. [C,D,E,F]
        +---+---+---+---+---+
                 f:

  }


insert(G);

             r:
        +---+---+---+---+---+
        | F | G | C | D | E |           i.e. [C,D,E,F,G]
        +---+---+---+---+---+
                 f:


isFull() {
  return  (nItems == maxSize);
  }


remove() {
  long temp = queArray[ f++ ];

             r:
        +---+---+---+---+---+
        | F | G |   | D | E |           i.e. [D,E,F,G]
        +---+---+---+---+---+
                     f:

  if ( (f == maxSize) )
    f = 0;

  return  temp;

  }


remove()

             r:
        +---+---+---+---+---+
        | F | G |   |   | E |           i.e. [E,F,G]
        +---+---+---+---+---+
                         f:


remove() {
  long temp = queArray[ f++ ];

             r:
        +---+---+---+---+---+
        | F | G |   |   |   |           i.e. [F,G]
        +---+---+---+---+---+
                             f:

  if ( (f == maxSize) )
    f = 0;

             r:
        +---+---+---+---+---+
        | F | G |   |   |   |           i.e. [F,G]
        +---+---+---+---+---+
         f:

  return  temp;

  }


remove()

             r:
        +---+---+---+---+---+
        |   | G |   |   |   |           i.e. [G]
        +---+---+---+---+---+
             f:



time-analysis:
with this implementation,
each Queue operation  insert(),  remove(), ... is O()
like Stack


(Copyright © 2006 by Hugh McGuire   — for thoughts about this, see   http://www.cis.gvsu.edu/~mcguire/teaching/copyright_thoughts.html .)