概念
数据结构是计算机存储、组织数据的方式。数据结构是指相互之间存在一种或多种特定关系的数据元素的集合。通常情况下,精心选择的数据结构可以带来更高的运行或者存储效率。数据结构往往同高效的检索算法和索引技术有关。
栈作为一种数据结构,是一种只能在一端进行插入和删除操作的特殊线性表。它按照先进后出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后一个数据被第一个读出来)。栈具有记忆作用,对栈的插入与删除操作中,不需要改变栈底指针。
注意
1、栈可以理解成在一个通道里,将通道的一端堵住,这样通道里面后进来的元素也就先出去,而先进来的元素必须等到其它元素出去后,才能出去。
2、栈是允许在同一端进行插入和删除操作的特殊线性表。允许进行插入和删除操作的一端称为栈顶(top),另一端为栈底(bottom)。
3、栈底固定,而栈顶浮动。
4、栈中元素个数为零时称为空栈。插入一般称为进栈(PUSH),删除则称为退栈(POP)。
4、栈中元素个数为零时称为空栈。插入一般称为进栈(PUSH),删除则称为退栈(POP)。
5、栈中的数据元素遵守“先进后出"(First In Last Out)的原则,简称FILO结构。(后进先出的叫法,也是可以的)。
6、栈可以使用数组和链表两种方式实现,数组的实现方式必须先确定栈的空间大小,相对而言,没有那么灵活,所以建议采用链表的方式实现。
基于数组的栈(顺序栈)——以数组为底层数据结构时,通常以数组头为栈底,数组头到数组尾为栈顶的生长方向
基于单链表的栈(链栈)——以链表为底层的数据结构时,以链表头为栈顶,便于节点的插入与删除,压栈产生的新节点将一直出现在链表的头部
核心
理解栈主要是栈的生长方向,栈顶、栈底、入栈和出栈。
![]()
栈顶:指向允许元素插入和删除的一端。
栈底:与栈顶相对的另一端。
入栈:在栈中插入元素的操作。
出栈:在栈中删除元素的操作。
示例
★包含头文件stack_array.h和源文件stack_array.c(均已验证通过)。
stack_array.h
/** * @Filename : stack_array.h * @Revision : $Revision: 1.0 $ * @Author : Feng(微信公众号:不只会拍照的程序猿) * @Description : 顺序栈示例 **/ #ifndef __STACK_ARRAY_H__ #define __STACK_ARRAY_H__ #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 10 /* 栈最大元素个数 */ /* 栈空间定义 */ typedef struct stack{ int buff[MAX_SIZE]; /* 元素 */ int size; /* 元素个数 */ }t_stack; /** * @初始化栈 * @p_stack:栈 **/ void stack_init(t_stack *p_stack); /** * @清除栈 * @p_stack:栈 **/ void stack_deinit(t_stack *p_stack); /** * @判断栈是否已满 * @p_stack:栈 * @返回1表示已满,0表示未满 **/ int stack_is_full(const t_stack *p_stack); /** * @判断栈是否已空 * @p_stack:栈 * @返回1表示已空,0表示未空 **/ int stack_is_empty(const t_stack *p_stack); /** * @获取栈元素个数 * @p_stack:栈 * @返回元素个数 **/ int stack_get_size(const t_stack *p_stack); /** * @入栈操作 * @p_stack:栈 data:待插入元素 * @返回1表示成功,0表示失败 **/ int stack_push(t_stack *p_stack, int data); /** * @出栈操作 * @p_stack:栈 p_data:保存出栈元素 * @返回1表示成功,0表示失败 **/ int stack_pop(t_stack *p_stack, int *p_data); /** * @获取栈顶元素 * @p_stack:栈 p_data:保存栈顶元素 * @返回1表示成功,0表示失败 **/ int stack_get_top(const t_stack *p_stack, int *p_data); /** * @打印栈元素内容 * @p_stack:栈 **/ void stack_print(const t_stack *p_stack); #endif
stack_array.c
/** * @Filename : stack_array.c * @Revision : $Revision: 1.0 $ * @Author : Feng(微信公众号:不只会拍照的程序猿) * @Description : 顺序栈示例 **/ #include "stack_array.h" /** * @初始化栈 * @p_stack:栈 **/ void stack_init(t_stack *p_stack) { p_stack->size = 0; } /** * @清除栈 * @p_stack:栈 **/ void stack_deinit(t_stack *p_stack) { p_stack->size = 0; } /** * @判断栈是否已满 * @p_stack:栈 * @返回1表示已满,0表示未满 **/ int stack_is_full(const t_stack *p_stack) { return (p_stack->size >= MAX_SIZE); } /** * @判断栈是否已空 * @p_stack:栈 * @返回1表示已空,0表示未空 **/ int stack_is_empty(const t_stack *p_stack) { return (p_stack->size == 0); } /** * @获取栈元素个数 * @p_stack:栈 * @返回元素个数 **/ int stack_get_size(const t_stack *p_stack) { return (p_stack->size); } /** * @入栈操作 * @p_stack:栈 data:待插入元素 * @返回1表示成功,0表示失败 **/ int stack_push(t_stack *p_stack, int data) { if (p_stack->size >= MAX_SIZE) return 0; p_stack->buff[p_stack->size++] = data; return 1; } /** * @出栈操作 * @p_stack:栈 p_data:保存出栈元素 * @返回1表示成功,0表示失败 **/ int stack_pop(t_stack *p_stack, int *p_data) { if (p_stack->size == 0) return 0; *p_data = p_stack->buff[p_stack->size - 1]; p_stack->size--; return 1; } /** * @获取栈顶元素 * @p_stack:栈 p_data:保存栈顶元素 * @返回1表示成功,0表示失败 **/ int stack_get_top(const t_stack *p_stack, int *p_data) { if (p_stack->size == 0) return 0; *p_data = p_stack->buff[p_stack->size - 1]; return 1; } /** * @打印栈元素内容 * @p_stack:栈 **/ void stack_print(const t_stack *p_stack) { int i; for (i=0; i<p_stack->size; i++) printf("%d ", p_stack->buff[i]); printf("\n"); } /** * @顺序栈测试代码 **/ int main(void) { int val; t_stack my_stack; stack_init(&my_stack); /* 初始化栈,此时为空栈 */ printf("the stack is %s\n", stack_is_empty(&my_stack) ? "empty" : "not empty"); printf("---------------------------------------\n"); /* 压栈,栈:25 41 16 7 8 19 22,元素个数为7 */ stack_push(&my_stack, 25); stack_push(&my_stack, 41); stack_push(&my_stack, 16); stack_push(&my_stack, 7); stack_push(&my_stack, 8); stack_push(&my_stack, 19); stack_push(&my_stack, 22); printf("the size of stack is: %d\n", stack_get_size(&my_stack)); stack_print(&my_stack); printf("---------------------------------------\n"); /* 压栈35 38 11 22(栈已满,入栈失败),栈:25 41 16 7 8 19 22 35 38 11,个数10 */ stack_push(&my_stack, 35); stack_push(&my_stack, 38); stack_push(&my_stack, 11); stack_push(&my_stack, 22); printf("the size of stack is: %d\n", stack_get_size(&my_stack)); stack_print(&my_stack); printf("the stack is %s\n", stack_is_full(&my_stack) ? "full" : "not full"); printf("---------------------------------------\n"); /* 栈顶元素为11,第一次出栈11,第二次出栈38,栈:25 41 16 7 8 19 22 35,个数8 */ if (stack_get_top(&my_stack, &val)) printf("the top stack data is: %d\n", val); if (stack_pop(&my_stack, &val)) printf("the pop stack data is: %d\n", val); if (stack_pop(&my_stack, &val)) printf("the pop stack data is: %d\n", val); printf("the size of stack is: %d\n", stack_get_size(&my_stack)); stack_print(&my_stack); printf("---------------------------------------\n"); /* 清除栈,个数0 */ stack_deinit(&my_stack); printf("the size of stack is: %d\n", stack_get_size(&my_stack)); return 0; }
结论
feng:stack$ gcc -o stack_array stack_array.c feng:stack$ ./stack_array the stack is empty --------------------------------------- the size of stack is: 7 25 41 16 7 8 19 22 --------------------------------------- the size of stack is: 10 25 41 16 7 8 19 22 35 38 11 the stack is full --------------------------------------- the top stack data is: 11 the pop stack data is: 11 the pop stack data is: 38 the size of stack is: 8 25 41 16 7 8 19 22 35 --------------------------------------- the size of stack is: 0 feng:stack$
本示例仅为顺序栈示例,公众号也提供链栈示例《也没想象中那么神秘的数据结构-后来居上的“栈”(链栈)》,以及栈的典型应用示例。
关注
更多精彩内容,请关注微信公众号:不只会拍照的程序猿,本人致力分享linux、设计模式、C语言、嵌入式、编程相关知识,也会抽空分享些摄影相关内容,同样也分享大量摄影、编程相关视频和源码,另外你若想要本文章源码请关注公众号:不只会拍照的程序猿,后台回复:数据结构源码,也可点击此处下载。