队列的线性存储之循环队列
void SqQuene::InitQuene(sqQuene &s) {
s.front = 0;
s.rear = 0;
}
int SqQuene::QueneLength(sqQuene s) {
int lenght = (s.rear - s.front + MAXSIZE) % MAXSIZE;
return lenght;
}
sqQuene SqQuene::EnQuene(sqQuene *s, QElemType e) {
if ((s->rear + 1)%MAXSIZE != s->front){
s->data[s->rear] = e;
s->rear = (s->rear + 1) % MAXSIZE;
}
return *s;
}
sqQuene SqQuene::DeQueue(sqQuene *s, QElemType *e) {
if (s->front == s->rear) {
exit(0);
}
*e = s->data[s->front];
s->front = (s->front + 1) % MAXSIZE;
return *s;
}
QElemType SqQuene::TraversalQuene(sqQuene s) {
if (s.front == s.rear) {
exit(0);
}
for (int i = 0; i < s.rear; i++)
{
cout << s.data[i];
}
}
队列的链式存储
void LinkQuene::InitQuene(linkQuene & l)
{
l.rear = (QNode *)malloc(sizeof(QNode));
l.front = l.rear;
l.front->next = NULL;
}
void LinkQuene::EnQuene(linkQuene & l, QElemType e)
{
QuenePtr s = (QuenePtr)malloc(sizeof(QNode));
s->data = e;
s->next = NULL;
l.rear->next = s;
l.rear = s;
}
void LinkQuene::DeQuene(linkQuene & l, QElemType & e)
{
QuenePtr s;
s = l.front->next;
e = s->data;
l.front->next = s->next;
if (l.rear == s)
{
l.rear = l.front;
}
free(s);
}
void LinkQuene::TraversalQuene(linkQuene l)
{
QNode *p;
p = l.front->next;
while (p)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}