顺序栈基本操作的C语言实现(含全部代码实现)--- 数据结构之顺序栈
生活随笔
收集整理的這篇文章主要介紹了
顺序栈基本操作的C语言实现(含全部代码实现)--- 数据结构之顺序栈
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、存儲結(jié)構(gòu)
#define Stack_Init_Size 100 #define StackIncrement 10 typedef int ElemType; typedef int Status;// 方式一(本文采取) typedef struct {ElemType *base; // 棧底指針ElemType *top; // 棧頂指針int stacksize; // 棧的最大容量 } SqStack;// 方式二 typedef struct {int data[MaxSize];int top; } SeqStack;2、函數(shù)列表
- Status InitStack(SqStack *S) 初始化棧
- Status GetTopStack(SqStack *S, ElemType *e) 獲取棧頂元素,參數(shù)e存放棧頂元素的值
- Status PushStack(SqStack *S, ElemType e) 進棧,將元素e入棧
- Status PopStack(SqStack *S, ElemType *e) 出棧,出棧的元素存放在參數(shù)e中
- Status EmptyStack(SqStack *S) 判斷棧是否為空
- Status LengthStack(SqStack *S) 獲取棧的實際長度
- Status DestroyStack(SqStack *S) 銷毀棧
- Status StackTraverse(SqStack *S) 遍歷棧,打印每個元素
3、完整代碼
#include <stdio.h> #include <stdlib.h>#define Stack_Init_Size 10 // 初始化棧的最大長度 #define StackIncrement 10 // 若棧最大空間不夠時,需要增加的長度 typedef int ElemType; typedef int Status;typedef struct {ElemType *base; // 棧底指針ElemType *top; // 棧頂指針int stack_size; // 棧的最大長度 } SqStack;// 初始化棧 Status InitStack(SqStack *S) {// 分配初始空間S->base = (ElemType *) malloc(Stack_Init_Size * sizeof(ElemType));if (!S->base) {exit(0);}S->top = S->base; /// 棧頂與棧底相同S->stack_size = Stack_Init_Size; // 棧的最大長度等于初始長度return 1; }// 判斷棧是否為空,只需要判斷棧頂指針與棧底指針是否相同即可 Status EmptyStack(SqStack *S) {return S->base == S->top; }// 獲取棧的實際長度,棧頂減去棧底指針即為棧的長度 Status LengthStack(SqStack *S) {if (S->top == S->base) {return 0;}return (Status) (S->top - S->base); }// 獲取棧頂?shù)脑?#xff0c;參數(shù)e用來存放棧頂?shù)脑?/span> Status GetTopStack(SqStack *S, ElemType *e) {if (S->top == S->base) {return 0;} *e = *(S->top - 1);return 1; }// 進棧,參數(shù)e是要進棧的元素 Status PushStack(SqStack *S, ElemType e) {// 若棧的最大長度不會夠用時,重新開辟,增大長度if (S->top - S->base >= S->stack_size) {S->base = (ElemType *)realloc(S->base, (S->stack_size + StackIncrement) * sizeof(ElemType));if (!S->base) {return 0;}// 棧頂指針為棧底指針加上棧之前的最大長度S->top = S->base + S->stack_size;// 棧當前的最大長度等于棧之前的最大長度與增加的長度之和S->stack_size += StackIncrement;}*S->top++ = e; // 先賦值,后棧頂指針上移return 1; }// 出棧,參數(shù)e用來存放出棧的元素 Status PopStack(SqStack *S, ElemType *e) {if (S->base == S->top) {return 0;}*e = *--S->top; // 棧頂指針先下移,后賦值return 1; }// 銷毀棧,釋放棧空間,棧頂棧底指針置為NULL,長度置為0 Status DestroyStack(SqStack *S) {free(S->base);S->base = S->top = NULL;S->stack_size = 0;return 1; }// 遍歷棧,依次打印每個元素 Status StackTraverse(SqStack *S) {ElemType *p;if (S->top == S->base) {printf("Stack is NULL.\n");return 0;}p = S->top;// 由棧頂依次向下遍歷while (p > S->base) {p--;printf("%d ", *p);}printf("\n");return 1; }int main() {SqStack q, *S;S = &q;int i, n, e;printf("Creat a NULL Stack :\n");InitStack(S);printf("input the length of the Stack :\n");scanf("%d", &n);for (i = 1; i <= n; i++) {scanf("%d", &e);PushStack(S, e);}printf("Is the stack NULL?\n");if (EmptyStack(S)) {printf("Yes!\n");} else {printf("No!\n");}printf("The length of stack is %d.\n", LengthStack(S));printf("The stack is :\n");StackTraverse(S);GetTopStack(S, &e);printf("The top data is %d.\n", e);printf("input the data to the stack :\n");scanf("%d", &e);PushStack(S, e);printf("The new stack is :\n");StackTraverse(S);printf("Delete the top data : ");e = PopStack(S, &e);printf("%d\n", e);printf("The new stack is :\n");StackTraverse(S);printf("Destroy the stack :\n");DestroyStack(S);StackTraverse(S);return 0; }總結(jié)
以上是生活随笔為你收集整理的顺序栈基本操作的C语言实现(含全部代码实现)--- 数据结构之顺序栈的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java IO流读取/写入/修改某个字符
- 下一篇: 【基础篇】Navicat让MySQL数据