2014-09-11 38 views
0

我是鏈接LIsts的新手,我試圖在C中實現鏈表。 下面在我的代碼: -C中的鏈表實現C

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

struct node { 
    int data; 
    struct node *next; 
}; 
void insert (struct node *head, int data); 
void print (struct node *head); 
int main() 
{ 
    struct node *head ; 
    head= NULL; 
    printf("new\n"); 
    insert(head,5); 
    printf("%d\n",head); 
    insert(head,4); 
    insert(head,6); 
    print(head); 
    print(head); 
    print(head); 


} 
void insert(struct node *head,int data){ 

    printf("%d\n",head); 
    if(head == NULL){ 
     head =malloc(sizeof(struct node)); 
     head->next = NULL; 
     head->data = data; 

    } 
    else { 
     printf("entered else\n"); 
     struct node *tmp = head; 
     if(tmp->next!=NULL){ 
      tmp = tmp->next; 
     } 
     tmp->next = malloc(sizeof(struct node)); 
     tmp->next->next = NULL; 
     tmp->next->data = data; 

    } 

} 


void print (struct node *head) { 
    printf("%d\n",head); 
    struct node *tmp = head; 
    if (head == NULL) { 
     printf("entered null\n"); 
     return; 
    } 
    while (tmp != NULL) { 
     if (tmp->next == NULL) { 
      printf("%0d", tmp->data); 
     } else { 
      printf("%0d -> ", tmp->data); 
     } 
     tmp = tmp->next; 
    } 
    printf("\n"); 
} 

當我運行這段代碼的輸出是: -

new 
0 
0 
0 
0 
0 
entered null 
0 
entered null 
0 
entered null 

頭總是空和它不更新空。它不會在insert中進入else循環。 任何人都可以幫助我解決這個問題。指出我正在做的錯誤。 謝謝

+0

在網絡上的C教程中必須有一個萬億的鏈表​​。爲什麼不從其中一個開始,然後修改它以滿足您的要求? Google上的熱門點擊之一是http://www.cprogramming.com/tutorial/c/lesson15.html – 2014-09-11 06:33:45

+0

「插入」功能提示問題。 – user1336087 2014-09-11 06:36:51

回答

6

您的代碼中可能存在其他錯誤,但是一個大問題是您試圖設置頭節點insert,但隻影響傳入的指針的本地副本,所以它沒有任何影響在發送方:

void insert(struct node *head,int data){ 
    .... 
    head = malloc(sizeof(struct node)); // head is local, caller won't see this 

您還需要確保當你通過不是NULL一個節點,你居然attatch新節點頭部。 您可以通過將指針傳遞給指針或通過返回設置指針來解決第一個問題。例如,

void insert(struct node **head, int data) { 
    if(*head == NULL) { 
    // create the head node 
    ... 
    *head = malloc(sizeof(struct node)); 
    .... 
    else { 
    // create a new node and attach it to the head 
    struct node* tmp = malloc(sizeof(struct node)); 
    .... 
    (*head)->next = tmp; 
    } 
} 

然後,在main,你需要一個指針傳遞於頭指針,即使用地址的運營商&

struct node *head = NULL; 
insert(&head, 5); 

注意一部分問題是該函數試圖做太多。它被稱爲insert,但如果傳入的指針是NULL,它會嘗試創建一個新節點。這將是更好地分離這些職責:

// allocate a node and set its data field 
struct node* create_node(int data) 
{ 
    struct node* n = malloc(sizeof(struct node)); 
    n->next = NULL; 
    n->data = data; 
    return n; 
} 

// create a node and add to end node. 
// return the new end node. 
// end has to point to a valid node object. 
struct node* append_node(struct node* tail, int node_data) 
{ 
    struct node* new_tail = create_node(node_data); 
    tail->next = new_tail; 
    return new_tail; 
} 
+0

你爲什麼要做(* head) - > next = tmp? – Newbie 2014-09-11 07:14:45

+0

@Newbie因爲在這個例子中'head'是一個指向指針的指針。所以'head'使我可以訪問指針,並且'(* head) - >'可以訪問指針所指向的對象的元素。但我更喜歡筆記中建議的責任分工方法。 – juanchopanza 2014-09-11 07:17:57

+0

還有一件事,我們必須通過插入(&head,5)。爲什麼我們必須通過&head? – Newbie 2014-09-11 07:37:22

-2

無效插入(結構節點&頭,int數據){//傳引用,而不是指針 ^ 你傳遞一個指針,但只會讓你改變它指向的數據。爲了改變指針本身,你必須傳遞一個引用。不知道我的C是不是有點生疏,但我只是指出你在正確的方向....

問候,

安德烈

+1

在C中,refrences不存在。只有指針。他應該使用指向指針的指針,或從函數返回一個指針。參考文件不存在。 – LoPiTaL 2014-09-11 06:42:20

+0

對不起,你是對的。但是從樣本給出我無法辨別提問者是使用C還是C++,而且自從我上次使用C以來已經有一段時間了。 – 2014-09-12 08:39:13

-1

我有固定的insert功能:

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

struct node { 
    int data; 
    struct node *next; 
}; 
#define CREATENODE malloc(sizeof(struct node)) 
void insert (struct node *head, int data); 
void print (struct node *head); 
int main() 
{ 
    struct node *head ; 
    head = CREATENODE; 
    printf("new\n"); 
    insert(head,5); 
    insert(head,4); 
    insert(head,6); 
    print(head); 
    print(head); 
    print(head); 


} 
void insert(struct node *head,int data){ 
    struct node *temp, *nn; 
    for(temp=head;temp->next!=NULL;temp=temp->next); 
    nn=CREATENODE; 
    nn->data = data; 
    nn->next =temp->next; 
    temp->next = nn; 
} 


void print (struct node *head) { 
    struct node *tmp = head->next; 
    if (head == NULL) { 
     printf("entered null\n"); 
     return; 
    } 
    while (tmp != NULL) { 
     if (tmp->next == NULL) { 
      printf("%0d", tmp->data); 
     } else { 
      printf("%0d -> ", tmp->data); 
     } 
     tmp = tmp->next; 
    } 
    printf("\n"); 
}