数据结构学习第十一课(顺序栈)
作者:互联网
2,顺序栈
2.1链表基本框架
typedef int Data;
#define INIT_STACK_SIZE 8
typedef struct _SeqStack
{
Data* base;
int capacity;
int top;//指向栈顶
}SeqStack;
2.2创建一个链表
//创建一个链表
SeqStack* createSeqStack()
{
SeqStack* s = calloc(1, sizeof(SeqStack));
assert(s != NULL);
s->base = calloc(INIT_STACK_SIZE, sizeof(Data));
assert(s->base != NULL);
s->capacity = INIT_STACK_SIZE;
s->top = 0;
return s;
}
2.3插入
//插入
void push(SeqStack* s, Data val)
{
if (s->top==s->capacity)
{
printf("满了");
return;
}
s->base[s->top++] = val;
}
2.4删除
//删除
void pop(SeqStack* s)
{
if (empty(s))
{
return;
}
s->top--;
}
2.5获取栈顶元素
//获取栈顶元素
Data top(SeqStack* s)
{
assert(!empty(s));
return s->base[s->top - 1];
}
2.6大小
//大小
int size(SeqStack* s)
{
return s->top;
}
2.7判空
//判空
#include<stdbool.h>
bool empty(SeqStack* s)
{
return s->top==0;
}
标签:SeqStack,return,int,top,第十一,顺序,base,数据结构,Data 来源: https://blog.csdn.net/kdnnnd/article/details/113746470