线性表(栈)

栈是一种把表的一端执行删除和添加操作的线性表,通常把操作的一端成为栈顶,另一端为栈底。

根据定义可知它的结构让数据保持先进后出的特点。

栈的基本运算有六种:

  1.置空栈:构造一个空栈。

  2.判断空栈:若栈为空,则返回true, 否则 false.

  3.判断栈满:若栈满,则返回true,否则false.

  4.进栈:将元素插入栈顶。

  5.出栈:若栈非空,则将栈顶元素删除并返回栈顶元素。

  6.取栈顶元素:若栈非空,返回栈顶元素不改变栈的状态。

栈的存储结构有两种:顺序栈和链栈

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 #define StackSize 100
 5 typedef char DataType;
 6 /**
 7 **顺序栈结构 
 8 **/
 9 typedef struct {
10     DataType data[StackSize];
11     int top; //栈顶指针 
12 } SeqStack;
13 
14 /**
15 **置空栈 
16 **/
17 void InitStack(SeqStack *S) 
18 {
19     //置空顺序栈并数组下标0为起始值,所以空栈指针为-1 
20     S->top = -1;
21 } 
22 /**
23 **判断栈空 
24 **/
25 int StackEmpty(SeqStack *S) 
26 {
27     return S->top == -1; 
28 }
29 /**
30 ** 判断栈满 
31 **/ 
32 int StackFull(SeqStack *S) 
33 {
34     return S->top + 1 == StackSize;
35 }
36 /**
37 ** 入栈 
38 **/
39 void Push(SeqStack *S, DataType c) 
40 {
41     if (StackFull(S)) {
42         printf("stack overflow
");
43     } else {
44         S->data[++S->top] = c;
45     }
46 }
47 /**
48 ** 出栈
49 **/
50 DataType Pop(SeqStack *S) 
51 {
52     if(StackEmpty(S)) {
53         printf("Stack underflow
");
54         exit(1);
55     } else {
56         return S->data[S->top--];
57     }
58 }
59 /**
60 ** 取栈顶元素 
61 **/
62 DataType GetTop(SeqStack *S) 
63 {
64     if (StackEmpty(S)) {
65         printf("Stack underflow
");
66         exit(1);
67     } else {
68         return S->data[S->top];
69     }
70 }
71 
72 
73 int main(int argc, char *argv[]) {
74     SeqStack data;
75     SeqStack *S = &data;
76     InitStack(S);
77     Push(S, 'b');
78     Push(S, 'a');
79     printf("%c
", Pop(S));
80     printf("%c
", GetTop(S));
81     printf("%c
", GetTop(S));
82     printf("%c
", Pop(S));
83 }
View Code
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 typedef char DataType;
 5 /**
 6 ** 链栈结构 
 7 **/
 8 typedef struct stacknode{
 9     DataType data;
10     struct stacknode *next; 
11 } StackNode;
12 
13 typedef StackNode * LinkStack;
14 
15 
16 /**
17 **判断栈空 
18 **/
19 int StackEmpty(LinkStack top) 
20 {
21     return top == NULL;
22 }
23 
24 /**
25 ** 入栈 
26 **/
27 LinkStack Push(LinkStack top, DataType c) 
28 {
29     StackNode *p;
30     p = (StackNode *)malloc(sizeof(StackNode));
31     if (p == NULL) {
32         printf("Out of memory
");
33         exit(1);
34     }
35     p->data = c;
36     p->next = top;
37     top = p;
38     return top;
39 }
40 /**
41 ** 出栈
42 **/
43 LinkStack Pop(LinkStack top, DataType *x) 
44 {
45     StackNode *p = top;
46     if(StackEmpty(p)) {
47         printf("stack empty
");
48         exit(1);
49     } else {
50         *x = p->data;
51         top = p->next;
52         free(p);
53         return top;
54     }
55 }
56 /**
57 ** 取栈顶元素 
58 **/
59 DataType GetTop(LinkStack top) 
60 {
61     if(StackEmpty(top)) {
62         printf("stack empty
");
63         exit(1);
64     } else {
65         return top->data;
66     }
67 }
68 
69 
70 int main(int argc, char *argv[]) {
71     LinkStack top;
72     char x;
73     top = Push(top, 'b');
74     top = Push(top, 'a');
75     top = Pop(top, &x);
76     printf("%c
", x);
77     printf("%c
", GetTop(top));
78 
79 }
View Code
原文地址:https://www.cnblogs.com/Python-233/p/15002696.html