链表程序

时间:2011-04-11 12:42:24

标签: c linked-list

我期待以下链接列表程序打印1 但它不是任何人都能找出原因吗?

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

struct node
{
  int data;
  struct node * link;
};

typedef struct node  NODE;
void display(NODE *);
void add(NODE *,int );

int main()
{
  NODE *head=NULL;
  add(head,1);
  display(head);
  printf("\n");
  getch();
  return 0;
}

void display(NODE *pt)
{
  while(pt!=NULL)
  {
    printf("element is");
    printf("%d",pt->data);
    pt=pt->link;
  }
}

void add(NODE *q,int num)
{
  NODE *temp;
  temp = q;
  if(q==NULL)
  {
    q=(NODE *)malloc(sizeof(struct node));
    temp = q;
  }
  else
  {
    while((temp=temp->link)!=NULL);
    temp->link = (NODE *)malloc(sizeof(struct node));
    temp=temp->link;
  }

  temp->data = num;
  temp->link  = NULL;
}

7 个答案:

答案 0 :(得分:2)

head功能未修改main()中的本地变量add()。这意味着您使用参数NULL调用display()

您需要将NODE **q传递给add,然后在add()中更新。

答案 1 :(得分:1)

add()函数正在修改q参数,但它是按值传递的。然后在add()调用之后head保持为NULL。

答案 2 :(得分:1)

第一次调用时add方法(head == NULL时)应将第一个节点添加到列表中,从而将head更改为指向新分配的节点。

但是这不会发生,因为add没有将已更改的head传回给调用者。

要解决此问题,您可以从函数中返回修改后的head

// call as before but assign the return value to head.
head = add(head,1);

.....

// return type changed from void to NODE *
NODE* add(NODE *q,int num)
{
    // make the changes as before

    // return the head pointer.  
    return q;
}

或者您可以通过地址将指针head传递给函数add,如下所示:

// pass the address of head.
add(&head,1);

.....

// change type of q from NODE * to NODE **
void add(NODE **q,int num)
{
  // same as before but change q to *q.

  // any changes made to the list here via q will be visible in caller.
}

答案 3 :(得分:1)

问题在于add方法的签名,为了使你的程序工作你应该将指针传递给NODE的指针,就像这样

void add(NODE **,int );

和他一起工作。 然后以防万一

if(*q==NULL)

您可以分配内存并将NULL指针替换为新的HEAD 通过它

*q=(NODE*)malloc(sizeof(struct node));

所以它会起作用。

问题是,当您分配内存时,只需将空指针的本地副本替换为NODE,但它不会影响主函数中的头部。

答案 4 :(得分:0)

int main()
{
  NODE *head=NULL;
  add(head,1);
  display(head);

NODE * head是本地主要的。它的值是NULL。你传递 NULL来添加,然后创建一个NODE并将其数据设置为1.然后返回main ... ...其中head是 STILL NULL 。您需要传递head的地址,以便在add()中更改它的实际值。您还需要更改add()以使用指针。

Main应返回EXIT_SUCCESS或EXIT_FAILURE。不要typedef struct node;它对可读性有害,你不会在这里使用它。

答案 5 :(得分:0)

当你调用Add时,永远不会返回新的头指针。所以它仍然指向NULL。

答案 6 :(得分:0)

啊......你被指针绊倒了...... 基本上,如果你想修改“head”,你需要发送一个引用...否则你只是修改指针...将你的代码改为:

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

struct node 
{ 
    int data; 
    struct node * link; 
}; 

typedef struct node NODE; 
void display(NODE *); 
void add(NODE **,int ); 

int main() 
{ 
    NODE *head=NULL; 
    add(&head,1); 
    display(head); 
    printf("\n"); 
    getch(); 
    return 0; 
} 

void display(NODE *pt) 
{ 
    while(pt!=NULL) 
    { 
        printf("element is "); 
        printf("%d",pt->data); 
        pt=pt->link; 
    } 
} 

void add(NODE **q,int num) 
{ 
    NODE *temp; 
    temp = *q; 
    if(*q==NULL) 
    { 
        *q=(NODE *)malloc(sizeof(struct node)); 
        temp = *q; 
    } 
    else 
    { 
        while((temp=temp->link)!=NULL); 
            temp->link = (NODE *)malloc(sizeof(struct node)); 
            temp=temp->link; 
    } 

    temp->data = num; 
    temp->link = NULL; 
}
相关问题