链式栈实现进制转换

   2016年7月25日11:03:37
    利用链表模拟栈,将十进制转换成对应的八进制,二进制,八进制
#include
#include
#include
typedef int DataType ;

//链表结点的结构体类型定义
typedef struct node  
{
    DataType stack;//数据域
    struct node * pNext;//指针域
}linkList;

//链式栈的结构体类型定义
typedef struct stack
{
    linkList * pTop;//指向栈顶元素
    linkList * pBottom;//指向栈底元素
}linkStack;

//函数前置声明
void initLinkStack(linkStack * S);//初始化链式栈
int  isEmpty(linkStack * S);//判断链式栈是否为空
int  pushLinkStack(linkStack * S,DataType element);//链式栈的压栈操作
int  popLinkStack(linkStack * S,DataType * element);//链式栈弹栈操作
int  getTop(linkStack * S, DataType * element);//获取链式栈的栈顶元素
int  getLength(linkStack * S);//获取链式栈的长度
void destroyLinkStack(linkStack * S);//链式栈的销毁

//链式栈的初始化
void initLinkStack(linkStack * S)
{
    S->pTop = (linkList * )malloc(sizeof(linkList));
    if(!S->pTop)
    {
        printf("动态内存分配失败!\n");
        exit(-1);
    }
    S->pBottom = S->pTop;
    S->pBottom->pNext = NULL;
    return ;
}

//判断链式栈是否为空,若为空则返回1,不为空则返回0;
int  isEmpty(linkStack * S)
{
    if(S->pBottom == S->pTop)
        return 1;
    else
        return 0;
}

//链式栈的压栈操作
int  pushLinkStack(linkStack * S,DataType element)
{
    linkList * pNew;

    pNew = (linkList * )malloc(sizeof(linkList));
    if(NULL == pNew )
    {
        printf("动态内存分配失败!\n");
        exit(-1);
    }
    pNew->stack = element;
    /*if( NULL == pTop ) { pNew->pNext = NULL; pTop = pNew; } else { pNew->pNext = pTop; pTop = pNew; }*/
    pNew->pNext = S->pTop;
    S->pTop = pNew;
    return 1;
}

//链式栈的弹栈操作
int  popLinkStack(linkStack * S,DataType * element)
{
    linkList * pFree;

    if( isEmpty(S) )
    {
        printf("栈为空,无法继续出栈");
        return 0;
    }

    pFree = S->pTop;
    *element = pFree->stack;
    S->pTop = S->pTop->pNext;
    free(pFree);
    return 1;
}

//获取链式栈的栈顶元素
int  getTop(linkStack * S, DataType * element)
{
    if(isEmpty(S))
        return 0;
    *element = S->pTop->stack;
    return 1;
}

//获取链式栈的长度
int  getLength(linkStack * S)
{
    int i = 0;
    linkList * pCur;
    pCur = S->pTop;
    /* if(NULL == pCur) { printf("栈为空!\n"); return 0; } */
    while( pCur )
    {
        pCur = pCur->pNext;
        i++;
    }
    return i;
}

//链式栈的销毁
void destroyLinkStick(linkStack * S)
{
    while(S->pTop)
    {
        S->pBottom = S->pTop->pNext;
        free(S->pTop);
        S->pTop = S->pBottom;
    }
    return;
}


//展示系统菜单
void showMenu()
{
    printf("___________________________________________\n");
    printf(" 欢迎使用进制转化系统\n");
    printf("\t 【1】转成八进制\n");
    printf("\t 【2】转成二进制\n");
    printf("\t 【3】转成十六进制\n");
    printf("\t 【4】退出系统\n");
    printf("___________________________________________\n");
    return;
}

//将十进制转化成N进制
void transform(DataType number,linkStack * S,int N)
{
    DataType restNumber;
    /*while(0!=number)//如果所给的十进制数为零的话,程序会出错; { restNumber = number%N; pushLinkStack(pTop,restNumber); number = number/N; }*/
    do 
    {
        restNumber = number%N;
        pushLinkStack(S,restNumber);
        number = number/N;
    } while (0!= number);
    return;
}

//将栈中的元素打印输出
void traverseLinkStack(linkStack * S)
{
    linkList * pCur;
    pCur = S->pTop;
    int i = 0;
    while(pCur != S->pBottom)
    {
        printf("%d",pCur->stack);
            i++;
        if(i%4==0)
            printf(" ");
        pCur = pCur->pNext;
    }
    return ;
}

//主函数
int main(void)
{
    linkStack S;
    initLinkStack(&S);
    DataType number;
    int N;
    int choice;
    while(true)
    {
        getchar();
        showMenu();
        printf("请输入您的选择:");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1: N = 8;
                    break;
            case 2: N = 2;
                    break;
            case 3: N = 16;
                    break;
            case 4: exit(0);
            default:
                    return 0;
        }
        printf("请输入一个十进制整数:");
        scanf("%d",&number);
        transform(number,&S,N);
        printf("转化成N进制后为:");
        traverseLinkStack(&S);
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(C,DataStructure)