顺序栈的实现代码:
主要实现顺序存储栈的各种常见操作,后续,在文章结尾目录里面继续更新源代码,可以收藏一波!
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 10
typedef struct {
int *data;
int top;
}SeqStack,* LinkStack;
void initStack(LinkStack &s) {
s = (SeqStack*)malloc(sizeof(SeqStack));
s->data = (int *)malloc(sizeof(int)*MaxSize);
s->top = -1;
}
bool isEmpty(LinkStack s) {
if (s->top == -1) {
return true;
}
else
{
return false;
}
}
bool isFull(LinkStack s) {
if (s->top == MaxSize - 1) {
return true;
}
else
{
return false;
}
}
bool push(LinkStack &s, int x) {
if (!isFull(s)) {
s->top++;
s->data[s->top] = x;
return true;
}
else
{
printf("栈满\n");
return false;
}
}
bool pop(LinkStack &s, int &x) {
if (!isEmpty(s)) {
x = s->data[s->top--];
return true;
}
else
{
printf("栈空\n");
return false;
}
}
bool getTop(LinkStack s, int &x) {
if (!isEmpty(s)) {
x = s->data[s->top];
return true;
}
else
{
printf("栈空\n");
return false;
}
}
int main() {
LinkStack s;
//SeqStack s;
initStack(s);
push(s, 2);
push(s, 3);
push(s, 4);
int x;
pop(s,x);
printf("%d\n",x);
getTop(s,x);
printf("%d\n", x);
}
查看考研数据结构目录:
https://www.acwing.com/file_system/file/content/whole/index/content/5394132/