C语言实现栈
1、结构体
typedef struct my_stack { int len;//既可以表示栈顶编号也可以表示元素个数 typename a[10001];//用数组表示栈内元素 }Stack;
2、初始化
Stack* create_emptystack() { Stack *s; s = (Stack*)malloc(sizeof(Stack)); if(s == NULL) printf("malloc error\n"); else s->len = -1; return s; }
3、判断栈是否为空
1 int isempty(Stack *s) { 2 return s->len == -1; 3 }
4、入栈
void push_stack(Stack *s, typename value) { if(s->len > 10001) printf("exceed!\n"); else { s->len++; s->a[s->len] = value; } }
5、出栈
void pop_stack(Stack *s) { if(!isempty(s)) s->len--; }
6、获取栈顶元素
1 tpyename get_top(Stack *s) { 2 return s->a[s->len]; 3 }
相关推荐
chensen 2020-11-14
拉斯厄尔高福 2020-11-04
杜倩 2020-10-29
拉斯厄尔高福 2020-10-19
嵌入式资讯精选 2020-10-15
zhaochen00 2020-10-13
penkgao 2020-10-13
yiyilanmei 2020-10-05
wanshiyingg 2020-09-29
Mars的自语 2020-09-27
shenwenjie 2020-09-24
一个逗逗 2020-09-22
flycony 2020-09-13
zhaochen00 2020-08-20
Biao 2020-08-20
qingsongzdq 2020-08-19
penkgao 2020-08-17
cetrolchen 2020-08-14
GuoSir 2020-08-07