LinkList_Queue(链式队列)

#include<stdio.h>
typedef int Datatype;
typedef struct link_node {
Datatype info;
struct link_node *next;
}node;
typedef struct Queue {
node *front, *rear;
}queue;


queue *init()
{
queue *q;
q = (queue *)malloc(sizeof(queue));
node *n = (node *)malloc(sizeof(node));
n->next = NULL;
q->front = n;
q->rear = n;
return q;
}


int empty(queue *q)
{
if (q->front->info == NULL)
return 1;
else
return 0;
}


void display(queue *q)
{
if (!q->front)
printf("栈为空,无法进行输出!\n");
else
{
node *p = q->front;
while (p)
{
printf("%5d", p->info);
p = p->next;
}
}
}


queue *insert(queue *q, Datatype x)
{
node *p, *l=q->rear;
p = (node *)malloc(sizeof(node));
p->info = x;
p->next = NULL;
if (!empty(q))
q->front=q->rear = p;
else
{
q->rear->next = p;
q->rear = p;
}
return q;
}




queue *dele(queue *q)
{
queue *p = q;
if (!p)
printf("栈为空,无法进行删除!\n");
else
{
node *n = q->front;
q->front = q->front->next;
free(n);
}
return q;
}


void main()
{
queue *Q;
Q = init();
int i;
for (i = 0; i<10; ++i)
Q = insert(Q, i*2);
display(Q);
putchar('\n');
printf("队头结点的值为:%d\n", Q->front->info);
Q = insert(Q, 666);
printf("666进队:");
display(Q);
putchar('\n');
Q = dele(Q);
printf("删除队头的结点后为:");
display(Q);
putchar('\n');
Q = dele(Q);
printf("删除队头的结点后为:");
display(Q);
putchar('\n');
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值