2-1 栈——顺序栈(C/C++语言)
实现的功能
void InitStack(SqStack0& S);//初始化一个栈
bool StackEmpty(SqStack0& S);//判断栈是否为空
bool Push(SqStack0& S, int x);//进栈
bool Pop(SqStack0& S, int& x);//出栈
bool GetTop(SqStack0 S, int& x);//读栈顶元素
void ClearStack(SqStack0& S);//清空栈
top在元素所在位置,即初始化top=-1
完整代码:
/*
Name:SqStack0(顺序栈top为-1)
Copyright:
Author: Sdjzu_Nxy
Date: 2023 年 2 月 27 日
Description:
*/
#include<stdio.h>
#include<stdbool.h>
#define MaxSize 10 //定义最大长度
typedef struct {
int data[MaxSize]; //用静态的“数组”存放数据元素
int top; //栈顶指针
int stackSize;
}SqStack0; //类型定义
//初始化一个栈
void InitStack(SqStack0& S) {
S.top = -1;
S.stackSize = 0;
}
//判断栈是否为空
bool StackEmpty(SqStack0 &S) {
//if (S.top == -1) {
// return true;
//}
//else
//{
// return false;
//}
//return S.stackSize == 0 ? true : false;
return S.top == -1 ? true : false;
}
//进栈
bool Push(SqStack0& S, int x) {
//if (S.stackSize == MaxSize)
//{
// return false;
//}
if (S.top == MaxSize - 1)
{
return false;
}
S.data[++S.top] = x;
S.stackSize++;
return true;
}
//出栈
bool Pop(SqStack0& S, int& x) {
//if (S.top == -1)
//{
// return false;
//}
if (S.stackSize == 0)
{
return false;
}
x = S.data[S.top--];
S.stackSize--;
return true;
}
//读栈顶元素
bool GetTop(SqStack0 S, int& x) {
if (S.top == -1)
{
return false;
}
x = S.data[S.top];
return true;
}
//清空栈
void ClearStack(SqStack0& S) {
while (S.stackSize != 0)
{
S.data[S.top--] = 0;
S.stackSize--;
}
}
int main() {
SqStack0 S;
InitStack(S);
printf("The SqStack is Empty? %d\n", StackEmpty(S));
Push(S, 1);
Push(S, 2);
Push(S, 3);
int x;
if (GetTop(S, x)) {
printf("The Sqstack Top data is %d.\n", x);
}
if (Pop(S, x))
{
printf("Pop SqStack data is %d.\n", x);
}
if (Pop(S, x))
{
printf("Pop SqStack data is %d.\n", x);
}
printf("The SqStack size is %d.\n", S.stackSize);
ClearStack(S);
printf("The SqStack size is %d, it is clear!", S.stackSize);
}
运行结果:
top在元素上一个位置,即top=0
完整代码:
/*
Name:SqStack0(顺序栈top为0)
Copyright:
Author: Sdjzu_Nxy
Date: 2023 年 2 月 27 日
Description:
*/
#include<stdio.h>
#include<stdbool.h>
#define MaxSize 10 //定义最大长度
typedef struct {
int data[MaxSize]; //用静态的“数组”存放数据元素
int top; //栈顶指针
int stackSize;
}SqStack1; //类型定义
void InitStack(SqStack1& S); //初始化一个栈
bool StackEmpty(SqStack1& S); //判断栈是否为空
bool Push(SqStack1& S, int x); //进栈
bool Pop(SqStack1& S, int& x); //出栈
bool GetTop(SqStack1 S, int& x); //读栈顶元素
void ClearStack(SqStack1& S); //清空栈
//初始化一个栈
void InitStack(SqStack1& S) {
S.top = 0;
S.stackSize = 0;
}
//判断栈是否为空
bool StackEmpty(SqStack1& S) {
//if (S.top == 0) {
// return true;
//}
//else
//{
// return false;
//}
//return S.stackSize == 0 ? true : false;
return S.top == 0 ? true : false;
}
//进栈
bool Push(SqStack1& S, int x) {
//if (S.stackSize == MaxSize)
//{
// return false;
//}
if (S.top == MaxSize)
{
return false;
}
S.data[S.top++] = x;
S.stackSize++;
return true;
}
//出栈
bool Pop(SqStack1& S, int& x) {
//if (S.top == 0)
//{
// return false;
//}
if (S.stackSize == 0)
{
return false;
}
x = S.data[--S.top];
S.stackSize--;
return true;
}
//读栈顶元素
bool GetTop(SqStack1 S, int& x) {
if (S.top == 0)
{
return false;
}
x = S.data[S.top - 1];
return true;
}
//清空栈
void ClearStack(SqStack1& S) {
while (S.stackSize != 0)
{
S.data[--S.top] = 0;
S.stackSize--;
}
}
int main() {
SqStack1 S;
InitStack(S);
printf("The SqStack is Empty? %d\n", StackEmpty(S));
Push(S, 1);
Push(S, 2);
Push(S, 3);
int x;
if (GetTop(S, x)) {
printf("The Sqstack Top data is %d.\n", x);
}
if (Pop(S, x))
{
printf("Pop SqStack data is %d.\n", x);
}
if (Pop(S, x))
{
printf("Pop SqStack data is %d.\n", x);
}
printf("The SqStack size is %d.\n", S.stackSize);
ClearStack(S);
printf("The SqStack size is %d, it is clear!", S.stackSize);
}
运行结果:
扩展:共享栈
采用一个一维数组表示两个栈,其目的是更有效的利用存储空间,如图所示:
top0=-1 0号栈为空;
top1=MaxSize 1号栈为空;
栈满:top0+1==top1
代码实现不再赘述。