#include<iostream>
using namespace std;
#define MaxSize 60
typedef int ElemType;
typedef struct {
ElemType data[MaxSize];
int front, rear;
}SqQueue;
bool InitSqQueue(SqQueue& Q)
{
Q.front = Q.rear = 0;
return true;
}
bool QueueEmpty(SqQueue Q)
{
return (Q.rear == 0);
}
bool EnQueue(SqQueue& Q, ElemType e)
{
if (Q.rear==MaxSize)
return false;
Q.data[Q.rear++] = e;
return true;
}
bool DeQueue(SqQueue& Q, ElemType& e)
{
if (QueueEmpty(Q)) return false;
e = Q.data[Q.front];
for (int i = 0; i < Q.rear; i++)
{
Q.data[i] = Q.data[i + 1];
}
Q.rear--;
return true;
}
bool GetHead(SqQueue Q, ElemType& e)
{
if (QueueEmpty(Q)) return false;
e = Q.data[Q.front];
return true;
}
void printQueue(SqQueue Q)
{
for (int i = 0; i < Q.rear; i++)
{
cout << Q.data[i] <<' ';
}
cout << endl;
}
int main()
{
SqQueue Q;
ElemType e;
InitSqQueue(Q);
for (int i = 0; i < MaxSize; i++)
{
EnQueue(Q, i * 2);
}
DeQueue(Q, e);
cout << "e=" << e << endl;
DeQueue(Q, e);
cout << "e=" << e << endl;
DeQueue(Q, e);
cout << "e=" << e << endl;
GetHead(Q, e);
cout << "e=" << e << endl;
GetHead(Q, e);
cout << "e=" << e << endl;
GetHead(Q, e);
cout << "e=" << e << endl;
return 0;
}
#include<iostream>
#define MaxSize 6
using namespace std;
typedef int ElemType;
class SqQueue {
public:
bool InitQueue();
bool QueueEmpty();
bool EnQueue(ElemType e);
bool DeQueue(ElemType& e);
bool GetHead(ElemType& e);
void Queuelength();
private:
ElemType data[MaxSize];
int front, rear;
};
bool SqQueue::InitQueue()
{
front = rear = 0;
return true;
}
bool SqQueue::QueueEmpty()
{
return rear== front;
}
bool SqQueue::EnQueue(ElemType e)
{
if ((rear + 1) % MaxSize == front)
return false;
data[rear] = e;
rear = (rear + 1) % MaxSize;
return true;
}
bool SqQueue::DeQueue(ElemType& e)
{
if (QueueEmpty())
return false;
e = data[front];
front = (front + 1) % MaxSize;
return true;
}
bool SqQueue::GetHead(ElemType& e)
{
if (QueueEmpty()) return false;
e = data[front];
return true;
}
void SqQueue::Queuelength()
{
cout << "Queuelength = " << (rear + MaxSize - front) % MaxSize << endl;
}
int main()
{
SqQueue S;
S.InitQueue();
for (int i = 1; i < 6; i++)
{
S.EnQueue(i);
}
S.Queuelength();
ElemType e;
S.DeQueue(e);
cout << "e=" << e << endl;
S.DeQueue(e);
cout << "e=" << e << endl;
S.DeQueue(e);
cout << "e=" << e << endl;
S.Queuelength();
for (int i = 1; i < 6; i++)
{
S.EnQueue(7+i);
}
S.Queuelength();
cout << "----------------" << endl;
S.DeQueue(e);
cout << "e=" << e << endl;
S.DeQueue(e);
cout << "e=" << e << endl;
S.GetHead(e);
cout << "e=" << e << endl;
S.DeQueue(e);
cout << "e=" << e << endl;
S.DeQueue(e);
cout << "e=" << e << endl;
S.GetHead(e);
cout << "e=" << e << endl;
S.Queuelength();
return 0;
}
#include<iostream>
#define MaxSize 6
using namespace std;
typedef int ElemType;
class Queue {
public:
bool InitQueue();
bool QueueEmpty();
bool EnQueue(ElemType e);
bool DeQueue(ElemType& e);
bool GetHead(ElemType& e);
void QueueLength();
private:
ElemType data[MaxSize];
int front, rear, count;
};
bool Queue::InitQueue()
{
front = 0;
rear = -1;
count = 0;
return true;
}
bool Queue::QueueEmpty()
{
if (count == 0)
return true;
return false;
}
bool Queue::EnQueue(ElemType e)
{
if (count == MaxSize)
return false;
rear = (rear + 1) % MaxSize;
data[rear] = e;
count++;
return true;
}
bool Queue::DeQueue(ElemType& e)
{
if (QueueEmpty())
return false;
e = data[front];
count--;
front = (front + 1) % MaxSize;
return true;
}
bool Queue::GetHead(ElemType& e)
{
if (QueueEmpty()) return false;
e = data[front];
}
void Queue::QueueLength()
{
cout << "QueueLength = " << count << endl;
}
int main()
{
Queue S;
S.InitQueue();
for (int i = 1; i < 10; i++)
{
S.EnQueue(i);
}
S.QueueLength();
ElemType e;
S.DeQueue(e);
cout << "e=" << e << endl;
S.DeQueue(e);
cout << "e=" << e << endl;
S.DeQueue(e);
cout << "e=" << e << endl;
S.QueueLength();
for (int i = 1; i < 6; i++)
{
S.EnQueue(7 + i);
}
S.QueueLength();
cout << "----------------" << endl;
S.DeQueue(e);
cout << "e=" << e << endl;
S.DeQueue(e);
cout << "e=" << e << endl;
S.GetHead(e);
cout << "e=" << e << endl;
S.DeQueue(e);
cout << "e=" << e << endl;
S.DeQueue(e);
cout << "e=" << e << endl;
S.GetHead(e);
cout << "e=" << e << endl;
S.QueueLength();
return 0;
}