bosthhe1 / -

数据结构与算法(初阶)
0 stars 0 forks source link

#7

Open bosthhe1 opened 2 years ago

bosthhe1 commented 2 years ago

主函数

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; }

bosthhe1 commented 2 years ago

函数定义

define _CRT_SECURE_NO_WARNINGS 1

include"head.h"

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; }

bosthhe1 commented 2 years ago

函数接口

define _CRT_SECURE_NO_WARNINGS 1

pragma once

include

include

include

include

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);