C语言之基于链表队列

2 篇文章 0 订阅
1 篇文章 0 订阅
/*
内容:基于链表的队列
作者:Carre
日期:2015.9.18
*/
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>


//定义队列结点数据类型
typedef int DataType;
//定义队列结点结构体
typedef struct node{
DataType data;
struct node *next;
}Q_node;
//定义队列结构体
typedef struct list{
Q_node *front;
Q_node *rear;
}Q_list;
//初始化队列
void Init_queue(Q_list *L){
L->front=NULL;
L->rear=NULL;
}
//判断是否继续操作
void Judge(char *ch){
scanf("%*[^\n]");
scanf("%*c");
scanf("%c",ch);
}
//打印队列
void print_queue(const Q_list *L){
Q_node *p_front,*p_rear;
p_front=L->front;
p_rear=L->rear;
if(p_front==NULL&&p_rear==NULL){
printf("队列为空!\n");
return;
}
while(p_front!=p_rear){
printf("%d->",p_front->data);
p_front=p_front->next;
}
printf("%d\n",p_rear->data);
}
//队空
bool empty(Q_list *L){
return L->front==NULL||L->rear==NULL;
}


//创建队列
void Create_queue(Q_list *L){
Q_node *tmp;
char flag;
while(1){
tmp=(Q_node *)malloc(sizeof(Q_node));
// tmp->next=NULL;
printf("请输入队列结点数据:");
scanf("%d",&tmp->data);
if(L->front==NULL&&L->rear==NULL){
L->front=tmp;
L->rear=tmp;
}
else{
tmp->next=L->front;
L->front=tmp;
}
printf("是否继续操作y/n:");
Judge(&flag);
if(flag=='y');
else break;
}
}
//入队
bool push(Q_list *L,DataType d){
Q_node *tmp;
tmp=(Q_node *)malloc(sizeof(Q_node));
tmp->data=d;
tmp->next=NULL;
if(L->front==NULL&&L->rear==NULL){
L->front=tmp;
L->rear=tmp;
return true;
}
else{
tmp->next=L->front;
L->front=tmp;
return true;
}
}
//出队
bool pop(Q_list *L){
Q_node *tmp=L->front,*p=NULL;
if(empty(L)) return false;
else{
while(tmp->next!=L->rear){
tmp=tmp->next;
}
tmp->next=NULL;
p=L->rear;
L->rear=tmp;
free(p);
return true;
}
}


int main(){
//定义队列
Q_list queue;
//初始化队列
Init_queue(&queue);
//创建队列
Create_queue(&queue);
//打印队列
print_queue(&queue);
DataType d;//等待入队结点数据域数据
char flag;//判断是否继续操作
while(1){
//入队
printf("请输入入对结点数据:");
scanf("%d",&d);
push(&queue,d);
//打印队列
print_queue(&queue);
//出队
pop(&queue);
//打印队列
printf("出对后结果:\n");
print_queue(&queue);
printf("是否继续操作y/n:");
Judge(&flag);
if(flag=='y');
else break;
}
printf("\n");
return 0;

}









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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值