Queue:: Construct: Empty: Addq: Front: Removeq
Queue:: Construct: Empty: Addq: Front: Removeq
Disk Memory
Input
Buffer
CPU
X
Infile >> X;
Interactively: (scroll — queue on one
end, stack on the other)
Scroll:
Keyboard Front: Remove ONLY
Back: Add
or
Remove (Backspace)
Memory
Backspace
Input
Buffer
X cin >> X;
Screen handling: (deque — double-
ended queue)
Scroll:
Front: Remove
or
Add
Back: Add
or
Remove
b. Scheduling queues in a multi-user
computer system:
Printer queue: When files are submitted to a printer, they are
placed in the printer queue. The printer software executes
an algorithm something like:
for (;;)
{
while (printerQueue.empty())
sleep 1;
printFile = printerQueue.removeQ();
Print(printFile);
}
Other Queues:
1. Resident queue: On disk, waiting for memory
2. Ready queue: In memory — has everything it needs to run,
except the CPU
3. Suspended queue: Waiting for I/O transfer or to be reassigned the CPU
1
2
3
c. CPU Scheduling:
Probably uses a priority
queue: Items with lower
priority are behind all those
with higher priority.
(Usually a new item is
inserted behind those with the
same priority.)
Queues vs. Stacks
•Stacks are a LIFO container
•Store data in the reverse of order received
•Queues are a FIFO container
•Store data in the order received
•Stacks then suggest applications where some sort of reversal
or unwinding is desired.
•Queues suggest applications where service is to be rendered
relative to order received.
•Stacks and Queues can be used in conjunction to compare
different orderings of the same data set.
•From an ordering perspective, then, Queues are the “opposite”
of stacks
•Easy solution to the palindrome problem
Easy Palindrome Check
#include “Stack.h”;
#include “Queue.h”;
#include <iostream.h>
using namespace std;
int main(){
Stack S;
Queue Q;
char ch;
cout << “Enter string to be tested as palindrome: “;
while (cin >> ch){
S.push(ch);
Q.addq(ch);
}
Solutions include:
Shifting the elements downward with each deletion (YUCK!!)
Viewing array as a circular buffer, i.e. wrapping the end to the front
“Circular” Array-Implementation
Wraparound keeps the addition/deletion cycle from walking off the edge of the
storage array.
When myBack hits the end of myArray, a deletion should wrap myBack around
to the first element of myArray, viz:
myBack++;
if (myBack = = QUEUE_CAPACITY)
myBack = 0;
bool Queue::empty()
{
return myFront == myBack;
}
bool Queue::full()
{
return myFront == (myBack + 1) % QUEUE_CAPACITY;
}
QUEUE class implementation II
void Queue::addQ(const QueueElement& value)
{
if (myFront != (myBack + 1) % QUEUE_CAPACITY)
{
myArray[myBack] = value;
myBack = (myBack + 1) % QUEUE_CAPACITY;
}
return;
}
void Queue::removeQ()
{
myFront = (myFront + 1) % QUEUE_CAPACITY;
}
Linked list implementation
Linked list implementation is another approach for queue representation.
#include <iostream>
using namespace std;
void addq(int);
int removeq();
bool empty();
bool full();
int back();
int size();
private:
const int PRIORITYQ_CAPACITY = some_value;
int myArray[PRIORITYQ_CAPACITY];
int myFront;
int myBack;
};