3 回答
TA贡献1775条经验 获得超11个赞
彻底修改好了,请看:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define maxqsize 10
#define overlow 0
#define error 0
#define ok 1
typedef struct
{
char *base;
int front;
int rear;
} sqqueue;
int initqueue_sq(sqqueue *q)
{ //初始化顺序队
q->base = (char *) malloc(maxqsize * sizeof (char));
if (!q->base)
return overlow; //存储分配失败
q->front = q->rear = 0;
return ok;
}
int enqueue_sq(sqqueue *q, char e)
{ //e插入队尾作为新元素
if ((q->rear + 1) % maxqsize == q->front)
return error; //存储分配失败
q->base[q->rear] = e;
q->rear = (q->rear + 1) % maxqsize;
return ok;
}
int dequeue_sq(sqqueue *q, char *e)
{ //出队,返回首元素e的值
if (q->front == q->rear)
return error;
*e = q->base[q->front];
q->front = (q->front + 1) % maxqsize;
return ok;
}
int queuelength_sq(sqqueue *q) //求对长
{
return (q->rear - q->front + maxqsize) % maxqsize;
}
//int queuetraverse_sq(sqqueue q, int(*visit)()) //这里的参数visit应该跟下面的函数visit格式一致
int queuetraverse_sq(sqqueue q, int(*visit)(char *))
{ //遍历顺序循环队
int i, n;
n = queuelength_sq(&q);
for (i = 0; i < n; i++)
//visit(q.base + i); //应该从front遍历到rear,而不是从base开始,直接遍历N个元素
visit(q.base + (q.front+i)%maxqsize);
return ok;
}
//#-----------------这个函数写的问题有问题
/*int visit(char (q.base))
{
printf("%c", *(q.base));
return ok;
} */
//#---------------------------------
int visit(char *base)
{
printf("%c", *base);
return ok;
}
int main()
{
sqqueue q;
char e;
int i, n, m;
initqueue_sq(&q);
printf("input the number of the datas:");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
//fflush(stdin);//用下面几句代码,替换fflush
int c;
if(feof(stdin) || ferror(stdin))
{
break;
}
while ( (c = getchar()) != '\n' && c != EOF ) ;
printf("enter the queue:");
scanf("%c", &e);
if(!enqueue_sq(&q, e))
{
printf("the queue is full!\n");
break;
}
}
printf("traverse the queue now:");
//queuetraverse_sq(q, int(*e));//不知道你这样写,有什么意图,应该是下面这句的吧?
queuetraverse_sq(q, (*visit));
printf("\nthe length of the queue is %d\n", queuelength_sq(&q));
printf("input the number of the datas to out:");
scanf("%d", &m);
for (i = 0; i < m; i++)
{
printf("output the first data in the queue:");
dequeue_sq(&q, &e);
printf("%c\n", e);
}
printf("traverse the queue now:");
queuetraverse_sq(q, (*visit));
printf("\nthe length of the queue is %d\n", queuelength_sq(&q));
getchar();getchar();//暂停一下
}
TA贡献1831条经验 获得超10个赞
函数visit定义时形参char(q.base)不可用,改成一个指针char *a即可;
main函数里:第一个for循环结束后第二行:queuetraverse_sq(q, int(*e));
e不能做queuetraverse_sq(这个函数名太长了....我后面写函数1好吧)的实参。函数1的形参2是一个指向函数的指针。实际上函数1不需要第二个参数,在定义的时候直接写queuestraverse_sq(sqqueue q){/*....*/}就可以了。
最后把所有函数1的第二参数都删掉就行了。
TA贡献1780条经验 获得超1个赞
1. 调用 queuetraverse_sq
queuetraverse_sq(q, int(*e));
-->>
queuetraverse_sq(q, (*visit));
2. 定义queuetraverse_sq
int queuetraverse_sq(sqqueue q, int(*visit)())
-->>
int queuetraverse_sq(sqqueue q, int(*visit)(char*))
3. 定义visit
int visit(char (q.base)) {
printf("%c", *(q.base));
return ok;
}
-->>
int visit(char* base) {
printf("%c", *base);
return ok;
}
- 3 回答
- 0 关注
- 196 浏览
添加回答
举报