我为使用堆栈的程序创建的两个函数有问题.然而,我不能确定两个函数同时是否存在错误,因为它们是相互关联的.因此,当将数据添加到堆栈及其后续输出中时,我得到的输出并不完全正确.

input the  1 th element :1
input the  2 th element :2
input the  3 th element :3

所以如果输入是这样的,当我 Select 第三个选项来输出堆栈中的数据时,我得到了这样的输出

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

如有任何建议或批评,我将不胜感激.非常感谢.

推荐答案

With sptr=stack+1; you always write to the same position in stack.
You need to increment it instead of always setting it to stack+1.

sptr=sptr+1;
or 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

I.e. the core of the push/pop functionality is fixed.
I think you might also want to avoid the 0 in output which was not entered for pushing.

为此,我推荐inside printInfo():

        while(temp!=empty)
        { 
            printf("%d\n",*temp);   
            temp--;
        }

这将为您提供以下内容的最终输出:

         the elements in the stack are: 3
2
1

它可以对空白进行一些处理,但只输出实际推送的值.

因此,是的,一个功能在功能上是错误的,另一个至少是误导性的没有帮助.

C++相关问答推荐

如何正确地索引C中的 struct 指针数组?

如何解决C中的严格别名?

自定义应用程序上的日志(log)轮换问题

#If指令中未定义宏?

在libwget中启用Cookie会导致分段故障

如何只获取字符串的第一个单词,然后将其与c中的另一个单词进行比较?

处理来自浏览器的HTTP请求

将回调/基于事件的C API转换为非回调API

安全倒计时循环

C";中的ANN运行时判断失败#2-变量outputLayer;周围的堆栈已损坏.运行后出错

C程序向服务器发送TCPRST

未为同一文件中的函数执行DirectFunctionCall

为什么INT_MIN是在c语言的头文件limits.h中定义的(-INT_MAX-1)而不是直接使用-2147483648

变量的指针右对齐,函数的指针左对齐

在链表中插入一个值

nullptr_t 是否会 destruct 类型双关或指针转换?

如何根据当前舍入方向将float转换为int?

c中数组上显示的随机元素

在内存泄漏中获取Syscall param execve(argv) 指向未初始化的字节?

在C语言中,write access violation是什么意思,我该如何解决?