归纳编程学习的感悟,
记录奋斗路上的点滴,
希望能帮到一样刻苦的你!
如有不足欢迎指正!
共同学习交流!
🌎欢迎各位→点赞 👍+ 收藏⭐ + 留言📝
既然选择了远方,当不负青春,砥砺前行!
SeqStack.h
#pragma once
#include<stdio.h>
#include<stdlib.h>#define MaxSize 100
typedef int DataType;typedef struct
{DataType data[MaxSize];int top;
}SeqStack;void InitSeqStack(SeqStack* S);int StackEmpty(SeqStack S);int GetTop(SeqStack S, DataType* e);int PushStack(SeqStack* S, DataType e);int PopStack(SeqStack* S, DataType* e);int StackLength(SeqStack S);void ClearStack(SeqStack* S);
SeqStack.cpp
#include "SeqStack.h"#define _CRT_SECURE_NO_WARNINGS 1void InitSeqStack(SeqStack* S)
{S->top = -1;
}int StackEmpty(SeqStack S)
{if (S.top == -1){return 1;}else{return 0;}
}int GetTop(SeqStack S,DataType *e)
{if (S.top <= -1){printf("栈已为空!\n");return 0;}*e = S.data[S.top];return 1;
}int PushStack(SeqStack* S, DataType e)
{if (S->top == MaxSize - 1){printf("栈已满,不能将元素入栈!\n");return 0;}S->top++;S->data[S->top] = e;return 1;
}int PopStack(SeqStack* S, DataType* e)
{if (S->top == -1){printf("栈中已经没有元素,不能进行出栈操作!\n");return 0;}*e = S->data[S->top];S->top--;return 1;
}int StackLength(SeqStack S)
{S.top++;return S.top;
}void ClearStack(SeqStack* S)
{S->top = -1;
}
main.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqStack.h"int main()
{SeqStack S;int e;InitSeqStack(&S);PushStack(&S, 10);PushStack(&S, 20);printf("%d\n", StackLength(S));GetTop(S, &e);printf("%d\n", e);PopStack(&S, &e);GetTop(S, &e);printf("%d\n", e);PopStack(&S, &e);printf("%d\n", e);if (StackEmpty(S) == 1){printf("栈已为空\n");}PushStack(&S, 50);GetTop(S, &e);printf("%d\n", e);ClearStack(&S);if (StackEmpty(S) == 1){printf("栈已为空\n");}return 0;
}