2016-03-01 76 views
0

因此,我試圖找到具有最小值的節點,並將其放在列表的末尾。我有兩個功能試圖找出實現它的多種方式。但列表打印出來不變。LinkedList錯誤?在C

任何幫助,將不勝感激。

我的代碼:

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

typedef struct node * linkk; 
typedef struct struct_list * list; 

struct node { 
    int item; 
    linkk next; 
}; 

int ConnectSmallElementToLast(linkk A); 
int Connect(linkk A); 


int main(int argc, const char * argv[]) { 

    linkk t = malloc(sizeof(*t)); 
    linkk head = t; 


    for (int i = 0; i < 10; i++) 
    { 
     t->item = i; 
     t->next = malloc(sizeof(*t)); 
     t = t->next; 
    } 

    connect(t); 

    for (int i = 0; i <10; i++) 
    { 
     printf("%d\n",head->item); 
     head = head->next; 
    } 





    return 0; 
} 

int ConnectSmallElementToLast(linkk A) 
{ 
    linkk L = A; 
    linkk head = A; 
    linkk printhead = L; 
    linkk smallestNode = NULL; 
    linkk pre = NULL; 
    linkk post = NULL; 
    int count=1, Number; 
    Number = L->item; 
    L = L->next; 
    for (int i = 1; i < sizeof(L);i++) 
    { 
     if(Number > L->item) 
     { 
      Number == L->item; 
      smallestNode = L; 
      post = L->next; 
      L = L->next; 
      count++; 
     } 
     else{L = L->next;} 
    } 
    L->next = smallestNode; 
    for (int i = 0; i< sizeof(head);i++) 
    { 
     if (i == (count-1)) 
     { 
      head->next = post; 
     }else if(head->next == NULL) 
     { 
      head->next = smallestNode; 
     } 
    } 
    for (int i = 0; i<sizeof(printhead);i++) 
    { 
     printf("%d\n",printhead->item); 
     printhead = printhead->next; 
    } 

    return 0; 
} 

int Connect(linkk A) 
{ 
    linkk L = A; 
    linkk pre = NULL; 
    linkk post = NULL; 
    linkk current = NULL; 
    linkk head = L; 
    int smallest; 
    int NumberPre,NumberCur,NumberPost; 
    while (L != NULL && L->next !=NULL && L!=NULL) 
    { 
     pre = L; 
     current = L->next; 
     post = L->next->next; 
     NumberPre = pre->item; 
     NumberCur = L->next->item; 
     NumberPost = L->next->next->item; 
     if (NumberCur < NumberPre && NumberCur < NumberPost) 
     { 
      pre->next = post; 
      smallest = NumberCur; 

     }else if(NumberPre < NumberCur && NumberPre < NumberPost) 
     { 
      L = current; 
      smallest = NumberPre; 
     } 
     pre = pre->next; 
     current = current->next; 
     post = post->next; 

    } 
    for (int i = 0; i<sizeof(head);i++) 
    { 
     if (head->next == NULL) 
     { 
      head->next->item = smallest; 
      head->next->next = NULL; 
     } 
    } 
    return 0; 
} 
+0

't-> next = malloc(sizeof(* t));':'next'最後一個元素必須是'NULL' 。 – BLUEPIXY

回答

0

首先,在所有的,你的代碼是很奇怪實現鏈表。嘗試谷歌其他人如何使用鏈接列表。

從代碼:

linkk t = malloc(sizeof(*t)); 
linkk head = t; 


for (int i = 0; i < 10; i++) 
{ 
    t->item = i; 
    t->next = malloc(sizeof(*t)); 
    t = t->next; 
} 

它創建頭一個鏈表,並在列表中10個節點。

在您的連接方法:

for (int i = 0; i<sizeof(head);i++) 
{ 
    if (head->next == NULL) 
    { 
     head->next->item = smallest; 
     head->next->next = NULL; 
    } 
} 

的sizeof(頭)是的sizeof(linkk),它是一個指向的節點。 好吧,它在32位系統中總是4個字節。

這解釋了爲什麼循環失敗。

此外,你需要malloc一個內存的節點,然後分配給列表。 就像你在main()的for循環中所做的一樣。

還有一件事,人們通常使用'create'函數來包裝創建節點時需要做的所有事情。

例如。 1.分配內存 2.分配值 3.輸入到列表中