其他分享
首页 > 其他分享> > 顺序栈

顺序栈

作者:互联网

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<iostream>

#define MaxSize 100
typedef int elemtype;
typedef struct Sqstack {
    int data[MaxSize];
    int top;
}Sqstack;
//初始化
void initstack(Sqstack* S) {
     S->top = -1;
}
//入栈
void Push(Sqstack* S, elemtype e) {
    if (S->top == MaxSize - 1) {
        printf("栈已满,无法入栈");
        return;
    }
    
    S->top++;
    S->data[S->top] = e;
}
//出栈
void Pop(Sqstack* S, elemtype* e) {
    if (S->top == -1) {
        printf("栈已空,无法出栈");
        return;
    }    
    *e= S->data[S->top];
    S->top--;
}
//打印栈
void Showstack(Sqstack* S) {
    int i = S->top;
    if (S->top == -1) {
        printf("栈已空,无法打印");
    }
    else {
        while (S->top != -1)
        {
            printf("栈元素为:%d\n", S->data[S->top]);
            S->top--;
        }
    }
    S->top = i;

}
int main() {
    Sqstack Sq;
    elemtype e;
    initstack(&Sq);
    Push(&Sq, 10);
    Push(&Sq, 11);
    Push(&Sq, 12);
    Push(&Sq, 13);
    Push(&Sq, 14);
    Push(&Sq, 15);
    Showstack(&Sq);
    Pop(&Sq, &e);
    printf("删除的元素为:%d\n", e);
    Showstack(&Sq);
    system("pause");
    return 0;
}

 

标签:顺序,Sqstack,int,top,printf,Push,Sq
来源: https://www.cnblogs.com/tingwen123/p/16320134.html