队列的基本操作c语言代码大全,求链队列的一些基本操作

#include

#include

#include

#define OK 1

int yes=0;

typedef struct node

{

char data;

struct node *next;

}node,*queueptr;

typedef struct

{

queueptr front;

queueptr rear;

}linkqueue;

void initqueue(linkqueue *q)

{

q->front=q->rear=(queueptr)malloc(sizeof(node));

if(!(q->front))

exit(1);

q->front->next=0;

}

void enqueue(linkqueue *q,char e)

{

queueptr p;

p=(queueptr)malloc(sizeof(node));

if(!p)exit(1);

p->data=e;

p->next=0;

q->rear->next=p;

q->rear=p;

}

void creatqueue(linkqueue *q)

{

int i,length;

char ch;

queueptr p;

printf("请输入队列长度:\n");

scanf("%d",&length);

printf("请输入队列的元素:\n");

for(i=0;i

{

p=(queueptr)malloc(sizeof(node));

if(!p)exit(1);

scanf("%c",&ch);

if((ch=getchar())!='\n')

{

p->data=ch;

p->next=0;

q->rear->next=p;

q->rear=p;

}

else

i--;

}

yes=1;

}

void destroyqueue(linkqueue *q)

{

if(q)

{

printf("栈队列已经是空队列!\n");

exit(1);

}

while(q->front)

{

q->rear=q->front->next;

free((q->front));

q->front=q->rear;

if(!q->rear)

free(q->rear);

}

free(q->front);

}

void disqueue(linkqueue *q)

{

node *r=q->front->next;

printf("此时的栈队列输出:\n");

while(r)

{

printf("%c\t",r->data);

r=r->next;

}

printf("\n");

}

void lenqueue(linkqueue *q)

{

node *r=q->front->next;

int s=0;

printf("此时的栈队列长度为:\n");

while(r)

{

r=r->next;

s++;

}

printf("%d\n",s);

}

char dequeue(linkqueue *q,char e)

{

node *p;

if(q->front==q->rear)

return -1;

p=q->front->next;

e=p->data;

q->front->next=p->next;

if(q->rear==p)

q->rear=q->front;

free(p);

return e;

}

int queueempty(linkqueue *q)

{

if(q->front==q->rear)

return 1;

else

return 0;

}

void menu()

{

printf("           栈队列试验统一界面               \n");

printf("********************************************\n");

printf("1 建立栈队列!                              *\n");

printf("2 栈队列入队操作!                          *\n");

printf("3 栈队列出队操作!                          *\n");

printf("4 求栈队列长度!                            *\n");

printf("5 判断栈队列是否为空!                      *\n");

printf("6 输出栈队列!                              *\n");

printf("7 销毁栈队列!                              *\n");

printf("0 退出!                                    *\n");

printf("********************************************\n");

}

int main(void)

{

linkqueue ptr;

int sel;

char ch;

initqueue(&ptr);

while(1)

{

menu();

printf("please input command:\n");

scanf("%d",&sel);

switch(sel)

{

case 1:if(yes==1)

{

printf("此时表已创建!不能再次创建!\n");

break;

}

else

creatqueue(&ptr);break;

case 2:if(yes==0)

{

printf("此时表未创建!不能入队!\n");

break;

}

else

{

printf("请输入队的元素值:");

scanf("%c",&ch);

if((ch=getchar())!='\n')

enqueue(&ptr,ch);

break;

}

case 3:if(yes==0)

{

printf("此时表为创建!不能出队!\n");

break;

}

else

{

ch=dequeue(&ptr,ch);

printf("目前出队的元素是%c",ch);

break;

}

case 4:if(yes==0)

{

printf("此时链队列未创建!不能求其长度!\n");

break;

}

lenqueue(&ptr);

break;

case 5:if(yes==0)

{

printf("此时表未创建!不能判断是否为空!\n");

break;

}

else

{

if(queueempty(&ptr))

printf("此时队列为空队列!\n");

else

printf("此时队列为非空队列!\n");

break;

}

case 6:if(yes==0)

{

printf("此时栈队列未创建!不能输出!\n");

break;

}

disqueue(&ptr);

break;

case 7:if(yes==0)

{

printf("此时栈队列未创建!不能销毁!\n");

break;

}

destroyqueue(&ptr);

break;

case 0:exit(1);break;

default:printf("输入命令错误!请重新输入:\n");

break;

}

}

return OK;

}

6f83fddf9cb9ff6843fffd45e1807199.gif

菜鸟也疯狂

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值