数据结构之链队列及其相关操作
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}QueueNode;
typedef struct
{
QueueNode *front;
QueueNode *rear;
}LinkQueue;
LinkQueue* Init_Queue(LinkQueue *q);
void Enter_Queue(LinkQueue *q,int num);
void Show_Queue(LinkQueue *q);
void Delete_Queue(LinkQueue *q,int *e);
void main()
{
int e;
LinkQueue *q = NULL;
q = Init_Queue(q);
printf("执行入队操作:\n");
Enter_Queue(q,100);
Enter_Queue(q,99);
printf("遍历队列:\n");
Show_Queue(q);
printf("执行出队操作:\n");
Delete_Queue(q,&e);
printf("%d出队\n",e);
Delete_Queue(q,&e);
printf("%d出队\n",e);
Delete_Queue(q,&e);
printf("%d出队\n",e);
}
LinkQueue* Init_Queue(LinkQueue *q)
{
QueueNode *p;
q = (LinkQueue *)malloc(sizeof(LinkQueue));
p = (QueueNode *)malloc(sizeof(QueueNode));
p->next = NULL;
q->front = p;
q->rear = p;
return q;
}
void Enter_Queue(LinkQueue *q,int num)
{
QueueNode *s = (QueueNode*)malloc(sizeof(QueueNode));
s->data = num;
s->next = NULL;
q->rear->next = s;
q->rear = s;
}
void Show_Queue(LinkQueue *q)
{
QueueNode *r;
r = q->front->next;
printf("%d ",r->data);
r = r->next;
while(r != NULL)
{
printf("%d ",r->data);
r = r->next;
}
printf("\n");
}
void Delete_Queue(LinkQueue *q,int *e)
{
QueueNode *p = NULL;
if(q->front->next == NULL)
{
printf("队列为空!\n");
exit(1);
}
else
{
p = q->front->next;
*e = p->data;
q->front->next = p->next;
free(p);
if(q->front->next == NULL)
{
q->rear = q->front;
}
}
}