标签:遍历 存储空间 int capacity 判断 define 一个 ++ 指定
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("创建空栈失败\n"); 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\n", *mystack.top); 42 push(mystack, 2); 43 printf("将2入栈后栈顶元素是:%d\n", *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("初始化栈失败\n"); 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("栈中元素为:\n"); 38 while (S.top != S.bottom ){ 39 S.top--; 40 printf("%d ", *S.top); 41 } 42 printf("\n"); 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("初始化栈失败\n"); 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("栈中元素为:\n"); 43 while (S.top != S.bottom ){ 44 S.top--; 45 printf("%d ", *S.top); 46 } 47 printf("\n"); 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("初始化栈失败\n"); 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("栈中元素为:\n"); 60 while (S.top != S.bottom ){ 61 S.top--; 62 printf("%d ", *S.top); 63 } 64 printf("\n"); 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\n", isempty); 77 78 int isfull=isFull(Push(mystack)); 79 printf("栈是满的吗?0表示满,1表示没有满 :%d\n", 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("初始化栈失败\n"); 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("栈中元素为:\n"); 55 while (S.top != S.bottom ){ 56 S.top--; 57 printf("%d ", *S.top); 58 } 59 printf("\n"); 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\n", 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("初始化栈失败\n"); 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("\n"); 58 } 59 60 61 void main() { 62 stack mystack; 63 mystack.stack_size = CAPACITY; //初始时给栈的尺寸是100 64 mystack = InitStack(mystack); //获取经过初始化后创建的空栈 65 66 printf("初始时压入10个元素后的栈是:\n"); 67 showStack(Push(mystack)); //打印出初始化时候压入栈中10个元素的栈 68 69 printf("弹出栈顶元素后的栈是:\n"); 70 showStack(Pop(mystack)); 71 72 73 74 }
7,完善栈的相关操作
①入栈时添加判断,若栈满则需要 追加存储空间
标签:遍历 存储空间 int capacity 判断 define 一个 ++ 指定
原文地址:https://www.cnblogs.com/shanlu0000/p/12548867.html