栈作为一种数据结构,是一种只能在一端进行插入和删除操作的特殊线性表。它按照后进先出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后一个数据被第一个读出来)。
栈的基本操作有:
1:构造一个空栈 status InitStack(SqStack& S)
//构造一个空栈
status InitStack(SqStack& S) {
S.base = (Selemtype*)malloc(STACK_INIT_SIZE * sizeof(Selemtype));
if (!S.base)
return Error;
S.top = S.base;
S.stasksise = STACK_INIT_SIZE;
return OK;
}
2:若栈不空,用元素返回栈的栈顶元素 status Getelem(SqStack S, Selemtype& e)
//若栈不空,用元素返回栈的栈顶元素
status Getelem(SqStack S, Selemtype& e) {
if (S.top == S.base)
return Error;
e = *(S.top - 1);
return OK;
}
3:插入新的栈顶元素 status Push(SqStack& S, Selemtype e)
插入新的栈顶元素
status Push(SqStack& S, Selemtype e) {
if (S.top - S.base >= S.stasksise)
{
S.base = (Selemtype*)realloc(S.base, (STACKINCREMENT + STACK_INIT_SIZE) * sizeof(Selemtype));
if (!S.base)
return Error;
S.top = S.base + S.stasksise;
S.stasksise += STACKINCREMENT;
}
*S.top=e;
S.top++;
return OK;
}
4:删除栈顶元素 status Pop(SqStack& S, Selemtype& e)
//删除栈顶元素
status Pop(SqStack& S, Selemtype& e) {
if (S.top == S.base)
return Error;
e = *--S.top;
return OK;
}
5:向栈中输入n个元素 status CreatStack(SqStack& S, int n)
//向栈中输入n个元素
status CreatStack(SqStack& S, int n) {
while (S.top - S.base < S.stasksise&&n>0) {
Selemtype e;
scanf_s("%d", &e);
*S.top++ = e;
n--;
}
return OK;
}
6:输出栈中的元素 status PrintfStack(SqStack S)
//输出栈中的元素
status PrintfStack(SqStack S) {
Selemtype* p;
p = --S.top;
while (p>=S.base) {
printf("%d ", *p);
p--;
}
return OK;
}
7:返回栈的长度 status StackLength(SqStack S)
//返回栈的长度
status StackLength(SqStack S) {
int Length;
Length = S.top - S.base;
printf("%d", Length);
return Length;
}
8:销毁栈 status DestroyStack(SqStack& S)
//销毁栈
status DestroyStack(SqStack& S) {
S.top = S.base;
return OK;
}
具体代码如下
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define Error 0
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int Selemtype;
typedef int status;
//栈的顺序储存表示
typedef struct {
Selemtype* top;
Selemtype* base;
int stasksise;
}SqStack;
//构造一个空栈
status InitStack(SqStack& S) {
S.base = (Selemtype*)malloc(STACK_INIT_SIZE * sizeof(Selemtype));
if (!S.base)
return Error;
S.top = S.base;
S.stasksise = STACK_INIT_SIZE;
return OK;
}
//向栈中输入n个元素
status CreatStack(SqStack& S, int n) {
while (S.top - S.base < S.stasksise && n>0) {
Selemtype e;
scanf_s("%d", &e);
*S.top++ = e;
n--;
}
return OK;
}
//若栈不空,用元素返回栈的栈顶元素
status Getelem(SqStack S, Selemtype& e) {
if (S.top == S.base)
return Error;
e = *(S.top - 1);
return OK;
}
//插入新的栈顶元素
status Push(SqStack& S, Selemtype e) {
if (S.top - S.base >= S.stasksise)
{
S.base = (Selemtype*)realloc(S.base, (STACKINCREMENT + STACK_INIT_SIZE) * sizeof(Selemtype));
if (!S.base)
return Error;
S.top = S.base + S.stasksise;
S.stasksise += STACKINCREMENT;
}
*S.top=e;
S.top++;
return OK;
}
//删除栈顶元素
status Pop(SqStack& S, Selemtype& e) {
if (S.top == S.base)
return Error;
e = *--S.top;
return OK;
}
//输出栈中的元素
status PrintfStack(SqStack S) {
Selemtype* p;
p = --S.top;
while (p>=S.base) {
printf("%d ", *p);
p--;
}
return OK;
}
//返回栈的长度
status StackLength(SqStack S) {
int Length;
Length = S.top - S.base;
printf("%d", Length);
return Length;
}
//销毁栈
status DestroyStack(SqStack& S) {
S.top = S.base;
return OK;
}
//主函数
int main() {
SqStack S;
InitStack(S);
int n,e;
printf("输入栈的长度\n");
scanf_s("%d", &n);
printf("输入栈中的元素\n");
CreatStack(S, n);
printf("删除栈顶元素\n");
Pop(S, e);
printf("删除的栈顶元素为%d\n",e);
printf("插入栈顶元素3\n");
Push(S, 3);
printf("返回栈的长度\n");
StackLength(S);
printf("返回栈的元素");
PrintfStack(S);
printf("销毁栈\n");
DestroyStack(S);
return 0;
}