带链表的C无限循环

时间:2014-10-17 17:27:00

标签: c

我正在尝试创建一个初始化单个链表的程序,然后向用户询问更多数据,并将其添加到当前链表的末尾。但是,我的编程被困在无限循环中。

#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);


}

3 个答案:

答案 0 :(得分:2)

在您的代码中

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

您永远不会更改current的值。它从NULL开始并且循环从不执行,或者它开始非NULL并且循环永远运行。

你应该添加

current = current->next

printf前进到链接列表中的下一个元素。

答案 1 :(得分:2)

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);
    }
}

答案 2 :(得分:0)

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
    }
}