目录
如上图,加入我们学习计算机课程,有些课程需要建立在先行课程基础上才能学习,那么我们该如何选择呢?
上图中,我们发现,如果我们像学习一门课程,我们就需要把它放到栈底,然后找到它的前驱课程,然后一直类推找前驱课程,直到一个没有前驱课程的课程,就是栈顶,当我们学习完一门课程就是出栈,利用栈这个模型就解决了我们选课的问题。
选一个方向,到头后原路返回。再去访问另一个分支。
如果用递归也可以解决,但是比较容易出错
把一个非线性的问题,线性化。把前驱课程放在底入栈,没有之后再把其他课程放进去,一层一层转换为线性问题。
它是顺序表的一种,具有顺序表同样的存储结构,由数组定义,配合用数组下标表示的栈顶指针top(相对指针)完成各种操作。
定义sqstack.h文件
- typedef int data_t ; /*定义栈中数据元素的数据类型*/
- typedef struct {
- data_t *data ; /*用指针指向栈的存储空间*/ 如果用data[]数组,必须提前设定好数组长度,用指针可以让用户自定义
- int maxlen; /*当前栈的最大元素个数*/
- int top ; /*指示栈顶位置(数组下标)的变量*/
- } sqstack; /*顺序栈类型定义*/
-
- sqstack * stack_create(int len);
- int stack_push(sqstack * s, data_t value);
- int stack_empty(sqstack *s);
- int stack_full(sqstack *s);
- data_t stack_pop(sqstack *s);
- data_t stack_top(sqstack *s);
- int stack_clear(sqstack *s);
- int stack_free(sqstack *s);
接下来实现sqstack.c
- sqstack * stack_create(int len) {
- sqstack * s;
-
- if ((s =(sqstack *)malloc(sizeof(sqstack))) == NULL) {
- printf("malloc sqstack failed\n");
- return NULL;
- }
-
- if ((s->data = (data_t *)malloc(len * sizeof(data_t)))==NULL) {
- printf("malloc data failed\n");
- free(s);
- return NULL;
- }
-
- memset(s->data, 0, len*sizeof(data_t));
- s->maxlen = len;
- s->top = -1;
-
- return s;
- }
malloc,第一次是结构体,第二次是结构体里数据,用户传进来的值。
- int stack_clear(sqstack *s) {
- if (s == NULL) {
- printf("s is NULL\n");
- return -1;
- }
-
- s->top = -1;
- return 0;
- }
- /*
- *@ret 1-empty
- * */
- int stack_empty(sqstack *s) {
- if (s == NULL) {
- printf("s is NULL\n");
- return -1;
- }
- return (s->top == -1 ? 1 : 0);
- }
- /*
- * @ret 1-full
- * */
- int stack_full(sqstack *s) {
- if (s == NULL) {
- printf("s is NULL\n");
- return -1;
- }
- return (s->top == s->maxlen-1 ? 1 : 0);
- }
- int stack_push(sqstack * s, data_t value) {
- if (s == NULL) {
- printf("s is NULL\n");
- return -1;
- }
-
- if (s->top == s->maxlen-1) {
- printf("stack is full\n");
- return -1;
- }
-
- s->top++;
- s->data[s->top] = value;
-
- return 0;
- }
- data_t stack_pop(sqstack *s) {
- s->top--;
- return (s->data[s->top+1]);
- }
- data_t stack_top(sqstack *s) {
- return (s->data[s->top]);
- }
需要先释放后申请的空间,再去释放前面申请的空间
- int stack_free(sqstack *s) {
- if (s == NULL) {
- printf("s is NULL\n");
- return -1;
- }
-
- if (s->data != NULL)
- free(s->data);
- free(s);
-
- return 0;
- }
- #include
- #include "sqstack.h"
-
- int main(int argc, const char *argv[])
- {
- sqstack *s;
-
- s = stack_create(100);
- if (s == NULL)
- return -1;
-
- stack_push(s, 10); //入栈
- stack_push(s, 20);
- stack_push(s, 30);
- stack_push(s, 40);
-
- while (!stack_empty(s)) {
- printf("pop: %d \n", stack_pop(s) );
- }
-
- stack_free(s);
-
- return 0;
- }
插入操作和删除操作均在链表头部进行,链表尾部就是栈底,栈顶指针就是头指针。
- typedef int data_t;
-
- typedef struct node {
- data_t data;
- struct node *next;
- }listnode, *linkstack;
-
- linkstack stack_create();
- int stack_push(linkstack s, data_t value);
- data_t stack_pop(linkstack s);
- int stack_empty(linkstack s);
- data_t stack_top(linkstack s);
- linkstack stack_free(linkstack s);
接下来实现linkstack.c
- sqstack * stack_create(int len) {
- sqstack * s;
-
- if ((s =(sqstack *)malloc(sizeof(sqstack))) == NULL) {
- printf("malloc sqstack failed\n");
- return NULL;
- }
-
- if ((s->data = (data_t *)malloc(len * sizeof(data_t)))==NULL) {
- printf("malloc data failed\n");
- free(s);
- return NULL;
- }
-
- memset(s->data, 0, len*sizeof(data_t));
- s->maxlen = len;
- s->top = -1;
-
- return s;
- }
- int stack_push(sqstack * s, data_t value) {
- if (s == NULL) {
- printf("s is NULL\n");
- return -1;
- }
-
- if (s->top == s->maxlen-1) {
- printf("stack is full\n");
- return -1;
- }
-
- s->top++;
- s->data[s->top] = value;
-
- return 0;
- }
- data_t stack_pop(sqstack *s) {
- s->top--;
- return (s->data[s->top+1]);
- }
- /*
- *@ret 1-empty
- * */
- int stack_empty(sqstack *s) {
- if (s == NULL) {
- printf("s is NULL\n");
- return -1;
- }
- return (s->top == -1 ? 1 : 0);
- }
- data_t stack_top(sqstack *s) {
- return (s->data[s->top]);
- }
- int stack_clear(sqstack *s) {
- if (s == NULL) {
- printf("s is NULL\n");
- return -1;
- }
-
- s->top = -1;
- return 0;
- }
- int stack_full(sqstack *s) {
- if (s == NULL) {
- printf("s is NULL\n");
- return -1;
- }
- return (s->top == s->maxlen-1 ? 1 : 0);
- }
- int stack_free(sqstack *s) {
- if (s == NULL) {
- printf("s is NULL\n");
- return -1;
- }
-
- if (s->data != NULL)
- free(s->data);
- free(s);
-
- return 0;
- }