3 回答

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個(gè)贊
徹底修改好了,請看:
#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)
{ //初始化順序隊(duì)
q->base = (char *) malloc(maxqsize * sizeof (char));
if (!q->base)
return overlow; //存儲(chǔ)分配失敗
q->front = q->rear = 0;
return ok;
}
int enqueue_sq(sqqueue *q, char e)
{ //e插入隊(duì)尾作為新元素
if ((q->rear + 1) % maxqsize == q->front)
return error; //存儲(chǔ)分配失敗
q->base[q->rear] = e;
q->rear = (q->rear + 1) % maxqsize;
return ok;
}
int dequeue_sq(sqqueue *q, char *e)
{ //出隊(duì),返回首元素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)()) //這里的參數(shù)visit應(yīng)該跟下面的函數(shù)visit格式一致
int queuetraverse_sq(sqqueue q, int(*visit)(char *))
{ //遍歷順序循環(huán)隊(duì)
int i, n;
n = queuelength_sq(&q);
for (i = 0; i < n; i++)
//visit(q.base + i); //應(yīng)該從front遍歷到rear,而不是從base開始,直接遍歷N個(gè)元素
visit(q.base + (q.front+i)%maxqsize);
return ok;
}
//#-----------------這個(gè)函數(shù)寫的問題有問題
/*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));//不知道你這樣寫,有什么意圖,應(yīng)該是下面這句的吧?
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貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊
函數(shù)visit定義時(shí)形參char(q.base)不可用,改成一個(gè)指針char *a即可;
main函數(shù)里:第一個(gè)for循環(huán)結(jié)束后第二行:queuetraverse_sq(q, int(*e));
e不能做queuetraverse_sq(這個(gè)函數(shù)名太長了....我后面寫函數(shù)1好吧)的實(shí)參。函數(shù)1的形參2是一個(gè)指向函數(shù)的指針。實(shí)際上函數(shù)1不需要第二個(gè)參數(shù),在定義的時(shí)候直接寫queuestraverse_sq(sqqueue q){/*....*/}就可以了。
最后把所有函數(shù)1的第二參數(shù)都刪掉就行了。

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超1個(gè)贊
1. 調(diào)用 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 關(guān)注
- 232 瀏覽
添加回答
舉報(bào)