GitHub地址:https://github.com/VicentWYS/DataStructure---SqStack
顺序栈:
项目文件结构:
main.h:

#include <iostream> #include<stdio.h> #include<stdlib.h> #include "function_for_Stack.h" using namespace std; int main() { TestStack(); return 0; }
function_for_Stack.h:

#ifndef FUNCTION_FOR_STACK_H_INCLUDED #define FUNCTION_FOR_STACK_H_INCLUDED #define MAXSIZE 100 typedef char ElemType; typedef struct{ ElemType *top; //栈顶指针 ElemType *base; //栈底指针 int stacksize; //栈最大容量 }SqStack; //初始化 //分配空间,top,base指针都指向0号位置 int InitStack(SqStack &S); //判断栈是否为空 int StackEmpty(SqStack S); //求顺序栈长度 //栈中元素个数 int StackLength(SqStack S); //清空顺序栈 void ClearStack(SqStack &S); //销毁顺序栈 void DestroyStack(SqStack &S); //入栈 void Push(SqStack &S, ElemType e); //出栈 ElemType Pop(SqStack &S); //打印栈中元素(自底向上) void printStack(SqStack S); //测试 void TestStack(); #endif // FUNCTION_FOR_STACK_H_INCLUDED
function_for_Stack.cpp:

#include<stdio.h> #include<stdlib.h> #include "function_for_Stack.h" //初始化 //分配空间,top,base指针都指向0号位置 int InitStack(SqStack &S){ //构造一个空栈 S.base = (ElemType*)malloc(MAXSIZE * sizeof(ElemType)); if(S.base == NULL){ //如果存储分配失败 exit(0); } S.top = S.base; //栈顶指针指向栈底 S.stacksize = MAXSIZE; return 1; } //判断栈是否为空 int StackEmpty(SqStack S){ if(S.top == S.base){ return 1; }else{ return 0; } } //求顺序栈长度 //栈中元素个数 int StackLength(SqStack S){ return (S.top - S.base); } //清空顺序栈 void ClearStack(SqStack &S){ if(S.base){ S.top = S.base; } } //销毁顺序栈 void DestroyStack(SqStack &S){ if(S.base){ free(S.base); S.stacksize = 0; S.base = NULL; S.top = NULL; } } //入栈 void Push(SqStack &S, ElemType e){ //先判断栈是否已满 if(S.top - S.base == S.stacksize) exit(0); *S.top = e; S.top++; } //出栈 ElemType Pop(SqStack &S){ //先判断栈是否为空 if(S.top == S.base) return 0; S.top--; return *S.top; } //打印栈中元素(自底向上) void printStack(SqStack S){ ElemType *p = S.base; //设置游标,指向栈底 while(S.top - p >=1){ //游标还没到栈顶 printf("%c ", *p); //输出游标所指的元素 p++; } printf(" "); } //测试 void TestStack(){ SqStack S; printf("初始化顺序栈:%d ", InitStack(S)); //插入元素 printf("入栈: "); Push(S, 'a'); Push(S, 'b'); Push(S, 'c'); Push(S, 'd'); Push(S, 'e'); printf(" 栈是否为空:%d ", StackEmpty(S)); printf("栈元素个数:%d ", StackLength(S)); printf("打印栈中元素: "); printStack(S); printf(" 出栈:%c ", Pop(S)); printf("出栈:%c ", Pop(S)); printf("出栈:%c ", Pop(S)); printf("出栈:%c ", Pop(S)); printf("出栈:%c ", Pop(S)); printf(" 栈是否为空:%d ", StackEmpty(S)); printf("栈元素个数:%d ", StackLength(S)); printf("打印栈中元素: "); printStack(S); printf("再次入栈: "); Push(S, 'a'); Push(S, 'b'); Push(S, 'c'); Push(S, 'd'); Push(S, 'e'); printf(" 栈是否为空:%d ", StackEmpty(S)); printf("栈元素个数:%d ", StackLength(S)); printf("打印栈中元素: "); printStack(S); printf("清空栈: "); ClearStack(S); printf(" 栈是否为空:%d ", StackEmpty(S)); printf("栈元素个数:%d ", StackLength(S)); printf("打印栈中元素: "); printStack(S); printf("再次入栈: "); Push(S, 'a'); Push(S, 'b'); Push(S, 'c'); Push(S, 'd'); Push(S, 'e'); printf(" 栈是否为空:%d ", StackEmpty(S)); printf("栈元素个数:%d ", StackLength(S)); printf("打印栈中元素: "); printStack(S); printf("销毁栈 "); DestroyStack(S); printf(" 栈是否为空:%d ", StackEmpty(S)); printf("栈元素个数:%d ", StackLength(S)); printf("打印栈中元素: "); printStack(S); }
运行结果: