2017-11-11 17 views
-1

有人能告訴我爲什麼我的代碼只打印鏈表中的最後一個值,不太擅長編碼,所以幫助會有用!有人可以告訴我爲什麼我的鏈表無法正確顯示嗎? (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

輸入數據節點號碼2:4

輸入數據節點號碼3:1

1->

+0

內存泄漏....你自己寫了這段代碼? – coderredoc

+0

一種,遵循YouTube教程大聲笑..哦,天哪我不是很好,malloc和東西。我該如何解決? – Rita

+0

你設置了多少次'p-> next = temp;'?當你做什麼時,每個'p-> next'會發生什麼? –

回答

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; 

    } 
} 
+0

如果temp爲NULL,爲什麼我們必須退出?此外,我只是做免費(頭)或像我如何釋放小節點? – Rita

+0

@Rita .:因爲那你肯定沒有分配內存。所以你會終止程序。 – coderredoc

+0

@Rita .:也嘗試執行我所說的任何事情......使用freemem並學習使用調試器 – coderredoc

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; 
+0

因此,您添加了尾部,tail是否代表列表中的最後一個元素?即時通訊仍然有點困惑,尾巴指向溫度,但然後它成爲溫度? – Rita

+0

循環的每次迭代都會將新分配的節點「temp」附加到列表的末尾。因此,列表指針的現有結尾「tail」需要指向列表的末尾。 – selbie

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 

這裏misplaces花括號'}'的問題是你的同樣的邏輯

node_t *創建(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; 

}

相關問題