2017-08-03 47 views
0

我对数据结构太新了,实际上我昨天才开始。下面是代码:链接列表出现问题(添加和打印)

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

struct node 
{ 
    int x; 
    node *next; 
}; 

void addToList(node *r, int a); 
void printList(node *r); 
int main() 
{ 
    node *root; 
    root = NULL; 

    for (int i = 0; i < 5; i++) 
    { 
     int a; 
     scanf("%d", &a); 
     addToList(root, a); 
    } 

    printList(root); 

    return 0; 
} 

void addToList(node *r, int a) 
{ 
    while (r != NULL) 
     r = r -> next; 

    r = (node *)malloc(sizeof(node)); 
    r -> x = a; 
    r -> next = NULL; 
} 

void printList(node *r) 
{ 
    while (r != NULL) 
    { 
     printf("%d ", r -> x); 
     r = r -> next; 
    } 

    printf("\n"); 
} 

我希望程序获取新的5元到列表中,然后打印它们。但该计划的结束没有发生。我的错是什么?

+0

应该'空隙addToList(节点* R,INT A){ 而(!R->下一= NULL) R = R - >下; r-> next =(node *)malloc(sizeof(node)); r-> next-> x = a; r-> next-> next = NULL; }' – roottraveller

+0

抱歉,但没有奏效。 – Atreidex

+0

它定义了第一个元素后工作。我应该永远定义第一个元素吗?没有办法完全清空列表? – Atreidex

回答

1

您有root = NULL但您的addtoList函数检查是否root !=NULL。所以测试失败了,没有增加。 你应该有这样的事情,而不是:

void addToList(node *r, int a) { 
     struct node *temp; 
     temp=(struct node *)malloc(sizeof(struct node)); 
     temp->data = a; 
     if (r== NULL) { 
      r = temp; 
      r->next = NULL; 
     } 
     else { 
      temp->next = r; 
      r = temp; 
     } 
} 
+0

但while循环检查我们是否在最后一个元素。 – Atreidex

+0

第一次运行时,root = null,测试将失败。 – Mekicha

+0

@Mekicha它仍然不会改变'root'指向的地方。 – SHG

1

的问题是在addToList()功能。如果您想更新列表的根节点,你必须像定义你的函数:

void addToList(node **r, int a) 

否则,你要发送的指针root,做任何你在函数内部做。但它不会影响root的值main(),它仍然是NULL

如果要更改指针的值,则必须从main()发送指向函数==>addToList(&root, a);的地址。

所以现在我们可以更新root指向哪里。但这还不够,因为你想root总是指向列表的开始==>你只想在第一次调用addToList()时更新它。

最后一个问题是将新创建的节点添加为列表中的最后一个节点。您可以通过将临时指针保存到最后一个节点来实现这一点。见我的评论的代码(标志着我与<<<变化):

void addToList(node **root, int a)     <<< 
{ 
    node *r = *root;         <<< 
    node *last = NULL;        <<< 

    while (r != NULL) { 
     last = r;         <<< 
     r = r -> next; 
    } 

    r = (node *)malloc(sizeof(node)); 
    r -> x = a; 
    r -> next = NULL; 
    if (last == NULL) {        <<< 
     // this is true only on the first call to 
     // addToList, so we update root only once 
     *root = r; 
    } else { 
     // all other times we add the new node to be the last one 
     last->next = r; 
    } 
} 
1

在这里,第一个错误是,你还没有采取*root指针变量作为全球性的,所以它不会更新*root时的值插入一个新节点。它将保留*root的值为NULL

下面的代码有它的评论,这将解释你很容易做的各种错误。

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

struct node 
{ 
    int x; 
    node *next; 
}; 
node *root;  //Declaring the *root as global 

void addToList(int a); 
void printList(); 
//removing the *root as parameter from both the functions 

int main() 
{ 
    root = NULL; 
    for (int i = 0; i < 5; i++) 
    { 
    int a; 
    scanf("%d", &a); 
    addToList(a); 
    } 
    printList(); 
    return 0; 
} 

void addToList(int a) 
{ 
    //Declaring a temporary pointer(*temp) to avoid the value loss of the *root pointer 
    node *temp=root; 

    //Declaring a new node to save the data taken from the user 
    node *nn = (node *)malloc(sizeof(node)); 

    //Assigning the values to the new node(*nn) 
    nn->x=a; 
    nn->next=NULL; 

    //Checking that the root node is NULL or not 
    //If root is empty, then new node is assigned to *root 
    if(root == NULL) 
    { 
     root=nn; 
    } 
    //Else, we will first find the last node of the linklist using the *temp pointer 
    else 
    { 
     while (temp->next != NULL) 
      temp = temp -> next; 

     //Assigning the new node after the last node of the linklist 
     temp->next=nn; 
    } 
} 

void printList() 
{ 
    node *r=root; 
    while (r != NULL) 
    { 
     printf("%d ", r -> x); 
     r = r -> next; 
    } 
    printf("\n"); 
}