队列的链式存储结构
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct Node {
ElemType data;
struct Node* next;
}* QNode;
typedef struct aQueue {
QNode front;
QNode rear;
}* Queue;
Queue QueueInit();
int QueueEmpty(Queue Q);
ElemType QueueFirst(Queue Q);
ElemType QueueLast(Queue Q);
void EnterQueue(Queue Q, ElemType p);
ElemType DeleteQueue(Queue Q);
int main()
{
Queue Q = QueueInit();
int n, p;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &p);
EnterQueue(Q, p);
}
printf("%d\n", QueueFirst(Q));
printf("%d\n", QueueLast(Q));
for (int i = 0; i < n; i++)
{
printf("%d ", DeleteQueue(Q));
}
printf("\n");
return 0;
}
Queue QueueInit()
{
Queue Q = (Queue)malloc(sizeof(struct aQueue));
Q->front = Q->rear = NULL;
return Q;
}
int QueueEmpty(Queue Q)
{
return Q->front == NULL;
}
ElemType QueueFirst(Queue Q)
{
if (QueueEmpty(Q))
return 0;
return Q->front->data;
}
ElemType QueueLast(Queue Q)
{
if (QueueEmpty(Q))
return 0;
return Q->rear->data;
}
void EnterQueue(Queue Q, ElemType x)
{
QNode n = (QNode)malloc(sizeof(struct Node));
n->data = x;
n->next = NULL;
if (Q->front)
{
Q->rear->next = n;
Q->rear = n;
}
else
{
Q->front = Q->rear = n;
}
}
ElemType DeleteQueue(Queue Q)
{
if (QueueEmpty(Q))
return 0;
ElemType x = Q->front->data;
QNode p = Q->front;
Q->front = Q->front->next;
free(p);
return x;
}