第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如下程序,關(guān)于循環(huán)隊(duì)列的錯(cuò)誤修改,該怎么辦?

如下程序,關(guān)于循環(huán)隊(duì)列的錯(cuò)誤修改,該怎么辦?

#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) { //初始化順序隊(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)()) { //遍歷順序循環(huán)隊(duì)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貢獻(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();//暫停一下
}


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

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ù)都刪掉就行了。

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

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

查看完整回答
反對 回復(fù) 2022-08-08
  • 3 回答
  • 0 關(guān)注
  • 232 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號