newwy
奋斗在IT路上的小蜗牛。一步一步往上爬,爬到小牛,在到大牛,然后是神牛,然后是犇,然后就可以离开IT行业,回归大自然了。 远离IT,珍爱生命!!! 记录学习的点滴。

 

代码


////////////////////////////////////////////
//链队列的初始化,建立,插入,查找,删除。//
//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;
}

 

posted on 2010-10-10 22:45  newwy  阅读(2912)  评论(0编辑  收藏  举报