我為使用堆疊的程式創建的 2 個函式存在問題。但是,我不能確定兩個函式是否同時出現錯誤,因為它們是相互關聯的。因此,當將資料添加到堆疊及其后續輸出時,我得到的輸出并不完全正確。
input the 1 th element :1
input the 2 th element :2
input the 3 th element :3
所以如果輸入是這樣的,當我選擇第三個選項來輸出堆疊中的資料時,我得到了這樣的輸出
the elements in the stack are: 0
0
0
3
0
這是代碼
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXSIZE 10
int i=1,choose;
/* i represents the number of inputted elements; choose represents the identifiers of the options in the menu.*/
int *sptr,*full,*empty;
int stack[MAXSIZE];
void push(void);
void pop(void);
void printInfo(void);
int main(){
sptr=stack; // sptr points to stack[0].
empty=stack; //empty points to stack[0]
full=stack MAXSIZE-1; // full points to stack[9]
do{
printf("\n\t===============STACK ==============\n");
printf("\n\t 1.Push stack");
printf("\n\t 2.Pop stack");
printf("\n\t 3.Print elements of the stack");
printf("\n\t 4.Exit\n");
printf("\n\t Please choose[1-4] :");
scanf("%d",&choose);
switch(choose){
case 1:
push();
break;
case 2:
pop();
break;
case 3:
printInfo();
break;
case 4:
exit(0);
default:
printf("\n\n\t==================Input error=================");
break;
}
}while(1);
return 0;
}
void push(void){
sptr=stack 1; // sptr point to the next position of the array
if(sptr==full){
printf("\n\n ........The stack is full.......");
sptr--;
}else{
printf("input the %d th element : ",i );
scanf("%d",sptr);
}
}
void pop(void){
if(sptr!=empty){
sptr--;
i--;
}else{
printf("\n\n\t\t ........the stack is empty.......");
i=1;
}
}
void printInfo(void){
int * temp;
temp=sptr;
printf("\n\n\t the elements in the stack are: ");
do{
if(temp!=empty){
for(i;i>=0;i--)
printf("%d\n",stack[i]);
temp--;
}else{
break;
}
}while(1);
printf("\n\n\t================END===============\n");
}
如果有任何建議或批評,我將不勝感激。謝謝你。
uj5u.com熱心網友回復:
sptr=stack 1;你總是寫到堆疊中的相同位置。
您需要增加它而不是總是將其設定為 stack 1。
sptr=sptr 1;
或者
sptr ;
這與您在內部所做的匹配pop():sptr--;。
這讓你的輸出
===============STACK ==============
1.Push stack
2.Pop stack
3.Print elements of the stack
4.Exit
Please choose[1-4] :1
input the 1 th element : 1
===============STACK ==============
1.Push stack
2.Pop stack
3.Print elements of the stack
4.Exit
Please choose[1-4] :1
input the 2 th element : 2
===============STACK ==============
1.Push stack
2.Pop stack
3.Print elements of the stack
4.Exit
Please choose[1-4] :1
input the 3 th element : 3
===============STACK ==============
1.Push stack
2.Pop stack
3.Print elements of the stack
4.Exit
Please choose[1-4] :3
the elements in the stack are: 0
3
2
1
0
即推送/彈出功能的核心是固定的。
我認為您可能還希望避免未輸入用于推送的輸出中的 0。
為此,我建議在里面printInfo():
while(temp!=empty)
{
printf("%d\n",*temp);
temp--;
}
它可以為您提供以下最終輸出:
the elements in the stack are: 3
2
1
它可以在空格上使用一些作業,但只輸出實際推送的值。
所以是的,一個功能在功能上是錯誤的,另一個至少在誤導上沒有幫助。
uj5u.com熱心網友回復:
這里發生了很多事情,你似乎把事情復雜化了并且感到困惑。讓我們一次看一塊:
#define MAXSIZE 10
int i=1,choose;
/* i represents the number of inputted elements; choose represents the identifiers of the options in the menu.*/
int *sptr,*full,*empty;
int stack[MAXSIZE];
除了出于學術原因之外,維護指標(sptr、full、empty)和計數(i)以索引到存盤(堆疊)中沒有多大意義。實際上,您將相同的資訊存盤在不同的地方(sptr類似于i、full類似于&stack[MAXSIZE]和emptyis stack),這種習慣會導致分歧和難以追蹤的錯誤。
雖然在 C 中可以使用指標算術,但它容易出錯并且是常見的錯誤來源。 i為我們提供了我們需要能夠知道有多少stack正在使用的所有資訊。
此外,陣列索引從0C 開始(它與基數的偏移),即使使用現有的邏輯,將其初始化1為沒有元素時顯然會導致問題。
所以剩下的就是:
#define MAXSIZE 10
int stack[MAXSIZE];
int i=0;
注意:choose僅在 main 范圍內使用時,沒有業務是全球性的,所以我現在將其排除在外。
以下內容現在是多余的,應該洗掉:
sptr=stack; // sptr points to stack[0].
empty=stack; //empty points to stack[0]
full=stack MAXSIZE-1; // full points to stack[9]
main 的其余部分都可以,除了添加了 choose 的定義
int main() {
int choose;
//...
我們現在可以依次處理每個功能。
void push(void){
if(i < MAXSIZE) {
printf("input the %d th element : ",i);
scanf("%d",&stack[i ]);
}else{
printf("\n\n ........The stack is full.......");
}
}
筆記:
- 個人喜好,但我更喜歡先展示“幸福路徑”,它稍微簡化了比較。
- 的后增量
i意味著當i == 0,0用作 的索引時stack,&將運算式轉換為指向偏移量處的值的指標i(這是scanf存盤值的位置所需要的)。
void pop(void) {
if(i > 0) {
i--;
} else {
printf("\n\n\t\t ........the stack is empty.......");
}
}
筆記:
- 再一次,快樂的道路第一,現在簡單多了;-)
void printInfo(void){
printf("\n\n\t the elements in the stack are: ");
for (int pos=0; pos < i ; pos ) {
printf("%d\n", stack[pos]);
}
printf("\n\n\t================END===============\n");
}
筆記:
- 記住我們不想
i在列印時進行修改,我們引入了一個新變數 ,pos來逐步遍歷 中的元素stack,直到當前計數。
完整代碼:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXSIZE 10
int i=1;
int stack[MAXSIZE];
void push(void);
void pop(void);
void printInfo(void);
int main() {
int choose;
do{
printf("\n\t===============STACK ==============\n");
printf("\n\t 1.Push stack");
printf("\n\t 2.Pop stack");
printf("\n\t 3.Print elements of the stack");
printf("\n\t 4.Exit\n");
printf("\n\t Please choose[1-4] :");
scanf("%d",&choose);
switch(choose){
case 1:
push();
break;
case 2:
pop();
break;
case 3:
printInfo();
break;
case 4:
exit(0);
default:
printf("\n\n\t==================Input error=================");
break;
}
}while(1);
return 0;
}
void push(void) {
if (i < MAXSIZE) {
printf("input the %d th element : ",i);
scanf("%d",&stack[i ]);
} else {
printf("\n\n ........The stack is full.......");
}
}
void pop(void) {
if (i > 0) {
i--;
} else {
printf("\n\n\t\t ........the stack is empty.......");
}
}
void printInfo(void) {
printf("\n\n\t the elements in the stack are: ");
for (int pos=0; pos < i ; pos ) {
printf("%d\n", stack[pos]);
}
printf("\n\n\t================END===============\n");
}
push如果你也想要,修改,pop和printInfo獲取指向stack計數 ( ) 的指標應該相當簡單,i這樣你就可以將全域變數移動到 main 的范圍內并將它們傳遞給助手。這減少了全域變數,意味著更容易推斷出 push、pop 和 printInfo 作用于什么(只有輸入,而不是全域變數)。
這將教你更多關于指標的知識,而不會導致壞習慣。
uj5u.com熱心網友回復:
關于for(i;i>=0;i--)你的void printInfo(void),首先你不能使用變數“i”,因為在你使用 printInfo 列印堆疊資訊之后,全域變數“i”將為 0,但其中有元素。
其次,您的 i 不是指輸入元素的數量, i-1 是指輸入元素的數量。
而且Yunnosch的回答也是對的,你的代碼有很多問題。你需要仔細檢查。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/468480.html
標籤:C
上一篇:帶解參考運算子的前綴和后綴
