简体   繁体   English

为什么我的链表程序会无限地打印出我输入的那个“第一个数字”的循环?

[英]Why does my linked-list program print out a loop of that "first number" I entered, infinitely?

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

struct Node{
    int data;
    struct Node* next; 
};

struct Node* head;

void Insert(int x){ 
    struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
    temp->data  = x; 
    temp->next = head;
    head = temp;
    if(head != NULL)
            temp->next = head;
    head = temp;
}

void Print(){
    struct Node* temp = head; 
    printf("List is: ");
    while(temp != NULL){
        printf("%d", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

int main(){
    head = NULL; //empty list
    printf("How many numbers?\n");
    int n, i;
    scanf("%d", &n);
    for(int i=0; i < n; i++){
        printf("Enter the number\n");
        scanf("%d", &x);
        Insert(x); 
        Print();
    }

Example of the wrong output that I got from my compiler:我从编译器得到的错误 output 示例:

How many numbers?多少个数字?

My input: 2我的输入:2

Enter the number:输入号码:

My input: 1我的输入:1

List is: 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111 and it goes on & on & on列表是: 111111111111111111111111111111111111111111111111111111111111111111111111111111111111 并且它继续 & on & on

I didn't even have the chance to enter the 2nd number, since I put 2 numbers as the amount of numbers.我什至没有机会输入第二个数字,因为我输入了 2 个数字作为数字的数量。

I refactored the code and it's worked.我重构了代码并且它有效。

The infinite loop was due to the first node pointing to the head and the head pointing to the first node.无限循环是由于第一个节点指向头部,而头部指向第一个节点。

temp->next = head;
head = temp;

So when you call Print() the while(temp != NULL) condition never fails.因此,当您调用Print()时, while(temp != NULL)条件永远不会失败。

struct Node{
    int data;
    struct Node* next; 
};

struct Node* head;

void Insert(int x){ 
    struct Node* temp = malloc(sizeof(struct Node));
    if (temp==NULL) { printf("No available space on heap\n"); exit(1); }
    temp->data  = x; 
    temp->next = NULL;
     //check if we have to insert at begining(initial node) or empty list
    if (head==NULL){
        head=temp;
        return ;
    }
    //traverse till the last node to insert new node
    //should not modify the head
    struct Node *t=head;
    while(t->next != NULL) t=t->next;
    t->next= temp;
}

void Print(){
    struct Node* temp = head; 
    printf("List is: ");
    while(temp != NULL){
        printf("%d", temp->data);
        temp = temp->next;
    }
    printf("\n");
}
void destroy_mem(struct Node *head)
{//we need destroy the used memory on heap for further use
    struct Node *curr = NULL;
    while ((curr = head) != NULL)
    {                      // set curr to head, stop if list empty.
        head = head->next; // moving head to next element.
        free(curr);        // delete saved pointer.
    }
    head = NULL;
}
int main(){
    head = NULL; //empty list
    printf("How many numbers?\n");
    int n, i,x;
    scanf("%d", &n);
    for(int i=0; i < n; i++){
        printf("Enter the number\n");
        scanf("%d", &x);
        Insert(x); 
       
    }
  Print();  
  destroy_mem(head);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的链表删除功能不起作用? - Why does my linked-list remove function not work? 当我尝试从单链表中删除节点时,为什么我的 C 程序崩溃 - Why does my C Program crash when I try to delete a node out of a singe linked list 在我的C程序中调试我的链接列表很困难 - Difficulty debugging my linked-list in my C program 如何计算链表中的节点数?为什么输出显示节点数为“2”? - How to count number of nodes in a linked-list ?Why does the output show the count of nodes as '2'? 当使用while循环遍历链接列表时,为什么我的循环迭代列表中节点数的两倍? - When using a while loop to traverse a linked list, why does my loop iterate by twice the number of nodes in the list? 为什么此链表从上次输入中打印? C链表程序 - Why this linked list prints from last input? C linked-list program 链表程序中的分段错误 - Segmentation fault error in Linked-List program 为什么在我的 C 程序中找不到无限循环在链表中搜索? 该程序有效 - Why can't I find the infinite loop in my C program to search in a linked list? The program works 我正在尝试读取和打印链表中的一个值,但我的程序没有给出任何输出 - i am trying to read and print one value in a linked list , but my program does not give any output 链表队列,无限循环 - linked-list queue, endless loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM