Exercises
Queues

1. What is a queue (at the logical level)?

2. Give an example of a queue in everyday life.

3. Use the header file to see how the seven `ListQueue` member functions compare with the seven apstack member functions in the table.
 `apstack` `ListQueue` `push(n)` `pop(n)` `isEmpty( )` `pop( )` `n = top( )` `n = length( )` `makeEmpty( )`

4. In problems 4 - 6, display each program fragment's output. Show your work.

```ListQueue Q; Q.enqueue(5); Q.enqueue(6); Q.enqueue(7); Q.enqueue(8); int x=Q.peekFront( ); int y; y=Q.dequeue(); Q.enqueue(x); Q.enqueue(++y); Q.dequeue(); y=Q.dequeue(); while( !Q.isEmpty( ) )    x=Q.dequeue(); System.out.println(x);```

5. ```ListQueue Q; int x = 2; Q.enqueue(4); Q.enqueue(x); Q.enqueue(Q.peekFront( )); Q.enqueue(3+Q.peekFront( )); Q.enqueue(0); Q.dequeue(); while(!Q.isEmpty()) {    x=Q.dequeue();    System.out.print(x);} System.out.println(); ```

6. ``` apstack S; ListQueue Q; int a=0, b=1; int c=a+b; while (c<10) {    if (c%2==0)       S.push(c);    else Q.enqueue(c);    a=b;    b=c;    c=a+b; } while ( !S.isEmpty( ) ) {    S.pop(c);    System.out.print(c); } while ( !Q.isEmpty( ) ) {    c=Q.dequeue();    System.out.print(c); } ```

Continue to:  Unit 7 / Prev / Next