目录
前言
一、什么是顺序栈
二、顺序栈的表示和实现
1.初始化
2.销毁
3.清空
4.栈空
5.栈长度
6.栈顶元素
7.入栈
8.出栈
8.遍历栈
9.测试代码
前言
这篇文章主要讲栈的顺序表示和实现。
一、什么是顺序栈
顺序栈,即栈的顺序存储结构是利用一组地址连续的存储单元依次存放自栈底到栈顶的数据元素,同时附设指针 top 指示栈顶元素在顺序栈中的位置。通常的习惯做法是以 top=0 表示空栈,鉴于C语言中数组的下标约定从0开始,则当以C作描述语言时,如此设定会带来很大不便;另一方面,由于栈在使用过程中所需最大空间的大小很难估计因此,一般来说,在初始化设空栈时不应限定栈的最大容量。一个较合理的做法是:先为栈分配一个基本容量,然后在应用过程中,当栈的空间不够使用时再逐段扩大。
因此设定两个常量:STACK_INIT_SIZE(存储空间初始分配量)和 STACKINCREMENT(存储空间分配增量),并以下述类型说明作为顺序栈的定义。
顺序栈的定义如下:
#define STACK_INIT_SIZE 100 // 存储空间初始分配量
#define STACKINCREMENT 10 // 存储空间分配增量
#define ElementType int
typedef int Status;typedef struct{ElementType * base;// 栈底指针ElementType * top; // 栈顶指针int stacksize; // 栈的当前可以使用的最大容量
}SeqStack;
二、顺序栈的表示和实现
1.初始化
初始化顺序栈,分配存储空间。
Status initSqStack(SqStack *sqStack){sqStack->base = (ElementType *)malloc(sizeof(ElementType) * STACK_INIT_SIZE);if (!sqStack->base) {return 0;}sqStack->top = sqStack->base;sqStack->stacksize = STACK_INIT_SIZE;return 1;
}
2.销毁
释放栈的存储空间,置空栈顶和栈底。
// 如果栈存在,销毁栈
void destroySqStack(SqStack *sqStack){free(sqStack->base);//释放栈基址的存储空间sqStack->base = sqStack->top = NULL;//栈顶和栈底置空sqStack->stacksize = 0;//栈大小置零
}
3.清空
void clearSqStack(SqStack * sqStack){sqStack->top = sqStack->base;
}
4.栈空
// 栈空
Status sqStackEmpty(SqStack *sqStack){return sqStack->top == sqStack->base;
}
5.栈长度
// 栈的长度
int sqStackLength(SqStack * sqStack){return (int)(sqStack->top - sqStack->base);
}
6.栈顶元素
// 获取栈顶元素
Status getSqStackTop(SqStack * sqStack,int *element){if (sqStack->top == sqStack->base) {return 0;}*element = *(sqStack->top - 1);return 1;
}
7.入栈
// 入栈
Status pushSqStack(SqStack *sqStack, ElementType element) {if (sqStack->top - sqStack->base >= sqStack->stacksize) { // 栈满sqStack->base = (ElementType *)realloc(sqStack->base, (sqStack->stacksize + STACKINCREMENT) * sizeof(ElementType));if (!sqStack->base) {return 0;}sqStack->top = sqStack->base + sqStack->stacksize; // 更新栈顶指针位置sqStack->stacksize = sqStack->stacksize + STACKINCREMENT;}*sqStack->top++ = element;return 1;
}
8.出栈
// 出栈
Status popSqStack(SqStack * sqStack,int * element){if (sqStack->top == sqStack->base) {//栈空return 0;}*element = * (--sqStack->top);return 1;
}
8.遍历栈
void traverseSqStack(SqStack * sqStack) {if (sqStackEmpty(sqStack)) {printf("栈为空\n");return;}int *p = sqStack->base;while (p < sqStack->top) {printf("%d\t", *p++);}printf("\n");
}
9.测试代码
#include <stdlib.h>
#include <stdio.h>#define STACK_INIT_SIZE 100 // 存储空间初始分配量
#define STACKINCREMENT 10 // 存储空间分配增量
#define ElementType int
typedef int Status;typedef struct{ElementType * base;// 栈底指针ElementType * top; // 栈顶指针int stacksize; // 栈的当前可以使用的最大容量
}SqStack;
Status initSqStack(SqStack *sqStack){sqStack->base = (ElementType *)malloc(sizeof(ElementType) * STACK_INIT_SIZE);if (!sqStack->base) {return 0;}sqStack->top = sqStack->base;sqStack->stacksize = STACK_INIT_SIZE;return 1;
}// 如果栈存在,销毁栈
void destroySqStack(SqStack *sqStack){free(sqStack->base);//释放栈基址的存储空间sqStack->base = sqStack->top = NULL;//栈顶和栈底置空sqStack->stacksize = 0;//栈大小置零
}void clearSqStack(SqStack * sqStack){sqStack->top = sqStack->base;
}// 栈空
Status sqStackEmpty(SqStack *sqStack){return sqStack->top == sqStack->base;
}// 栈的长度
int sqStackLength(SqStack * sqStack){return (int)(sqStack->top - sqStack->base);
}// 获取栈顶元素
Status getSqStackTop(SqStack * sqStack,int *element){if (sqStack->top == sqStack->base) {return 0;}*element = *(sqStack->top - 1);return 1;
}// 入栈
Status pushSqStack(SqStack *sqStack, ElementType element) {if (sqStack->top - sqStack->base >= sqStack->stacksize) { // 栈满sqStack->base = (ElementType *)realloc(sqStack->base, (sqStack->stacksize + STACKINCREMENT) * sizeof(ElementType));if (!sqStack->base) {return 0;}sqStack->top = sqStack->base + sqStack->stacksize; // 更新栈顶指针位置sqStack->stacksize = sqStack->stacksize + STACKINCREMENT;}*sqStack->top++ = element;return 1;
}// 出栈
Status popSqStack(SqStack * sqStack,int * element){if (sqStack->top == sqStack->base) {//栈空return 0;}*element = * (--sqStack->top);return 1;
}void traverseSqStack(SqStack * sqStack) {if (sqStackEmpty(sqStack)) {printf("栈为空\n");return;}int *p = sqStack->base;while (p < sqStack->top) {printf("%d\t", *p++);}printf("\n");
}void testStack(void){SqStack sqStack;printf("栈初始化中...\n");if (initSqStack(&sqStack)) {printf("栈初始化成功\n");}else{printf("栈初始化失败\n");}printf("入栈...\n");if (pushSqStack(&sqStack, 1)) {printf("入栈成功\n");}if (pushSqStack(&sqStack, 2)) {printf("入栈成功\n");}if (pushSqStack(&sqStack, 3)) {printf("入栈成功\n");}printf("栈中元素:\n");traverseSqStack(&sqStack);printf("栈的长度:%d\n",sqStackLength(&sqStack));if (!sqStackEmpty(&sqStack)) {printf("栈不为空\n");}int top;if (getSqStackTop(&sqStack, &top)) {printf("栈顶元素获取成功\n");}printf("栈顶元素:%d\n",top);//出栈测试int element;if (popSqStack(&sqStack, &element)) {printf("出栈成功,出栈元素为:%d\n",element);}if (popSqStack(&sqStack, &element)) {printf("出栈成功,出栈元素为:%d\n",element);}if (popSqStack(&sqStack, &element)) {printf("出栈成功,出栈元素为:%d\n",element);}printf("栈的长度:%d\n",sqStackLength(&sqStack));if (sqStackEmpty(&sqStack)) {printf("栈为空\n");}destroySqStack(&sqStack);
}