Professional Documents
Culture Documents
Queues
Queues
Sal F. Gambino
CS-342
C++ CS 342
Data Structures
&
Queues 1
Lists- QUEUE
sfg
Sal F. Gambino
CS-342
operations:
•ENQUEUE insertAtBack
•DEQUEUE removeFromFront
•isQueueEmpty
•getTop returns the top data item
•isFull usually used with static arrays
Implementation:
1. Dynamic: Pointers
Restricted Linked List
2. Static: Arrays
Partially Filled
Circular Arrays
3
Lists- QUEUE
sfg
Sal F. Gambino
Applications: CS-342
1. Recognizing Palindromes (words forward/back, same)
2. Printing queues (spools)
3. Buffering input (keyboard input)
4. Simulation Programs (lines, waiting for traffic lite)
5. Echoing a word (read ch enqueue, write Q)
6. A line (bank teller, drive-in window)
7. A line (busy baker’s shop, take a ticket #)
8. Phone calls to large companies are generally
placed on a Q when all operators are Busy
Implementation:
1. Pointers ………Linked List notes
0 4
COUNT CAPACITY
-1
0 1 2 3 FIRST
-1
11
LAST
INSERT & REMOVE sfg
If ( count == 0 ) If ( count == 0 )
{ count++; first = 0; last = 0; Sal F. Gambino
{ “Q is empty cannot remove”
cin >> Q[0]; } CS-342
} else if ( count == 1 )
else if ( count < CAPACITY ) { last = -1; first = -1; count = 0;
{ last = (last +1) % CAPACITY; }
cin >> Q[last]; else if ( count <= CAPACITY )
count++; { first = (first +1) % CAPACITY;
} count- -;
else “Q is FULL cannot add” }
0 4
COUNT CAPACITY
-1
0 1 2 3 FIRST
-1
12
LAST
INSERT & REMOVE sfg
If ( count == 0 ) If ( count == 0 )
{ count++; first = 0; last = 0; Sal F. Gambino
{ “Q is empty cannot remove”
cin >> Q[0]; } CS-342
} else if ( count == 1 )
else if ( count < CAPACITY ) { last = -1; first = -1; count = 0;
{ last = (last +1) % CAPACITY; }
cin >> Q[last]; else if ( count <= CAPACITY )
count++; { first = (first +1) % CAPACITY;
} count- -;
else “Q is FULL cannot add” }
0 4
COUNT CAPACITY
-1
0 1 2 3 FIRST
-1
13
LAST
INSERT & REMOVE sfg
If ( count == 0 ) If ( count == 0 )
{ count++; first = 0; last = 0; Sal F. Gambino
{ “Q is empty cannot remove”
cin >> Q[0]; } CS-342
} else if ( count == 1 )
else if ( count < CAPACITY ) { last = -1; first = -1; count = 0;
{ last = (last +1) % CAPACITY; }
cin >> Q[last]; else if ( count <= CAPACITY )
count++; { first = (first +1) % CAPACITY;
} count- -;
else “Q is FULL cannot add” }
0 4
COUNT CAPACITY
-1
0 1 2 3 FIRST
-1
14
LAST
#ifndef queue_h DERIVED class sfg
#define queue_h
#include "list.h" Sal F. Gambino
template<class QUEUETYPE> BASE classCS-342
class Queue : private List<QUEUETYPE>
{
Inheritance: class Queue will inherit from class List
Queue can access the PUBLIC & PROTECTED
members NOT the PRIVATE members of List
public:
void enqueue(const QUEUETYPE &d ) { insertAtBack(d); }
int dequeue (QUEUETYPE &d ) { return removeFromFront(d);}
int isQueueEmpty()const { return isEmpty(); }
void printQueue() const { print(); }
}; 15
#endif
#ifndef queue_h Includes: sfg
#define queue_h
List--> ListNode Sal F. Gambino
#include "list.h"
template<class QUEUETYPE> CS-342