![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
// //
// 链队列的初始化,建立,插入,查找,删除。 //
// Author:Wang Yong //
// Date: 2010.8.19 //
// //
#include < stdio.h >
#include < stdlib.h >
typedef int ElemType;
//
// 定义队列结点类型
typedef struct Qnode
{
ElemType data;
struct Qnode * next;
} Qnode;
/// 定义队列结点的头指针,为指针
typedef struct
{
Qnode * front;
Qnode * rear;
}LQueue, * LinkQueue;
//
// 队列初始化
LinkQueue LinkQueueInit()
{
LinkQueue Q;
Qnode * P;
Q = (LinkQueue)malloc( sizeof (LQueue)); // 申请头,尾指针结点
P = (Qnode * )malloc( sizeof (Qnode)); // 申请头结点
P -> next = NULL;
Q -> front = Q -> rear = P;
return Q;
}
/// //
// 入队
void LinkQueueEnter(LinkQueue Q,ElemType x)
{
Qnode * p;
p = (Qnode * )malloc( sizeof (Qnode)); // 申请新结点
p -> data = x;
p -> next = NULL;
Q -> rear -> next = p;
Q -> rear = p;
}
/// //
// 出队
ElemType LinkQueueOut(LinkQueue Q)
{
ElemType x;
Qnode * p;
if (Q -> front != Q -> rear)
{
p = Q -> front -> next;;
x = p -> data;
Q -> front -> next = p -> next; // 移动头指针
free(p);
if (Q -> front -> next == NULL) // 最后一个元素出队后,队空,修改队尾指针
Q -> rear = Q -> front;
}
else
return 0 ;
return x;
}
int main()
{
LinkQueue lqueue;
lqueue = LinkQueueInit();
ElemType x;
printf( " 请输入入队列的元素: " );
while (scanf( " %d " , & x) != EOF)
{
LinkQueueEnter(lqueue,x);
}
Qnode * p;
for (p = lqueue -> front -> next; p != lqueue -> rear -> next; p = p -> next )
printf( " %d " ,p -> data);
printf( " 出队列的结果为: " );
while (lqueue -> front != lqueue -> rear)
{
printf( " %d " ,LinkQueueOut(lqueue));
}
return 0 ;
}