【问题标题】:Can't Solve Infinite Loop无法解决无限循环
【发布时间】:2017-04-26 21:58:40
【问题描述】:

该程序应该接受用户输入的整数并通过单链表堆栈将其转换为二进制。我认为是我的 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;
}

【问题讨论】:

  • printStack() 中定义tmp 但不要使用它。相反,您修改全局head,从那时起head 就没有用了。
  • @WeatherVane 我也在我的 pop() 函数中做了这个。两者都改完后,无限循环消失了,谢谢!
  • 但是,在 pop(node *head) 中没问题,因为您修改的是按值传递参数,而不是 printStack() 修改的全局变量

标签: c linked-list stack infinite-loop


【解决方案1】:

由于未正确初始化变量,您会陷入无限循环。特别是,您无法保证您的 node* 头将被初始化为 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-23
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多