c++ - 静态的入栈、出栈、查看、遍历栈元素

标签 c++ c

在插入 5 个元素后,在我的程序中输入代码,我想遍历所有这些元素,但输出仅是 0023,这是不希望的,所以我想更正代码的遍历部分

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define CAPACITY 5
int top=-1;
int stack[CAPACITY];
void push(int);
int isFull(void);
int pop(void);
int isEmpty(void);
void peek(void);
void traverse(void);
void main(void)
{        clrscr();
    int ch,item,i;
    while(1)
    {
        printf(" Options which can be perform : \n");
        printf("1. for push \n");
        printf("2. for pop \n");
        printf("3. for peek\n");
        printf("4. for traverse \n");
        printf("5. for exit \n");

        printf("enter choice u want to enter from above operations : ");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1: printf("enter item u want to push : ");
                scanf("%d",&item);
                push(item);
                break;
            case 2: item=pop();
                if(item==0)
                {
                    printf("stack is underflow\n");
                    }
                    else
                    {
                        printf("pushed element : %d \n",&item);
                    }
                    break;
            case 3: peek();
                break;
            case 4: traverse();
                break;
            case 5: exit(0);
            default:
                 printf("invalid choice enter choice again \n");
        }
    }
}

void push(int ele)
{
    if(isFull())
    {
        printf("stack is overflow\n");
    }
    else
    {
        stack[top]=ele;
        top++;
        printf("Pushed element : %d \n",ele);
    }
}

int isFull(void)
{
    if(top==CAPACITY-1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int pop(void)
{
    if(isEmpty())
    {
        return 0;
    }
    else
    {
        return(stack[top--]);
    }
}

int isEmpty(void)
{
    if(top==-1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

void peek(void)
{
    if(isEmpty())
    {
    printf("stack is underflow\n");
    }
    else
    {
        printf("peek item : %d \n",&stack[top]);
    }
}

void traverse(void)
{
    if(isEmpty())
    {
        printf("stack is underflow \n");
    }
    else
    {
        int i;
        printf("elements of stack are : \n");
        for(i=0;i<=top-1;i++)
        {
            printf("%d\n",&stack[i]);
        }
    }
}

没有错误,这是程序中将元素插入堆栈时的唯一逻辑错误,即 1 2 3 4 5 当我想遍历这些项目时,输出应该返回 1 2 3 4 5 但它返回 0 0 2 3 为什么它是这样并使其正确

最佳答案

你的代码不会从程序中返回,ma​​in应该返回int来退出程序。您的推送操作是错误的,您无法将 stack array[top] 作为 top=-1 推送到 stack array[top] 上,而 top=-1 不能为负数。数组索引应从 0 开始。

修改工作代码 -

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define CAPACITY 5
int top=-1;
int stack[CAPACITY];
void push(int);
int isFull(void);
int pop(void);
int isEmpty(void);
void peek(void);
void traverse(void);
int main(void)
{        clrscr();
    int ch,item,i;
    while(1)
    {
        printf(" Options which can be perform : \n");
        printf("1. for push \n");
        printf("2. for pop \n");
        printf("3. for peek\n");
        printf("4. for traverse \n");
        printf("5. for exit \n");

        printf("enter choice u want to enter from above operations : ");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1: printf("enter item u want to push : ");
                scanf("%d",&item);
                push(item);
                break;
            case 2: item=pop();
                if(item==0)
                {
                    printf("stack is underflow\n");
                    }
                    else
                    {
                        printf("pushed element : %d \n",&item);
                    }
                    break;
            case 3: peek();
                break;
            case 4: traverse();
                break;
            case 5: exit(0);
            default:
                 printf("invalid choice enter choice again \n");
        }
    }
}

void push(int ele)
{
    if(isFull())
    {
        printf("stack is overflow\n");
    }
    else
    { top++; // here you should increment first top from -1 to 0 .coz array indexing is from 0
        stack[top]=ele;
        printf("Pushed element : %d \n",ele);
    }
}

int isFull(void)
{
    if(top==CAPACITY-1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int pop(void)
{
    if(isEmpty())
    {
        return 0;
    }
    else
    {
        return(stack[top--]);
    }
}

int isEmpty(void)
{
    if(top==-1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

void peek(void)
{
    if(isEmpty())
    {
    printf("stack is underflow\n");
    }
    else
    {
        printf("peek item : %d \n",stack[top]); // you dont print addresses you need to print values stored in stack array
    }
}

void traverse(void)
{
    if(isEmpty())
    {
        printf("stack is underflow \n");
    }
    else
    {
        int i;
        printf("elements of stack are : \n");
        for(i=0;i<=top;i++)
        {
            printf("%d\n",stack[i]);  // you dont print addresses you need to print values stored in stack array
        }
    }
}

关于c++ - 静态的入栈、出栈、查看、遍历栈元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57941645/

相关文章:

c++ - 如何设置仅在特定应用程序上单击鼠标的 Hook

c - fork 子中的 munmap 匿名共享内存

c - 使用 ShellExecuteEx() 将 UAC 窗口置于顶部

c - 如何在链接列表中添加和打印项目

C++ 模板和静态成员 - header 中的定义

c++ - QT 程序在 connect() 后崩溃

C 无法检索 mongodb BSON 数组

c - 限制 pthreads 中并发线程执行的数量

c++ - 检测模块 (DLL) 何时卸载

c++ - 测试适用于成员变量的成员函数