03_线性表应用一:栈

一、什么是栈

一种“先进后出”的数据结构;栈类似一个箱子, 先放进去后拿出来

栈的分类:

1.静态栈 : 以数组为内核的

2.动态栈 : 以链表为内核

栈的算法:

出栈、压栈

二、栈的顺序存储

三、栈的链式存储

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<malloc.h>
  4 //结点数据类型 
  5 typedef struct Node
  6 {
  7     int data;
  8     struct Node* pNext;
  9 }NODE,* PNODE;
 10 //栈数据类型   栈是一个砍掉一部分功能的链表 
 11 typedef struct Stack
 12 {
 13     PNODE pTop;
 14     PNODE pBottom;
 15 }STACK,* PSTACK;
 16 
 17 void init(PSTACK);
 18 void push(PSTACK pS,int val);
 19 void traverse(PSTACK);
 20 void pop(PSTACK);
 21 bool empty(PSTACK pS);
 22 
 23 int main(void)
 24 {
 25     STACK stack;
 26     init(&stack);
 27     push(&stack,1);
 28     push(&stack,2);
 29     push(&stack,3);
 30     push(&stack,4);
 31     push(&stack,5);
 32     push(&stack,6);
 33     traverse(&stack);
 34     pop(&stack);
 35 }
 36 
 37 void init(PSTACK pS)
 38 {
 39     pS->pTop = (PNODE)malloc(sizeof(NODE));
 40     if(NULL == pS->pTop)
 41     {
 42         printf("动态内存分配失败!");
 43         exit(-1); 
 44     }
 45     pS->pBottom = pS->pTop;
 46     pS->pTop->pNext = NULL;
 47 }
 48 
 49 void push(PSTACK pS,int val)
 50 {
 51     PNODE pTmp = (PNODE)malloc(sizeof(NODE));
 52     pTmp->data = val;
 53     
 54     pTmp->pNext  = pS->pTop;//把top给新结点 
 55     pS->pTop = pTmp;//新结点给top指向 
 56     return;
 57 }
 58 
 59 void traverse(PSTACK pS)
 60 {
 61     PNODE pTmp = pS->pTop; // 定义一个临时结点,永远指向顶点 
 62     while(pTmp != pS->pBottom) // 终止条件: pTmp != pBottom 
 63     {
 64         printf("pTmp->data : %d.\n",pTmp->data);
 65         pTmp = pTmp->pNext; // 往下走 
 66     }
 67 }
 68 bool empty(PSTACK pS)
 69 {
 70     if(pS->pTop == pS->pBottom || pS->pTop->pNext == NULL)
 71     {
 72         return true;
 73     }
 74     return false;
 75 }
 76 void pop(PSTACK pS)
 77 {
 78     if(empty(pS))
 79     {
 80         printf("空栈"); 
 81         exit(-1);
 82     }
 83     while(pS->pTop != pS->pBottom)
 84     {
 85         PNODE pTmp = pS->pTop;
 86         printf("free pTmp->data: %d .\n" ,pTmp->data);
 87         pS->pTop = pTmp->pNext;
 88         free(pTmp);
 89         pTmp = NULL;
 90     }
 91     return;
 92 }
 93 
 94 //清空
 95 void clear(PSTACK pS)
 96 {
 97     if(empty(pS))
 98     {
 99         printf("空栈"); 
100         exit(-1);
101     }
102     PNODE p = pS->pTop;
103     PNODE q;
104     while(p != pS->pBottom)
105     {
106         q = p->pNext;
107         free(p);
108         p = q;
109     }
110     pS->pTop = pS->pBottom;
111 }

 

四、栈的应用一:圆括号匹配

 

五、栈的应用二:字符串回文判断

六、栈和递归

6.1  阶乘

6.2  Fib数列

七、哈诺塔实现

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 void hanoi(int i,char a,char b,char c); 
 5 int main(void)
 6 {
 7     char a = 'A';
 8     char b = 'B';
 9     char c = 'C'; 
10     int i=0;
11     printf("请输入盘子的个数:");
12     scanf("%d",&i);
13     hanoi(i,a,b,c); 
14     return 0;
15 }
16 
17 void hanoi(int i,char A,char B,char C)
18 {
19     if(i == 1)//如果是1个盘子
20     {
21         printf("将编号为%d的盘子直接从%c,移到%c\n",i,A,C);//    直接将A上的盘子移到C
22     }
23     else//否则
24     {
25         
26         hanoi(i - 1,A,C,B);//将A上的n-1个盘子借助C移到B
27         printf("将编号为%d的盘子直接从%c,移到%c\n",i,A,C);//将A上的盘子移到C
28         hanoi(i - 1,B,A,C);//最后将B上的n-1个盘子借助A移到C
29     }
30      
31 }

 

posted @ 2018-01-21 01:35  短毛兔  阅读(288)  评论(0编辑  收藏  举报