LIFO 接口 Stack.h数组
1 //LIFO 链栈初始化
2 void InitStack(Stack top){ 3 //LIFO 链栈判断栈空
4 boolean StackKEmpty(Stack top){ 5 //LIFO 链栈进栈
6 void Push(Stack top, ElemType x){ 7 //LIFO 链栈出栈
8 ElemType Pop(Stack top){ 9 //LIFO 链栈读取栈顶
10 ElemType GetTop(Stack top){
LIFO 接口 链表实现 LinkedStack.cspa
1 //LIFO 链栈初始化
2 void InitStack(Stack top){ 3 top = NULL; 4 } 5
6 //LIFO 链栈判断栈空
7 boolean StackKEmpty(Stack top){ 8 if(top == NULL) return true; 9 else return false; 10 } 11
12 //LIFO 链栈进栈
13 void Push(Stack top, ElemType x){ 14 LinkedStack p; 15 p = malloc(sizeof *p); 16 p -> data =x; 17 p -> next = top; 18 top = p; 19 } 20
21 //LIFO 链栈出栈
22 ElemType Pop(Stack top){ 23 LinkedStack p; 24 ElemType x; 25 if(top == NULL){ 26 printf("栈下溢错误!\n"); 27 exit(1); 28 } 29 p = top; 30 x = p -> data; 31 top = top -> next; 32 free(p); 33 return x; 34 } 35
36 //LIFO 链栈读取栈顶
37 ElemType GetTop(Stack top){ 38 if(top == NULL){ 39 printf("栈下溢错误! \n"); 40 exit(1); 41 } 42 return top -> data; 43 }
LIFO 接口 数组实现 SeqStack.ccode
1 //LIFO 顺序栈 初始化
2 void InitStack(Stack s){ 3 s -> top = -1; 4 } 5
6 //LIFO 顺序栈判断栈空
7 boolean StackEmpty(Stack s){ 8 if(s -> top == -1) return true; 9 else return false; 10 } 11
12 //LIFO 顺序栈判断栈满
13 boolean StackFull(Stack s){ 14 if(s -> top == MaxSize-1) return true; 15 else return false; 16 } 17
18 //LIFO 顺序栈进栈
19 void Push(Stack s, ElemType x){ 20 if(s->top == MaxSize-1){ 21 printf("栈满溢出错误!\n"); 22 exit(1); 23 } 24 s -> top++; 25 s -> data[s>top] = x; 26 } 27
28 //LIFO 顺序栈出栈
29 ElemType Pop(Stack s){ 30 if(StackEmpty(s){ 31 printf("栈下溢错误!\n"); 32 exit(1); 33 } 34 x = s->data[s->top]; 35 s -> top--; 36 return x; 37 } 38
39 //LIFO 顺序栈读取栈顶元素
40 ElemType GetTop(Stack s){ 41 if(StackEmpty(s){ 42 printf("下溢错误!\n"); 43 exit(1); 44 } 45 return s -> data[s -> top]; 46 }
进制转换程序 main.corm
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<Stack.h>
4
5 void transForm(int m, int n); 6
7 int main(void){ 8
9 printf("将十进制数转换为任意进制数实例:\n"); 10 transForm(1567, 8); 11 transForm(1567, 6); 12 transForm(1567, 4); 13 transForm(1567, 2); 14 } 15
16 void transForm(int m, int n){ 17 int k; 18 int mm = m; 19
20 Stack S; 21 InitStack(&S); 22 while(m != 0){ 23 k = m % n; 24 Push(S, k); 25 m /= n; 26 } 27
28 printf("十进制数%d转换为%d 进制数为:",mm, n); 29 while(! StackEmpty(&S)){ 30 k = Pop(S, k); 31 printf("%d", k); 32 } 33 putchar('\n'); 34 }