LIFO 接口 Stack.h

 //LIFO 链栈初始化
void InitStack(Stack top){
//LIFO 链栈判断栈空
boolean StackKEmpty(Stack top){
//LIFO 链栈进栈
void Push(Stack top, ElemType x){
//LIFO 链栈出栈
ElemType Pop(Stack top){
//LIFO 链栈读取栈顶
ElemType GetTop(Stack top){

LIFO 接口 链表实现 LinkedStack.c

 //LIFO 链栈初始化
void InitStack(Stack top){
top = NULL;
} //LIFO 链栈判断栈空
boolean StackKEmpty(Stack top){
if(top == NULL) return true;
else return false;
} //LIFO 链栈进栈
void Push(Stack top, ElemType x){
LinkedStack p;
p = malloc(sizeof *p);
p -> data =x;
p -> next = top;
top = p;
} //LIFO 链栈出栈
ElemType Pop(Stack top){
LinkedStack p;
ElemType x;
if(top == NULL){
printf("栈下溢错误!\n");
exit();
}
p = top;
x = p -> data;
top = top -> next;
free(p);
return x;
} //LIFO 链栈读取栈顶
ElemType GetTop(Stack top){
if(top == NULL){
printf("栈下溢错误! \n");
exit();
}
return top -> data;
}

LIFO 接口 数组实现 SeqStack.c

 //LIFO 顺序栈 初始化
void InitStack(Stack s){
s -> top = -;
} //LIFO 顺序栈判断栈空
boolean StackEmpty(Stack s){
if(s -> top == -) return true;
else return false;
} //LIFO 顺序栈判断栈满
boolean StackFull(Stack s){
if(s -> top == MaxSize-) return true;
else return false;
} //LIFO 顺序栈进栈
void Push(Stack s, ElemType x){
if(s->top == MaxSize-){
printf("栈满溢出错误!\n");
exit();
}
s -> top++;
s -> data[s>top] = x;
} //LIFO 顺序栈出栈
ElemType Pop(Stack s){
if(StackEmpty(s){
printf("栈下溢错误!\n");
exit();
}
x = s->data[s->top];
s -> top--;
return x;
} //LIFO 顺序栈读取栈顶元素
ElemType GetTop(Stack s){
if(StackEmpty(s){
printf("下溢错误!\n");
exit();
}
return s -> data[s -> top];
}

进制转换程序 main.c

 #include<stdio.h>
#include<stdlib.h>
#include<Stack.h> void transForm(int m, int n); int main(void){ printf("将十进制数转换为任意进制数实例:\n");
transForm(, );
transForm(, );
transForm(, );
transForm(, );
} void transForm(int m, int n){
int k;
int mm = m; Stack S;
InitStack(&S);
while(m != ){
k = m % n;
Push(S, k);
m /= n;
} printf("十进制数%d转换为%d 进制数为:",mm, n);
while(! StackEmpty(&S)){
k = Pop(S, k);
printf("%d", k);
}
putchar('\n');
}

 

05-08 15:19