順序棧spa
1 #include <stdio.h> 2 #include <malloc.h> 3 4 #define OK 1 5 #define ERROR 0 6 #define INFEASIBLE -1 7 #define OVERFLOW -2 8 #define TRUE 1 9 #define FALSE 0 10 #define STACK_INIT_SIZE 100 11 #define STACKINCREMENT 10 12 13 typedef int Status; 14 15 typedef struct{ 16 int *base; 17 int *top; 18 int stacksize; 19 }SqStack; 20 21 Status InitStack(SqStack &S); 22 Status ClearStack(SqStack &S); 23 Status DestoryStack(SqStack &S); 24 bool StackEmpty(SqStack S); 25 int StackLength(SqStack S); 26 Status GetTop(SqStack S, int &e); 27 Status Push(SqStack &S, int e); 28 Status Pop(SqStack &S, int &e); 29 Status StackTraverse(SqStack S, Status (*visit)(int)); 30 Status visit(int e); 31 32 int main() 33 { 34 int i, e; 35 SqStack S; 36 printf("初始化\n"); 37 InitStack(S); 38 printf("壓棧:\n"); 39 for (i = 0; i < 5; i++) 40 { 41 printf("輸入e:"); 42 scanf("%d", &e); 43 Push(S, e); 44 } 45 printf("彈棧:"); 46 Pop(S, e); 47 printf("%d\n", e); 48 printf("棧長:%d\n", StackLength(S)); 49 printf("取棧頂元素:"); 50 GetTop(S, e); 51 printf("%d\n", e); 52 printf("遍歷棧:\n"); 53 StackTraverse(S, visit); 54 printf("置爲空棧\n"); 55 ClearStack(S); 56 printf("是否爲空棧:%d\n", StackEmpty(S)); 57 printf("銷燬棧:\n"); 58 DestoryStack(S); 59 return 0; 60 } 61 //初始化棧 62 Status InitStack(SqStack &S) 63 { 64 S.base = (int*)malloc(STACK_INIT_SIZE * sizeof(int)); 65 if (!S.base) 66 exit(OVERFLOW); 67 S.top = S.base; 68 S.stacksize = STACK_INIT_SIZE; 69 return OK; 70 } 71 //置爲空棧 72 Status ClearStack(SqStack &S) 73 { 74 S.top = S.base; 75 return OK; 76 } 77 //銷燬棧 78 Status DestoryStack(SqStack &S) 79 { 80 free(S.base); 81 S.base = NULL; 82 S.top = NULL; 83 S.stacksize = 0; 84 return OK; 85 } 86 //是不是空棧 87 bool StackEmpty(SqStack S) 88 { 89 return S.top == S.base; 90 } 91 //棧長 92 int StackLength(SqStack S) 93 { 94 return S.top - S.base; 95 } 96 //取棧頂元素 97 Status GetTop(SqStack S, int &e) 98 { 99 if (S.top == S.base) 100 { 101 return ERROR; 102 } 103 e = *(S.top - 1); 104 return OK; 105 } 106 //壓棧 107 Status Push(SqStack &S, int e) 108 { 109 if (S.top - S.base >= S.stacksize) 110 { 111 S.base = (int*)realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(int)); 112 if (!S.base) 113 { 114 return OVERFLOW; 115 } 116 S.top = S.base + S.stacksize; 117 S.stacksize += STACKINCREMENT; 118 } 119 *S.top = e; 120 S.top ++; 121 return OK; 122 } 123 //彈棧 124 Status Pop(SqStack &S, int &e) 125 { 126 if (S.top == S.base) 127 { 128 return ERROR; 129 } 130 e = *(S.top - 1); 131 S.top--; 132 return OK; 133 } 134 //遍歷棧 135 Status StackTraverse(SqStack S, Status (* visit)(int)) 136 { 137 while (S.top > S.base) 138 { 139 visit(*S.base); 140 S.base++; 141 } 142 return OK; 143 } 144 145 Status visit(int e) 146 { 147 printf("%d\n", e); 148 return OK; 149 }
鏈棧code
1 #include <stdio.h> 2 #include <malloc.h> 3 4 #define TRUE 1 5 #define FALSE 0 6 #define OK 1 7 #define ERROR 0 8 #define INFEASIBLE -1 9 #define OVERFLOW -2 10 11 typedef int Status; 12 13 typedef struct StackNode{ 14 int data; 15 struct StackNode *next; 16 }StackNode; 17 18 typedef struct LinkStack{ 19 StackNode *top; 20 int stacksize; 21 }LinkStack; 22 23 Status InitLinkStack(LinkStack &L); 24 Status ClearLinkStack(LinkStack &L); 25 bool LinkStackEmpty(LinkStack L); 26 Status Push(LinkStack &L, int e); 27 Status Pop(LinkStack &L, int &e); 28 Status LinkStackTraverse(LinkStack L, Status (*visit)(int)); 29 Status visit(int e); 30 31 int main() 32 { 33 int i, e; 34 LinkStack L; 35 printf("初始化\n"); 36 InitLinkStack(L); 37 printf("壓棧:\n"); 38 for (i = 0; i < 5; i++) 39 { 40 printf("輸入e:"); 41 scanf("%d", &e); 42 Push(L, e); 43 } 44 printf("遍歷棧:\n"); 45 LinkStackTraverse(L, visit); 46 printf("彈棧:"); 47 Pop(L, e); 48 printf("%d\n", e); 49 printf("遍歷棧:\n"); 50 LinkStackTraverse(L, visit); 51 printf("置爲空棧\n"); 52 ClearLinkStack(L); 53 printf("是否爲空棧:%d\n", LinkStackEmpty(L)); 54 return 0; 55 } 56 //初始化鏈棧 57 Status InitLinkStack(LinkStack &L) 58 { 59 L.stacksize = 0; 60 L.top = NULL; 61 return OK; 62 } 63 //置爲空棧 64 Status ClearLinkStack(LinkStack &L) 65 { 66 StackNode *p; 67 while (L.top) 68 { 69 p = L.top->next; 70 free(L.top); 71 L.top = p; 72 } 73 L.stacksize = 0; 74 return OK; 75 } 76 //是否爲空棧 77 bool LinkStackEmpty(LinkStack L) 78 { 79 return L.top == NULL; 80 } 81 //壓棧 82 Status Push(LinkStack &L, int e) 83 { 84 StackNode *p; 85 p = (StackNode*)malloc(sizeof(StackNode)); 86 if (!p) 87 { 88 exit(OVERFLOW); 89 } 90 p->data = e; 91 p->next = L.top; 92 L.top = p; 93 L.stacksize++; 94 return OK; 95 } 96 //彈棧 97 Status Pop(LinkStack &L, int &e) 98 { 99 StackNode *p; 100 if (LinkStackEmpty(L)) 101 { 102 return ERROR; 103 } 104 e = L.top->data; 105 p = L.top->next; 106 free(L.top); 107 L.top = p; 108 L.stacksize--; 109 return OK; 110 } 111 //遍歷棧 112 Status LinkStackTraverse(LinkStack L, Status (*visit)(int)) 113 { 114 if (LinkStackEmpty(L)) 115 { 116 return ERROR; 117 } 118 while (L.top) 119 { 120 visit(L.top->data); 121 L.top = L.top->next; 122 } 123 return OK; 124 } 125 126 Status visit(int e) 127 { 128 printf("%d\n", e); 129 return OK; 130 }