链队列的初始化,建立,插入,查找,删除。

 

代码
 
   


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

 

转载于:https://www.cnblogs.com/newwy/archive/2010/10/10/1847463.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值