2014-10-17 34 views
1

我试图做一个程序,它初始化单个链表,然后询问用户更多的数据并将它们添加到当前链表的末尾。但是,我的编程陷入了无限循环。带链接列表的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

而当您在调试器中追踪代码时,它确实显示了问题所在,对吗? (为什么你还没有使用调试器?) – abelenky 2014-10-17 17:28:24

回答

2

在你的代码

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

你永远不会改变的current值。它要么以NULL开始,并且循环从不执行,要么开始非NULL并且循环永远运行。

您应该将printf后添加

current = current->next 

推进到链表的下一个元素。

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