m0_61733427 2021-10-13 14:44 采纳率: 100%
浏览 256
已结题

设一个顺序栈,进行出栈和入栈操作。

输入若干个整数(不超过1000),依次入栈;scanf("%d",&e)==1来作为输入判断

依次出栈并输出元素值,以空格分隔。

  • 写回答

4条回答 默认 最新

  • CSDN专家-sinJack 2021-10-13 14:56
    关注
    #include <stdio.h>
    //元素elem进栈
    int push(int* a,int top,int elem){
        a[++top]=elem;
        return top;
    }
    //数据元素出栈
    int pop(int * a,int top){
        if (top==-1) {
            printf("空栈");
            return -1;
        }
        printf("%d ",a[top]);
        top--;
        return top;
    }
    int main() {
        int a[1000];
        int top=-1;
        int e,count=0;
        while(scanf("%d",&e)==1){
            top=push(a, top,e);
            count++;
        } 
        for(int i=0;i<count;i++){
            top=pop(a, top);
        }
        return 0;
    }
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • 关注

    定义1000个元素的数组,入栈索引从小到大,出栈索引只允许从大到小。

    评论
  • 关注
    #include <stdio.h>
    #define ROOM 1005
    typedef struct stack
    {
        int space[ROOM];
        int top;
    } Stack;
    int pop(Stack *s)
    {
        if (s->top >= 0)
        {
            printf("%d ", s->space[s->top]);
            s->top--;
            return 1;
        }
        return 0;
    }
    int push(Stack *s, int e)
    {
        if (s->top < ROOM - 1)
        {
            s->top++;
            s->space[s->top] = e;
            return 1;
        }
        return 0;
    }
    int main(int argc, char const *argv[])
    {
        Stack s;
        s.top = -1;
        int e;
        while (scanf("%d", &e) == 1 && push(&s, e))
            ;
        while (pop(&s))
            ;
    
        return 0;
    }
    
    评论
  • qfl_sdu 2021-10-13 14:57
    关注

    栈:先进后出,用链表来做,就是头插法,后进入的在前面,出栈的时候从链表头取。

    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 11月9日
  • 已采纳回答 11月1日
  • 创建了问题 10月13日

悬赏问题

  • ¥15 编译arm板子的gcc
  • ¥20 C语言用栈实现无向图邻接矩阵广度优先遍历
  • ¥15 C++代码报错问题,c++20协程
  • ¥15 c++图Djikstra算法求最短路径
  • ¥15 Linux操作系统中的,管道通信问题
  • ¥15 ansible tower 卡住
  • ¥15 等间距平面螺旋天线方程式
  • ¥15 通过链接访问,显示514或不是私密连接
  • ¥100 系统自动弹窗,键盘一接上就会
  • ¥50 股票交易系统设计(sql语言)