簡體   English   中英

帶鏈表的C無限循環

[英]C infinite loop with linked list

我正在嘗試制作一個程序來初始化單個鏈接列表,然后向用戶詢問更多數據,並將其添加到當前鏈接列表的末尾。 但是,我的編程陷入了無限循環。

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


typedef struct linked_list
{
int data;
struct linked_list *next;

}element;

typedef element *elementptr;


void addend(elementptr *,int);
void traverse(elementptr);


int main()
{
// create a linked list
elementptr first = NULL,
last = NULL;

int input,num;

printf("Please enter first integer data: \n");
scanf("%d",&input);

//Create a linked list with user initialized data


first = (elementptr)malloc(sizeof(element));
last = first;
last -> data = input;
last -> next = NULL;



printf("Please enter number of data you want to add in the end of linked list\n");
scanf("%d",&num);
addend(&last,num);
traverse(first);



return 0;
}


void addend(elementptr *l, int num)
{

int i = 0,extradata;

while(i<num)
{
    i++;


    printf("Please enter a data: \n");
    scanf("%d",&extradata);

    (*l) -> next = (elementptr)malloc(sizeof(element));
    *l = (*l) ->next;
    (*l) -> data = extradata;
    (*l) -> next = NULL;

}

printf("The data sets are added\n");
}



void traverse(elementptr f)
{
elementptr current;
int count = 0;

current = f;
while (current != NULL)

{
    count++;
    printf("The data is %d \n", current->data);


}

printf("The linked list has %d elements \n",count);


}

在你的代碼中

while (current != NULL)
{
    count++;
    printf("The data is %d \n", current->data);
}

你永遠不會改變current的價值。 它要么以NULL開頭並且循環永遠不執行,要么以非NULL開頭並且循環永遠運行。

您應該添加

current = current->next

在您的printf之后前進到鏈接列表中的下一個元素。

current = current -> next; 您在while循環中缺少此語句。 盡管我建議:

#include<stdio.h>
#include<stdlib.h>
struct linked_list
{
    int number;
    struct linked_list *next;
    struct linked_list *prev;
};
typedef struct linked_list node;
void create(node *p);
void print(node *p);
int main()
{
    node *head;
    printf("---Start of the list ---\n");
    printf("enter -1 to exit\n");
    head = (node*)malloc(sizeof(node));
    create(head);
    print(head);
}
void create(node *list)
{
     printf("enter the data");
     scanf(" %d",&list->number);
     if(list->number == -1)//Enter -1 to exit untill then keep on taking values
     {
         list->next=NULL;
     }
     else
     {
         list->next = (node*)malloc(sizeof(node));
         create(list->next);
     }
}

void print(node *list)
{
    if(list->next != NULL)
    {
        printf("%d-->",list->number);
        if((list->next->next == NULL) && (list->next->number!=-1))
        printf("%d -->End of the List",list->next->number);
        print(list->next);
    }
}

traverse功能中, current節點未指向下一個節點。 所以while條件while (current != NULL)將不成立,因此while循環無限運行。

要修復此問題, current節點應在打印數據后指向列表中的下一個節點( current = current->next )。


例如:

void traverse(elementptr f)
{
    elementptr current;
    int count = 0;

    current = f;
    while (current != NULL)
    {
        count++;
        printf("The data is %d \n", current->data);
        // Fix 
        current = current->next;  // Point to next node in link list
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM