一道数据结构的练习题

w372011482 2011-10-11 09:14:57
#include<stdio.h>
#include<stdlib.h>
#define maxsize 10
typedef int elemtype;
typedef struct
{
elemtype data[maxsize];
int front;
int rear;
}cirqueue;

main()
{
char ch;
elemtype x;
cirqueue *q;
void initcqueue(cirqueue **q);
int encqueue(cirqueue *q,elemtype x);
int decqueue(cirqueue *q,elemtype *x);
initcqueue(&q);
printf("\ndo you want to enter data into queue(y/n)?");
ch=getchar();
while(ch=='y')
{
printf("\ninput data:");
scanf("%d",&x);
encqueue(q,x);
printf("\ndo you want to continue(y/n)?");
getchar();
ch=getchar();
}
printf("\ndo you want to continue(y/n)?");
getchar();
ch=getchar();
while(ch=='y')
{
decqueue(q,&x);
printf("\nthe data deleted from queue is:%d",x);
printf("\ndo you want to continue(y/n)?");
getchar();
ch=getchar();
}
}

void initcqueue(cirqueue **q)//这里为什么有用指向指针的指针?
{
(*q)->rear=0;
(*q)->front=0;
}

int encqueue(cirqueue *q,elemtype x)
{
if(q->front==(q->rear+1)%maxsize)
{
printf("\nerror:the queue is full !");
return 0;
}

q->data[q->rear]=x;//这两句看不懂
q->rear =(q->rear+1)%maxsize;
return 1;
}

int decqueue(cirqueue *q,elemtype *x)
{
if(q->front==q->rear)
{
printf("\nerror: the queue is empty !");
return 0;
}
*x=q->data[q->front];
q->front=(q->front+1)%maxsize;
return 1;
}
不知道为什么这个程序运行不了。
...全文
72 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
C_HelloWorld 2011-10-12
  • 打赏
  • 举报
回复
(*q)->rear=0; //没有初始化q
initcqueue中**q是因为在initcqueue(&q);这个语句取指针的地址
q->data[q->rear]=x //赋值,数组下标是q->rear

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧