有人能说出为什么我的链表没有正确显示吗? (C编程)

时间:2017-11-11 05:28:17

标签: c

有人可以告诉我为什么我的代码只打印链表中的最后一个值,而不是真正擅长编码所以帮助会很有用!!

这是代码:

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

typedef struct node {
  int data;  //element
  struct node * next;  //address of next node
} node_t;

node_t * create(int n );
void display(node_t *head);

int main(int argc, char *argv[]) {
  int n=0;
  node_t * HEAD=NULL;
  printf("Enter number of nodes: ");
  scanf("%d",&n);
  HEAD=create(n);
  display(HEAD);
    return 0; 
} 

node_t * create(int n) {
  node_t * head=NULL;
  node_t * temp=NULL; 
  node_t * p=NULL;
  int i;

  for (i=0; i<n; i++) {   // this is just reading the nodes
    temp=(node_t*)malloc(sizeof(node_t));
    printf("\n Enter the data for node num %d: ",i+1);
    scanf("%d",&(temp->data));
    temp->next=NULL;
  } 
  if (head==NULL) {   //if list is item
    head = temp;
  } else {    // this is linking the items.
    p =head;
    while (p->next !=NULL) {
      p=p->next;
      p->next=temp;
    }
  }
  return head;
}

void display(node_t *head) {
  node_t *p = head;
  while (p !=NULL) {
    printf("\n%d->",p->data);
    p=p->next;

  }
}

这是输出:

  
    

输入节点数:3

         

输入节点num 1:2的数据

         

输入节点num 2:4的数据

         

输入节点num 3:1的数据

         

1→

  

3 个答案:

答案 0 :(得分:2)

你想做的事情是什么,但你做的是其他事情。

您正在分配,然后丢失对它的引用。并重新分配。链表的head仍为空。你传递它,等待出现的东西。什么都没发生。

node_t * ttemp;
for (i=0; i<n; i++) {   // this is just reading the nodes
    temp=malloc(sizeof(node_t));
    if( temp == NULL){
       fprintf(stderr,"error in malloc");
       exit(1);
    }
    printf("\n Enter the data for node num %d: ",i+1);
    scanf("%d",&(temp->data));
    temp->next=NULL;
    if( i == 0) head = temp,ttemp=temp;
    else{
        ttemp->next = temp;
        ttemp=ttemp->next;       
    }  
  } 

  return head;

这里分配存储器并存储参考。头被改为并指向开头。

不要忘记释放你分配的记忆。释放链接列表的内存时,free每个节点的内存不仅仅是head

也不要投出malloc的结果。

node_t * create(int n) {
  node_t * head, *temp, *ttemp, *p;
  int i;

  for (i=0; i<n; i++) {   // this is just reading the nodes
    temp=malloc(sizeof(node_t));
    if( temp == NULL){
       fprintf(stderr,"error in malloc");
       exit(1);
    }
    printf("\n Enter the data for node num %d: ",i+1);
    scanf("%d",&(temp->data));
    temp->next=NULL;
    if( i == 0) head = temp,ttemp=temp;
    else{
        ttemp->next = temp;
        ttemp=ttemp->next;       
    }  
  } 
  return head;
  }

此外,您还必须拥有此功能。当您使用完列表时,请将其调用。

void freemem(node_t* head){
    node_t *temp;
    while(head){
        tenp=head;
        head=head->next;
        free(temp);
    }
}

完整代码如下: -

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

typedef struct node {
  int data;  //element
  struct node * next;  //address of next node
} node_t;

node_t * create(int n );
void display(node_t *head);
void freemem(node_t* head){
    node_t *temp;
    while(head){
        temp=head;
        head=head->next;
        free(temp);
    }
}
int main(int argc, char *argv[]) {
  int n=0;
  node_t * HEAD=NULL;
  printf("Enter number of nodes: ");
  scanf("%d",&n);
  HEAD=create(n);
  display(HEAD);
  freemem(HEAD);
  HEAD=NULL;
    return 0; 
} 

node_t * create(int n) {
  node_t * head, *temp, *ttemp, *p;
  int i;

  for (i=0; i<n; i++) {   // this is just reading the nodes
    temp=malloc(sizeof(node_t));
    if( temp == NULL){
       fprintf(stderr,"error in malloc");
       exit(1);
    }
    printf("\n Enter the data for node num %d: ",i+1);
    scanf("%d",&(temp->data));
    temp->next=NULL;
    if( i == 0) head = temp,ttemp=temp;
    else{
        ttemp->next = temp;
        ttemp=ttemp->next;       
    }  
  } 

  return head;
}

void display(node_t *head) {
  node_t *p = head;
  while (p !=NULL) {
    printf("\n%d->",p->data);
    p=p->next;

  }
}

答案 1 :(得分:2)

对于以下代码:

  for (i=0; i<n; i++) {   // this is just reading the nodes
    temp=(node_t*)malloc(sizeof(node_t));
    printf("\n Enter the data for node num %d: ",i+1);
    scanf("%d",&(temp->data));
    temp->next=NULL;
  } 

您一遍又一遍地分配temp并仅使用最后一个。

我的建议:

  node_t* head = NULL;
  node_t* tail = NULL;
  for (i=0; i<n; i++) {   // this is just reading the nodes
      temp=(node_t*)malloc(sizeof(node_t));
      printf("\n Enter the data for node num %d: ",i+1);
      scanf("%d",&(temp->data));

      if (head == NULL) {
           head = temp;   // first one
      }
      else {
          tail->next = temp;
      }
      tail = temp;
   }
   return head;

答案 2 :(得分:0)

只是在'}'

中错误地关闭大括号create的问题
  for (i=0; i<n; i++) {   // this is just reading the nodes
    temp=(node_t*)malloc(sizeof(node_t));
    printf("\n Enter the data for node num %d: ",i+1);
    scanf("%d",&(temp->data));
    temp->next=NULL;
  // } this needs to go to end of loop 
  if (head==NULL) {   //if list is item
    head = temp;
  } else {    // this is linking the items.
    p = head;
    while (p->next !=NULL) {
      p=p->next;
    } // one step up is here
      p->next=temp;
    //} This needs to go one step up
  }
 } // <-- end of loop is here

,这与你的逻辑相同

node_t * create(int n){

    node_t * head=NULL;
    node_t * temp=NULL;
    node_t * p=NULL;
    int i;

    for (i = 0; i < n; i++ ) {   // this is just reading the nodes
            temp = malloc(sizeof(node_t));
            if( temp ) {
                    printf("\n Enter the data for node num %d: ",i+1);
                    scanf("%d",&(temp->data));
                    temp->next = NULL;
                    if (head == NULL) {   //if list is item
                            head = temp;
                    } else {    // this is linking the items.
                            p = head;
                            while (p->next)
                                    p=p->next;
                            p->next=temp;
                    }
            } else {
                    printf("\n oops malloc !! ");
            }
    }
    return head;

}