1,创建一个空栈,并向栈中压入1个元素
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 #define CAPACITY 100 //栈的容量 5 #define SIZE 10 //如果栈需要扩充,每次扩充10 6 7 //定义栈 8 typedef struct Stack { 9 int* top;//栈顶指针 10 int* bottom;//栈底指针 11 int stack_size; //栈的尺寸,栈里面有多少个元素 12 }stack; 13 14 //创建一个空栈 15 stack initStack(stack S) { 16 S.bottom = (int*)malloc(CAPACITY * sizeof(int)); 17 if (S.bottom == NULL) { 18 printf("创建空栈失败 "); 19 exit(0); 20 } 21 else { 22 S.top = S.bottom; 23 S.stack_size = CAPACITY; //初始化栈的尺寸是100 24 return S; 25 } 26 } 27 28 //入栈 29 void push(stack S,int elem) { 30 *S.top = elem; 31 S.top++; 32 S.stack_size++; 33 } 34 35 void main() { 36 stack mystack; 37 mystack.top = mystack.bottom = NULL; 38 mystack.stack_size = 0; 39 mystack = initStack(mystack); 40 push(mystack, 1); 41 printf("将1入栈后栈顶元素是:%d ", *mystack.top); 42 push(mystack, 2); 43 printf("将2入栈后栈顶元素是:%d ", *mystack.top); 44 45 }
2,遍历栈中所有元素
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define CAPACITY 100 5 #define SIZE 10 6 7 typedef struct Stack{ 8 int* top; 9 int* bottom; 10 int stack_size; 11 }stack; 12 13 14 //入栈 15 stack Push(stack S,int elem){ 16 *S.top = elem; 17 S.top++; 18 S.stack_size++; 19 return S; 20 } 21 22 //栈的初始化 23 stack InitStack(stack S){ 24 S.bottom = (int*)malloc(CAPACITY * sizeof(int)); 25 if (S.bottom == NULL) 26 { 27 printf("初始化栈失败 "); 28 exit(0); 29 } 30 S.top = S.bottom; 31 S.stack_size = 0; 32 return S; 33 } 34 35 //遍历栈中元素,从栈顶到栈底 36 void showStack(stack S){ 37 printf("栈中元素为: "); 38 while (S.top != S.bottom ){ 39 S.top--; 40 printf("%d ", *S.top); 41 } 42 printf(" "); 43 } 44 45 46 void main() { 47 stack mystack; 48 mystack.stack_size = 0; 49 mystack = InitStack(mystack); 50 mystack = Push(mystack,1); 51 showStack(mystack); 52 }
3,初始化时压入10个元素入栈
1 //遍历,入栈(初始化压入10个元素) 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 #define CAPACITY 100 6 7 typedef struct Stack{ 8 int* top; 9 int* bottom; 10 int stack_size; //栈的初始化尺寸 11 }stack; 12 13 14 15 16 //栈的初始化 17 stack InitStack(stack S){ 18 S.bottom = (int*)malloc(CAPACITY * sizeof(int)); 19 if (S.bottom == NULL) 20 { 21 printf("初始化栈失败 "); 22 exit(0); 23 } 24 S.top = S.bottom; 25 return S; 26 } 27 28 29 //入栈 30 stack Push(stack S) { 31 //初始化时压入10个元素 32 for (int i = 0; i < 10; i++) { 33 *S.top = i; 34 S.top++; 35 } 36 return S; 37 } 38 39 40 //遍历栈中元素,从栈顶到栈底 41 void showStack(stack S){ 42 printf("栈中元素为: "); 43 while (S.top != S.bottom ){ 44 S.top--; 45 printf("%d ", *S.top); 46 } 47 printf(" "); 48 } 49 50 51 void main() { 52 stack mystack; 53 mystack.stack_size = CAPACITY; //初始时给栈的尺寸是100 54 mystack = InitStack(mystack); //获取经过初始化后创建的空栈 55 56 showStack(Push(mystack)); //打印出初始化时候压入栈中10个元素的栈 57 }
4,判断栈是否空,是否满?
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define CAPACITY 100 5 #define SIZE 10 6 7 typedef struct Stack{ 8 int* top; 9 int* bottom; 10 int stack_size; 11 }stack; 12 13 14 //栈的初始化 15 stack InitStack(stack S){ 16 S.bottom = (int*)malloc(CAPACITY * sizeof(int)); 17 if (S.bottom == NULL) 18 { 19 printf("初始化栈失败 "); 20 exit(0); 21 } 22 S.top = S.bottom; 23 return S; 24 } 25 26 //入栈 27 stack Push(stack S) { 28 //初始化时压入10个元素 29 for (int i = 0; i < 10; i++) { 30 *S.top = i; 31 S.top++; 32 } 33 return S; 34 } 35 36 //判栈满 37 int isFull(stack S) { 38 if (S.top - S.bottom >= S.stack_size) { //栈满 39 return 0; //0表示满 40 } 41 else { 42 return 1; //1表示没有满 43 } 44 } 45 46 //判栈空 47 int isEmpty(stack S) { 48 if (S.top == S.bottom) { 49 return 0; //0表示空 50 } 51 else { 52 return 1; //1表示非空 53 } 54 } 55 56 57 //遍历栈中元素,从栈顶到栈底 58 void showStack(stack S){ 59 printf("栈中元素为: "); 60 while (S.top != S.bottom ){ 61 S.top--; 62 printf("%d ", *S.top); 63 } 64 printf(" "); 65 } 66 67 68 void main() { 69 stack mystack; 70 mystack.stack_size = CAPACITY; 71 mystack = InitStack(mystack); //获取经过初始化后创建的空栈 72 73 showStack(Push(mystack)); //打印出初始化时候压入栈中10个元素的栈 74 75 int isempty = isEmpty(Push(mystack)); 76 printf("栈是空的吗?0表示空,1表示非空:%d ", isempty); 77 78 int isfull=isFull(Push(mystack)); 79 printf("栈是满的吗?0表示满,1表示没有满 :%d ", isfull); 80 }
5,清空栈
1 //出栈 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 #define CAPACITY 100 7 8 typedef struct Stack{ 9 int* top; 10 int* bottom; 11 int stack_size; //栈的初始化尺寸 12 }stack; 13 14 15 16 17 //栈的初始化 18 stack InitStack(stack S){ 19 S.bottom = (int*)malloc(CAPACITY * sizeof(int)); 20 if (S.bottom == NULL) 21 { 22 printf("初始化栈失败 "); 23 exit(0); 24 } 25 S.top = S.bottom; 26 return S; 27 } 28 29 30 //入栈 31 stack Push(stack S) { 32 //初始化时压入10个元素 33 for (int i = 0; i < 10; i++) { 34 *S.top = i; 35 S.top++; 36 } 37 return S; 38 } 39 40 41 //出栈 42 stack ClearStack(stack S) { 43 S = Push(S); 44 for (int i = 0; i < 10; i++) { 45 S.top--; 46 *S.top = 8 - i; 47 } 48 return S; 49 } 50 51 52 //遍历栈中元素,从栈顶到栈底 53 void showStack(stack S){ 54 printf("栈中元素为: "); 55 while (S.top != S.bottom ){ 56 S.top--; 57 printf("%d ", *S.top); 58 } 59 printf(" "); 60 } 61 62 63 //判栈空 64 int isEmpty(stack S) { 65 if (S.top == S.bottom) { 66 return 0; //0表示空 67 } 68 else { 69 return 1; //1表示非空 70 } 71 } 72 73 74 void main() { 75 stack mystack; 76 mystack.stack_size = CAPACITY; //初始时给栈的尺寸是100 77 mystack = InitStack(mystack); //获取经过初始化后创建的空栈 78 79 showStack(Push(mystack)); //打印出初始化时候压入栈中10个元素的栈 80 81 int isempty = isEmpty(ClearStack(mystack)); 82 printf("栈是空的吗?0表示空,1表示非空:%d ", isempty); 83 84 }
6,弹出栈顶元素(出栈)
1 //让指定元素出栈 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 #define CAPACITY 100 7 8 typedef struct Stack{ 9 int* top; 10 int* bottom; 11 int stack_size; //栈的初始化尺寸 12 }stack; 13 14 15 16 17 //栈的初始化 18 stack InitStack(stack S){ 19 S.bottom = (int*)malloc(CAPACITY * sizeof(int)); 20 if (S.bottom == NULL) 21 { 22 printf("初始化栈失败 "); 23 exit(0); 24 } 25 S.top = S.bottom; 26 return S; 27 } 28 29 30 //入栈 31 stack Push(stack S) { 32 //初始化时压入10个元素 33 for (int i = 0; i < 10; i++) { 34 *S.top = i; 35 S.top++; 36 } 37 return S; 38 } 39 40 41 //让指定元素出栈 42 stack Pop(stack S) { 43 S = Push(S); 44 S.top--; 45 return S; 46 } 47 48 49 50 51 //遍历栈中元素,从栈顶到栈底 52 void showStack(stack S){ 53 while (S.top != S.bottom ){ 54 S.top--; 55 printf("%d ", *S.top); 56 } 57 printf(" "); 58 } 59 60 61 void main() { 62 stack mystack; 63 mystack.stack_size = CAPACITY; //初始时给栈的尺寸是100 64 mystack = InitStack(mystack); //获取经过初始化后创建的空栈 65 66 printf("初始时压入10个元素后的栈是: "); 67 showStack(Push(mystack)); //打印出初始化时候压入栈中10个元素的栈 68 69 printf("弹出栈顶元素后的栈是: "); 70 showStack(Pop(mystack)); 71 72 73 74 }
7,完善栈的相关操作
①入栈时添加判断,若栈满则需要 追加存储空间
这里 设置 CAPACITY 为10,即栈满的情况
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define CAPACITY 10 5 #define SIZE 10 6 7 typedef struct Stack { 8 int* top; 9 int* bottom; 10 int stack_size; //栈的初始化尺寸 11 }stack; 12 13 14 15 16 //栈的初始化 17 stack InitStack(stack S) { 18 S.bottom = (int*)malloc(CAPACITY * sizeof(int)); 19 if (S.bottom == NULL) 20 { 21 printf("初始化栈失败 "); 22 exit(0); 23 } 24 S.top = S.bottom; 25 return S; 26 } 27 28 29 //判栈满 30 int isFull(stack S) { 31 if (S.top - S.bottom >= S.stack_size) { //栈满 32 return 0; //0表示满 33 } 34 else { 35 return 1; //1表示没有满 36 } 37 } 38 39 //遍历栈中元素,从栈顶到栈底 40 void showStack(stack S) { 41 while (S.top != S.bottom) { 42 S.top--; 43 printf("%d ", *S.top); 44 } 45 printf(" "); 46 } 47 48 //入栈 49 stack Push(stack S) { 50 //初始化时压入10个元素 51 for (int i = 0; i < 10; i++) { 52 *S.top = i; 53 S.top++; 54 } 55 printf("初始化压入10个元素后的栈是: "); 56 showStack(S); 57 58 if (isFull(S) == 0){ 59 printf("栈满了,准备追加存储空间 "); 60 S.bottom = (int*)realloc(S.bottom,(S.stack_size+SIZE)*sizeof(int)); //SIZE是定义的每次追加的空间 61 if (S.bottom == NULL) { 62 printf("追加存储空间失败 "); 63 exit(0); 64 } 65 else { 66 printf("追加存储空间成功 "); 67 S.top = S.bottom + S.stack_size; //设置栈顶 68 S.stack_size += SIZE; 69 } 70 } 71 return S; 72 } 73 74 75 void main() { 76 stack mystack; 77 mystack.stack_size = CAPACITY; //初始时给栈的尺寸是100 78 mystack = InitStack(mystack); //获取经过初始化后创建的空栈 79 80 //showStack(Push(mystack)); //打印出初始化时候压入栈中10个元素的栈 81 82 int isfull = isFull(Push(mystack)); 83 printf("栈是满的吗?0表示满,1表示没有满 :%d ", isfull); 84 }
②出栈时,如果栈已经空了,则提示 “栈已空”
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define CAPACITY 100 5 #define SIZE 10 6 7 typedef struct Stack { 8 int* top; 9 int* bottom; 10 int stack_size; //栈的初始化尺寸 11 }stack; 12 13 14 15 16 //栈的初始化 17 stack InitStack(stack S) { 18 S.bottom = (int*)malloc(CAPACITY * sizeof(int)); 19 if (S.bottom == NULL) 20 { 21 printf("初始化栈失败 "); 22 exit(0); 23 } 24 S.top = S.bottom; 25 return S; 26 } 27 28 29 //判栈满 30 int isFull(stack S) { 31 if (S.top - S.bottom >= S.stack_size) { //栈满 32 return 0; //0表示满 33 } 34 else { 35 return 1; //1表示没有满 36 } 37 } 38 39 //判栈空 40 int isEmpty(stack S) { 41 if (S.top == S.bottom) { 42 return 0; //0表示空 43 } 44 else { 45 return 1; //1表示非空 46 } 47 } 48 49 50 //遍历栈中元素,从栈顶到栈底 51 void showStack(stack S) { 52 while (S.top != S.bottom) { 53 S.top--; 54 printf("%d ", *S.top); 55 } 56 printf(" "); 57 } 58 59 //入栈 60 stack Push(stack S) { 61 //初始化时压入10个元素 62 for (int i = 0; i < 10; i++) { 63 *S.top = i; 64 S.top++; 65 } 66 printf("初始化压入10个元素后的栈是: "); 67 showStack(S); 68 69 if (isFull(S) == 0){ 70 printf("栈满了,准备追加存储空间 "); 71 S.bottom = (int*)realloc(S.bottom,(S.stack_size+SIZE)*sizeof(int)); //SIZE是定义的每次追加的空间 72 if (S.bottom == NULL) { 73 printf("追加存储空间失败 "); 74 exit(0); 75 } 76 else { 77 printf("追加存储空间成功 "); 78 S.top = S.bottom + S.stack_size; //设置栈顶 79 S.stack_size += SIZE; 80 } 81 } 82 return S; 83 } 84 85 //出栈 86 stack Pop(stack S) { 87 S = Push(S); 88 for (int i = 0; i < 10; i++) { 89 S.top--; 90 printf("弹出栈顶元素后的栈是: "); 91 showStack(S); 92 if (isEmpty(S) == 0) { 93 printf("栈空了 "); 94 exit(0); 95 } 96 } 97 return S; 98 99 } 100 101 102 void main() { 103 stack mystack; 104 mystack.stack_size = CAPACITY; //初始时给栈的尺寸是100 105 mystack = InitStack(mystack); //获取经过初始化后创建的空栈 106 107 //打印出pop后的栈 108 showStack(Pop(mystack)); 109 110 }