Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Queue

Hi all
Please can anyone answer my question about static queue or give me link explain how the queue is work

1- where first item will save? in index 0 correct?
2- rear (tail) Always empty?
3- I want to Enqueue tow numbers and Dqueue one number from 1 to queue is full, then get first drop number. Please tell me if my code is correct

Thank

#include <iostream>
#include <stdio.h>

#define SIZE 10
int stat=0,l_value;
class QueueClass {
int queue; // holds the queue
int head, tail; // indices of head and tail
public:
QueueClass(); // constructor
void q(int num); // store
int deq(); // retrieve
void display();
};

QueueClass::QueueClass()
{
head = tail = 0;
}

void QueueClass::q(int num)
{
if(tail+1==head|| tail+1==SIZE && !head) {

if (stat!=1)
{
cout << "Queue is full\n";
cout << "\n\n==== The dorp number is ===>> " << num << endl;
}
stat=1;

return;
}

if(tail==SIZE) tail = 0; // cycle around
queue[tail] = num;
cout << "Tial =" << tail <<" ";
tail++;
}

int QueueClass::deq()
{
int h_value;

if(head==SIZE) head = 0; // cycle around
cout << "head =" << head <<" ";
h_value= queue[head];
head++;
return h_value;
}

void QueueClass::display()
{
cout << "\n|=============== Queue items =============|" << endl;
for(int i=0;i<=9;i++)
cout <<" "<< queue[i] << " ";
}

void main()
{
//cout << "Please enter the start number (0,1,...n)";
//cin >>l_value;
QueueClass queue1;
while (stat !=1)
{
l_value++;
queue1.q(l_value);
if (stat!=1)
cout << "Enqueue : " << l_value << endl;
queue1.q(l_value+1);
if (stat!=1)
{
cout << "Enqueue : " << (l_value+1) << endl;
cout << " Dequeue : " << queue1.deq() << endl;
l_value++;
}
} // while end
queue1.display();
getchar();


[SIZE=6]Result

Tial =0 Enqueue : 1
Tial =1 Enqueue : 2
head =0 Dequeue : 1
Tial =2 Enqueue : 3
Tial =3 Enqueue : 4
head =1 Dequeue : 2
Tial =4 Enqueue : 5
Tial =5 Enqueue : 6
head =2 Dequeue : 3
Tial =6 Enqueue : 7
Tial =7 Enqueue : 8
head =3 Dequeue : 4
Tial =8 Enqueue : 9
Tial =9 Enqueue : 10
head =4 Dequeue : 5
Tial =0 Enqueue : 11
Tial =1 Enqueue : 12
head =5 Dequeue : 6
Tial =2 Enqueue : 13
Tial =3 Enqueue : 14
head =6 Dequeue : 7
Tial =4 Enqueue : 15
Tial =5 Enqueue : 16
head =7 Dequeue : 8
Tial =6 Enqueue : 17
Queue is full

==== The dorp number is ===>> 18

|=============== Queue items =============|
11 12 13 14 15 16 17 8 9 10
[3276 byte] By [Mansur] at [2007-11-11 8:28:27]
# 1 Re: Queue
With a queue, you push into the "tail" and pop off the "head" ("front"). The head and tail are always occupied, unless the queue is empty, when head == tail.

Normally a queue is not indexed - you only look at the front when you look at content. You might back a queue with an array (the circular manner as you seem to be using), which IS indexed, and the index is used in the background, but the public methods use only the queue methods (push, pop, front, isEmpty, isFull, size, empty).
nspils at 2007-11-11 21:01:21 >
# 2 Re: Queue
Often, when empty, head&tail are (invalid/NULL) and when you have one item, head = tail. After that, it normalizes and head and tail are different, valid things.
jonnin at 2007-11-11 21:02:21 >
# 3 Re: Queue
After posting last time, I went back and looked at my notes from the C++ based data structures class I took [I also took one using Java and we did not use the nifty roll your own stuff we had to resort to in the C++ class] and found exactly what jonin wrote about the state of head and tail when empty/one element/more than one element.
nspils at 2007-11-11 21:03:26 >
# 4 Re: Queue
Thank nspils / jonnin
Mansur at 2007-11-11 21:04:26 >