LIFO栈ADT接口实现十进制转其他进制
来源:net 网络用户发布,如有版权联系网管删除 2018-10-12
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.c
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.c
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.c
1 #include 2 #include 3 #include 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 }
查看评论 回复
"LIFO栈ADT接口实现十进制转其他进制"的相关文章
- 上一篇:LIFO栈ADT接口数组实现
- 下一篇:第二周学习小结