无法解决无限循环

时间:2017-04-26 21:58:41

标签: c linked-list stack infinite-loop

该程序应该用户输入的整数,并通过一堆单链表将其转换为二进制。我认为这是我的toBin()函数或我的printStack()函数导致无限循环。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct node_def node;

struct node_def
{
    int val;
    node *next;
};

node *head;

void push(int val);
void pop(node *head);
int top();
void printStack();
int toBin(int val);

int main()
{
    int num = 0;

    printf("Enter an integer: ");
    scanf("%d", &num);
    push(num);
    toBin(num);
    printStack();

    return 0;

}

void push(int val)
{
    node *new;
    new = malloc(sizeof(node));

    if (head == NULL)
    {
        head = malloc(sizeof(node));
        head->next = NULL;
        head->val = val;
    }
    else
    {
        new->next = head;
        new->val = val;
        head = new;

    }

    return;
}

void pop(node *head)
{
    node *tmp;
    if(head == NULL)
    {
        printf("Stack is Empty\n");
        return;
    }
    else
    {
        tmp = head;
        head = head->next;
        free(tmp);
    }
    return;
}

int top()
{
    return(head->val);
}

void printStack()
{
    node *tmp;
    tmp = head;

    if(head == NULL)
    {
        return;
    }

    while(head != NULL)
    {
        printf("%d ", head->val);
        head = head->next;
    }
    printf("\n");
    return;
}

int toBin(int val)
{
    pop(head);
    int i = 1, remainder, binary;

    while(val != 0)
    {
        remainder = val % 2;
        binary = binary + remainder * i;
        val = val / 2;
        i = i * 10;
        push(binary);
    }

    return val;
}

1 个答案:

答案 0 :(得分:1)

由于未正确初始化变量,您会遇到无限循环。特别是,您无法保证将节点* head初始化为NULL,或者将toBin()中的int变量初始化为零。

在使用C / C ++进行编程时,总是,总是初始化变量。

修复这些错误并删除未使用的代码会让我们失望:

#include <stdio.h>
#include <stdlib.h>

typedef struct node_def node;

struct node_def
{
    int val;
    node *next;
};

/* Note that we are initialising the global variable to NULL. */
node *head = NULL; 

void push(int val);
void printStack();
int toBin(int val);

int main()
{
    int num = 0;

    printf("Enter an integer: ");
    scanf("%d", &num);

    /* Removed push(num), as you're using parameters in the following call: */
    toBin(num);
    printStack();

    return 0;

}

/* Changed printStack to use a tmp pointer to 
   traverse the stack without mutating it */
void printStack()
{
    node* tmp = head;
    while(tmp != NULL)
    {
        printf("%d ", tmp->val);
        tmp = tmp->next;
    }
    printf("\n");
    return;
}

int toBin(int val)
{
    /* Removed pop() as you're getting val from parameters */

    /* Also initialising remainder and binary variables */
    int i = 1, remainder = 0, binary = 0;

    while(val != 0)
    {
        remainder = val % 2;
        binary = binary + remainder * i;
        val = val / 2;
        i = i * 10;
        push(binary);
    }

    return val;
}

/* It's a stack so no if's are necessary for pushing */
void push(int val)
{
    node *new = malloc(sizeof(node));
    new->val = val;
    new->next = head;

    head = new;
    return;
}