顺序栈
顺序栈的存储结构定义:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define Stack_Size 20 4 typedef char StackElementType; 5 typedef struct { 6 StackElementType elem[Stack_Size]; 7 int top; 8 }SeqStack;
初始化顺序栈:
1 /*初始化顺序栈*/ 2 SeqStack* initStack() { 3 SeqStack* s = (SeqStack*)malloc(sizeof(SeqStack)); 4 s->top = -1; 5 return s; 6 } 7 int main(){ 8 SeqStack* s = initStack(); 9 return 0; 10 }
顺序栈进栈:
进栈时,首先判断当前栈是否已满,如果栈已满,还要进栈就会发生上溢。
1 /*顺序栈进栈*/ 2 int push(SeqStack *s,StackElementType x) { 3 if (s->top==Stack_Size-1) 4 { 5 return false; /*栈已满*/ 6 } 7 /*进栈*/ 8 s->top++; 9 s->elem[s->top] = x; 10 return true; 11 }
顺序栈出栈:
出栈时,首先判断当前栈是否为空,如果栈空,还要出栈就会发生下溢。
1 /*顺序栈出栈*/ 2 int pop(SeqStack *s,StackElementType *x) { 3 /*判断栈是否为空*/ 4 if (s->top==-1) 5 { 6 return false; 7 } 8 else 9 { 10 *x = s->elem[s->top]; /*栈顶元素赋给x*/ 11 s->top--; 12 return true; 13 }
顺序栈获取栈顶元素:
1 /*获取栈顶元素*/ 2 int getTop(SeqStack *s,StackElementType *x) { 3 if (s->top==-1) 4 { 5 return false; 6 7 } 8 else 9 { 10 *x = s->elem[s->top]; 11 return true; 12 } 13 }
顺序栈置成空栈:
1 void clearStack(SeqStack *s) { 2 s->top = -1; 3 }
顺序栈操作总代码:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define Stack_Size 10 4 typedef int StackElementType; 5 typedef struct { 6 StackElementType elem[Stack_Size]; 7 int top; 8 9 }SeqStack; 10 /*初始化顺序栈*/ 11 SeqStack* initStack() { 12 SeqStack* s = (SeqStack*)malloc(sizeof(SeqStack)); 13 s->top = -1; 14 return s; 15 } 16 /*顺序栈进栈*/ 17 int push(SeqStack *s,StackElementType x) { 18 /*栈满*/ 19 if (s->top==Stack_Size-1) 20 { 21 return false; 22 } 23 /*进栈*/ 24 s->top++; 25 s->elem[s->top] = x; 26 return true; 27 } 28 /*顺序栈出栈*/ 29 int pop(SeqStack *s,StackElementType *x) { 30 /*判断栈是否为空*/ 31 if (s->top==-1) 32 { 33 return false; 34 } 35 else 36 { 37 *x = s->elem[s->top]; 38 s->top--; 39 return true; 40 } 41 } 42 /*获取栈顶元素*/ 43 int getTop(SeqStack *s,StackElementType *x) { 44 if (s->top==-1) 45 { 46 return false; 47 48 } 49 else 50 { 51 *x = s->elem[s->top]; 52 return true; 53 } 54 } 55 void clearStack(SeqStack *s) { 56 s->top = -1; 57 } 58 /*栈特点:先进后出或后进先出*/ 59 void printfStack(SeqStack *s) { 60 printf("栈内元素:"); 61 for (int i = s->top; i >=0; i--) 62 { 63 printf("%d ", s->elem[i]); 64 } 65 printf("\n"); 66 } 67 int main() { 68 StackElementType x; 69 SeqStack* s = initStack(); 70 for (int i = 3; i <=9; i+=3) 71 { 72 push(s,i); 73 printfStack(s); /*每进栈一次打印栈内元素一次*/ 74 } 75 pop(s, &x); /*出栈的元素存入x*/ 76 printf("出栈元素:%d\n", x); 77 78 printf("置空"); 79 clearStack(s); 80 printfStack(s); 81 82 return 0; 83 }
顺序栈的应用:
判断数组中缺少的小括号
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define Stack_Size 10 4 typedef char StackElementType; 5 typedef struct { 6 StackElementType elem[Stack_Size]; 7 int top; 8 9 }SeqStack; 10 /*初始化顺序栈*/ 11 SeqStack* initStack() { 12 SeqStack* s = (SeqStack*)malloc(sizeof(SeqStack)); 13 s->top = -1; 14 return s; 15 } 16 /*顺序栈进栈*/ 17 int push(SeqStack *s,StackElementType x) { 18 /*栈满*/ 19 if (s->top==Stack_Size-1) 20 { 21 return false; 22 } 23 /*进栈*/ 24 s->top++; 25 s->elem[s->top] = x; 26 return true; 27 } 28 /*顺序栈出栈*/ 29 int pop(SeqStack *s,StackElementType *x) { 30 /*判断栈是否为空*/ 31 if (s->top==-1) 32 { 33 return false; 34 } 35 else 36 { 37 *x = s->elem[s->top]; 38 s->top--; 39 return true; 40 } 41 } 42 int main() { 43 StackElementType x; 44 SeqStack* s = initStack(); 45 char str[100] = "(7*8)/(5*(9-5)*7"; 46 for (int i = 0; str[i]!='\0'; i++) 47 { 48 if (str[i]=='(') 49 { 50 push(s, str[i]); 51 } 52 if (str[i]==')') 53 { 54 if (s->top == -1) { 55 printf("missing ("); 56 return true; 57 } 58 else 59 { 60 pop(s, &x); 61 } 62 } 63 64 } 65 if (s->top ==- 1) { 66 printf("right"); 67 } 68 else 69 { 70 printf("missing )"); 71 } 72 return 0; 73 }