c - C 中使用数组、入栈、弹出和显示的堆栈实现

标签 c

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

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

最佳答案

使用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

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

所以,是的,一个函数在功能上是错误的,另一个函数至少具有误导性,没有帮助。

关于c - C 中使用数组、入栈、弹出和显示的堆栈实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72067354/

相关文章:

c - 信号量阻塞 child

c - 隐藏终端上的密码输入

c - 随时退出循环

c - 如何将 noreturn 与函数指针一起使用?

c++ - 快速计算数组中零值字节的数量

c - 静态内存与堆内存?

c - 在 Ubuntu 10.04 上编译时未声明 PATH_MAX

c - 即使设置 PATH 变量后也是 "gcc is not recognized"。剩下什么?

python - 传递带有指向 ctypes 中其他结构的指针的结构

c - MIPS 结构节点