数据结构-------顺序栈


#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\n",a[top]);
    top--;
    return top;
}

int main(){
    int a[100];
    int top=-1;
    top=push(a,top,1);
    top=push(a, top, 2);
    top=push(a, top, 3);
    top=push(a, top, 4);

    top=pop(a,  top);
    top=pop(a, top);
    top=pop(a, top);
    top=pop(a, top);
    top=pop(a, top);
    return 0;
}
root@book-virtual-machine:/mnt/hgfs/lua/C++# g++  -std=c++11 salman_0119.cpp -o salman_0119
root@book-virtual-machine:/mnt/hgfs/lua/C++# ./salman_0119
弹栈元素:4
弹栈元素:3
弹栈元素:2
弹栈元素:1
空栈root@book-virtual-machine:/mnt/hgfs/lua/C++#

猜你喜欢

转载自blog.csdn.net/aa804738534/article/details/113740893
今日推荐