Open bosthhe1 opened 2 years ago
函数定义
void SListInit(SSlist StackL) { StackL->Head = NULL; StackL->top = 0; StackL->capacity = 0; } void SListPush(SSlist StackL, int x) { assert(StackL); if (StackL->capacity == StackL->top) { int newcapacity = StackL->capacity == 0 ? 4 : StackL->capacity 2; int Stack = NULL; Stack = (int )realloc(StackL->Head, sizeof(StackL->Head)newcapacity); if (Stack == NULL) { printf("Stack fail\n"); exit(-1); } StackL->capacity = newcapacity; StackL->Head = Stack; } StackL->Head[StackL->top] = x; StackL->top++; } void SListPop(SSlist * StackL) { assert(StackL->Head); StackL->top--; }
void SListDestory(SSlist StackL) { assert(StackL->Head); free(StackL->Head); StackL->Head = NULL; } int StackTop(SSlist StackL) { int Top = StackL->Head[StackL->top-1]; return Top; } bool SListEmpty(SSlist * StackL) { StackL->top--; return StackL->top == 0; }
函数接口
typedef struct SSlist { int Head; int top; int capacity; }SSlist; void SListInit(SSlist StackL); void SListPop(SSlist StackL); void SListPush(SSlist StackL, int x); void SListDestory(SSlist StackL); int StackTop(SSlist StackL); bool SListEmpty(SSlist * StackL);
主函数
define _CRT_SECURE_NO_WARNINGS 1
include"head.h"
void Test() { SSlist Slist; SListInit(&Slist); SListPush(&Slist, 1); SListPush(&Slist, 3); SListPush(&Slist, 2); SListPush(&Slist, 4); printf("%d ", StackTop(&Slist)); SListPop(&Slist);//出栈4 printf("%d ", StackTop(&Slist)); SListPop(&Slist);//出栈2 SListPush(&Slist, 3); SListPush(&Slist, 1); while (!SListEmpty(&Slist)) { printf("%d ", StackTop(&Slist)); } } int main() { Test(); return 0; }