标题:十进制转八进制用链栈写 显示没有错误,但是不能运行
只看楼主
Jessica_Rong
Rank: 1
来 自:河北唐山
等 级:新手上路
帖 子:25
专家分:0
注 册:2017-5-12
结帖率:83.33%
已结贴  问题点数:10 回复次数:4 
十进制转八进制用链栈写 显示没有错误,但是不能运行

#include<stdio.h>
#include<stdlib.h>
typedef struct aaa
{
    char ch;
    struct aaa *next;
}AAA;
int main()
{
    AAA *p,*q,*s,*top;
    int n;
    top=(AAA *)malloc(sizeof(AAA));
    top->next=NULL;
    printf("请输入你想转换的数:");
    scanf("%d",&n);
    while(n)
    {
        s->ch=n%8;
        n=n/8;
        p->next=top->next;
        top->next=p;
        break;
    }
    printf("\n");
    while(top!=0)
    {
        s=top->next;
        printf("转换为:%d\n",top->next);
        top->next=s->next;
        free(s);
    }
        printf("栈已空!\n");
}
搜索更多相关主题的帖子: include 八进制 十进制 
2017-05-19 11:49
Jessica_Rong
Rank: 1
来 自:河北唐山
等 级:新手上路
帖 子:25
专家分:0
注 册:2017-5-12
得分:0 
刚才忘了加return 0;还是现在这样运行不了
2017-05-19 11:50
Emotiona
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:311
专家分:581
注 册:2017-3-7
得分:3 
你只开辟了top的空间,q p s都没有开辟空间,相当于指针直接赋值,给top的话就不会出错
2017-05-19 12:13
Emotiona
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:311
专家分:581
注 册:2017-3-7
得分:3 
程序代码:
#include<stdio.h>
#include<stdlib.h>

#define NUMBERSIZE  50

typedef struct aaa
{
    char ch;
    struct aaa *next;
} AAA;
int main()
{
    AAA *p1, *p2, *head = NULL;
    int cot = 0, n;

    printf("请输入你想转换的数:");
    scanf("%d",&n);
    while(n)
    {
        p1=(AAA *)malloc(sizeof(AAA));
        p1->ch = n % 8 + '0';
        n = n / 8;
        if(head == NULL)
            head = p2 = p1;
        else
        {
            p2->next = p1;
            p2 = p1;
        }
    }
    p2->next = NULL;
    AAA *p, *List[NUMBERSIZE];
    p = head;
    while(p != 0)               //指针存放数字对应8进制倒置的字符
    {
        List[cot++] = p;
        printf("%c",p->ch);
        p = p->next;
    }
    printf("\n");
    while(cot > 0)              //将字符转成相应的进制
        printf("%c",List[--cot]->ch);
        printf("\n栈已空!\n");
    return 0;
}

2017-05-19 14:37
Emotiona
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:311
专家分:581
注 册:2017-3-7
得分:4 
上面的是链表,下面的是链栈

程序代码:
#include <stdio.h>
#include <malloc.h>

typedef struct Node
{
    char val;
    struct Node *next;
    struct Node *top;
}Stack;

Stack * Init_stack()
{
    Stack *p = NULL;
    p = (Stack *)malloc(sizeof(Stack));
    p->top = NULL;
    return p;
}

Stack *Push_stack(Stack *p, char val)
{
    Stack *temp;
    temp = (Stack *)malloc(sizeof(Stack));
    if (p == NULL || temp == NULL)
        return p;
    temp->next = p->top;
    p->top = temp;
    temp->val = val;
    return  p;
}

void Show_stack(Stack *p)
{
    Stack *temp = p->top;

    if (p == NULL)
        printf("The stack is empty!\n");
    else
        while (temp != NULL)
        {
            printf("%c", temp->val);
            temp = temp->next;
        }
        


}

int main() 
{
    long long n;
    int num;
    Stack *p;

    printf("Please input decimal integet to X\n");
    scanf("%lld", &n);
    printf("Please input number\n");
    scanf("%d", &num);

    p = Init_stack();
    while (num)
    {

            Push_stack(p, (num % n <= 10) ? num % n + '0' : num % n - 10 + 'A');
            num /= n;

    }
    printf("\nconverted back :\n\n");
    Show_stack(p);
    printf("\n\n");

    return 0;
}

2017-05-19 22:51



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-477145-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.989134 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved