LIFO栈 ADT接口 实现十进制转其他进制

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(1);
     }
     p = top;
     x = p -> data;
     top = top -> next;
     free(p);
     return x;
}

//LIFO 链栈读取栈顶
ElemType GetTop(Stack top){
     if(top == NULL){
        printf("栈下溢错误! \n");
        exit(1);
     }
     return top -> data;
}

LIFO 接口 数组实现 SeqStack.c

//LIFO 顺序栈 初始化
void InitStack(Stack s){
     s -> top = -1;
}

//LIFO 顺序栈判断栈空
boolean StackEmpty(Stack s){
     if(s -> top == -1) return true;
     else return false;
}

//LIFO 顺序栈判断栈满
boolean StackFull(Stack s){
     if(s -> top == MaxSize-1) return true;
     else return false;
}

//LIFO 顺序栈进栈
void Push(Stack s, ElemType x){
     if(s->top == MaxSize-1){
        printf("栈满溢出错误!\n");
        exit(1);
     }
     s -> top++;
     s -> data[s>top] = x;
}

//LIFO 顺序栈出栈
ElemType Pop(Stack s){
     if(StackEmpty(s){
        printf("栈下溢错误!\n");
        exit(1);
     }
     x = s->data[s->top];
     s -> top--;
     return x;
}

//LIFO 顺序栈读取栈顶元素
ElemType GetTop(Stack s){
     if(StackEmpty(s){
        printf("下溢错误!\n");
        exit(1);
     }
     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(1567, 8);
    transForm(1567, 6);
    transForm(1567, 4);
    transForm(1567, 2);
}

void transForm(int m, int n){
    int k;
    int mm = m;

    Stack S;
    InitStack(&S);
    while(m != 0){
    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');
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值