关于栈:栈和队列
栈和队列一、对于模仿栈应用何种模型 1.程序表:尾插尾删很快,缓存利用率高,然而要扩容 2.单链表:应用链表头作为栈顶来插入删除数据也很快 3.带头双向循环链表:也能够,工夫也是O(1) 二、栈的模仿实现 //"stack.h"typedef int type;typedef struct stack{ type* a; int top; int capacity;}st;//stack.c#include"stack.h"void st_init(st* sta){ assert(sta); sta->a = NULL; sta->top = 0;//数组下标的意思,是数据下一个下标,外面没有值;top = -1,则是top指向最初一个元素 sta->capacity = 0;}void st_push(st* sta, type x){ assert(sta); if (sta->top == sta->capacity) { //满了 int newcapa = sta->capacity == 0 ? 4 : 2 * sta->capacity; type* t = realloc(sta->a, sizeof(type) * newcapa);//扩容;如果a== NULL,它的行为就和malloc一样 if (t == NULL) { printf("realloc fail\n"); exit(-1); } sta->a = t; sta->capacity = newcapa; } sta->a[sta->top] = x; ++sta->top;}void st_destroy(st* sta){ assert(sta); free(sta->a); sta->a = NULL; sta->top = 0; sta->capacity = 0;}void st_pop(st* sta){ assert(sta); assert(sta->top > 0); sta->top--;}type st_top(st* sta){ assert(sta); assert(sta->top > 0); return sta->a[sta->top - 1];}bool st_empty(st* sta){ assert(sta); return sta->top == 0;}int st_size(st* sta){ assert(sta); return sta->top; }三、根底oj ...