链队列

*****用途*****

可以自由控制队列的空间

*****用途*****


#include <stdio.h>

#include <stdlib.h>
#define MAXSIZE 20


typedef int datatype;
typedef struct node {
datatype data;
struct node *next;
}QNode;
typedef struct {
QNode *front;
QNode *rear;
}LQueue;


LQueue *Init_LQueue(); //建立链队列
void InLQueue(); //入队
int Empty_LQueue(); //判队空
int Out_LQueue(); //出队


void main () {
LQueue *sq;
datatype x1 = 10;
datatype x2;
sq = Init_LQueue();
InLQueue(sq,x1);
Empty_LQueue(sq);
Out_LQueue(sq,x2);
}


//建立一个带头节点的空队
LQueue* Init_LQueue () {
LQueue *q;
QNode *p;

q = malloc(sizeof(LQueue));
p = malloc(sizeof(QNode));
p->next = NULL;
q->front = q->rear = p;
printf("入队数据 : %d\n",q->front->next);
return q;
}


//入队
void InLQueue (LQueue *q, datatype x) {
QNode *p;
p = malloc(sizeof(QNode));
p->data = x;
p->next = NULL;
q->front->next = p;
q->rear = p;
printf("入队数据 : %d\n",q->front->next->data);
}


//判队空
int Empty_LQueue(LQueue *q, datatype x) {
if (q->front == q->rear) {
printf("队空\n");


return 1;
}
else {
printf("队不空\n");
return 0;

}
}


//出队
int Out_LQueue (LQueue *q, datatype *x) {
QNode *p;
if (Empty_LQueue(q)) {
printf("队空");
return -1;
}
else {
p = q->front->next;
q->front->next = p->next;
x = p->data; //队头元素放在x中  error

// free(p);
if (q->front->next == NULL) 
q->rear = q->front; //只有一个元素时,出队后队空,修改队尾元素
printf("出队操作结束\n");
printf("入队数据 : %d\n",q->front->next);
return 1;
}

}



*****用途*****

可以自由控制队列的空间

*****用途*****


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值