亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如下程序,關于循環隊列的錯誤修改,該怎么辦?

如下程序,關于循環隊列的錯誤修改,該怎么辦?

呼喚遠方 2022-08-04 11:11:22
#include <stdio.h>#include <stdlib.h>#include <malloc.h>#define maxqsize 10#define overlow 0#define error 0#define ok 1typedef 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)()) { //遍歷順序循環隊int i, n;n = queuelength_sq(&q);for (i = 0; i < n; i++)visit(q.base + i);return ok;}int visit(char (q.base)) {printf("%c", *(q.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);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));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));}
查看完整描述

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();//暫停一下
}


查看完整回答
反對 回復 2022-08-08
?
慕哥6287543

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的第二參數都刪掉就行了。

查看完整回答
反對 回復 2022-08-08
?
慕神8447489

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;
}

查看完整回答
反對 回復 2022-08-08
  • 3 回答
  • 0 關注
  • 227 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號