2012-11-05 73 views
1

我正在做我的家庭作業,而且這是我已經得到的。我現在需要知道如何將輸入到列表中的信息打印出來。我需要重新配置insertNode函數以將列表從最小到最大排序。已命令鏈接列表打印

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

struct listNode{ 
    int data; //ordered field 
    struct listNode *next; 
}; 

//prototypes 
void insertNode(struct listNode *Head, int x); 
int printList(struct listNode *Head, int x); 
int freeList(struct listNode *Header, int x); 

//main 
int main(){ 
    struct listNode Head = {0, NULL}; 
    int x = 1; 
    printf("This program will create an odered linked list of numbers greater" 
    " than 0 until the user inputs 0 or a negative number.\n"); 
    while (x > 0){ 
    printf("Please input a value to store into the list.\n"); 
    scanf("%d", &x); 
      insertNode(&Head, x); 
    } 
    printf("Program terminated.\n"); 
    system("PAUSE"); 
    } 
void insertNode(struct listNode * Head, int x){ 
    struct listNode *newNode, *current; 
    newNode = malloc(sizeof(struct listNode)); 
    newNode->data = x; 
    newNode->next = NULL; 
    current = Head; 
    while (current->next != NULL && current->data < x) 
    { 
     current = current->next; 
     } 

     if(current->next == NULL){ 
      current->next = newNode; 
     } 
     else{ 
      newNode->next = current->next; 
      current->next = newNode; 
     } 
} 
+0

普羅蒂普:'的malloc(的sizeof(* listNode));' – Wug

+0

[打印有序鏈表(HTTP的可能重複://計算器。com/questions/13240332/print-an-ordered-linked-list) – Joe

回答

3

Header->next爲NULL,當你開始,當你添加一個元素你改變電流爲Header

current = Header; 
write(current->next !=NULL); 
// set current to be Header->next (which is NULL) 
current = current->next; 
// dereference null 
current->next = newNode; 

相反,新的元素添加到末尾:

current = Header; 
while (current->next != NULL) 
    current = current->next; 
current->next = newNode; 
0
current = Header; 
write(current->next !=NULL); 
current = current->next; 
current->next = newNode; 

您正嘗試訪問current-> next,但沒有分配它。而且你實際上並不是在尋找正確的位置來將它插入鏈表(從你的問題來看,它聽起來像是應該排序的)。

試着這麼做:

current = Header; 
while (current->next != NULL && current->data < x) 
{ 
    current = current->next; 
} 

if(current->next == NULL) 
{ 
    current->next = newNode; 
} 
else 
{ 
    newNode->next = current->next; 
    current->next = newNode; 
} 
+0

我試過了,它似乎有效!我用我的新問題編輯了原文。 – user1801067

+0

我爲你的其他問題添加了一個答案,以向你展示你在做什麼錯誤:http://stackoverflow.com/questions/13240332/print-an-ordered-linked-list/13242728#13242728 –

0

,我們在您IMPL三個問題:

  1. 你應該在最後追加新的號碼,所以你應該有兩個指針到列表中:一個指向頭部,另一個尾部(你也可以添加在列表頂部,那麼你只需要一個指針,但是然後列表是反向排序的)

  2. poi中斷鏈代碼是錯誤的,我想你想嘗試在你的頭節點後插入新的元素,所以你的代碼應該寫成:

    current = Header-> next; //節點Header-> next當前指向

    Header-> next = newNode; //讓標題 - >下一個指向新電子元件

    newNode-> next = current; //讓新ele的下一個點指向老top ele

  3. header-node在某種程度上是特殊和無用的,你應該處理一個空列表作爲特殊情況,並且標題應該初始爲0,並且insertnode會像這樣工作:

    如果(頭== 0)

    header=newNode 
    

    其他

    //做這樣的東西寫在2

所有這一切都可以肯定地以不同的方式進行,但是這是一個常見的形式給出該列表的問題... (嗯,不知何故代碼的4個前導空格不工作?)

0

得到它的工作,只需要弄清楚如何使的printList和空閒列表功能

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

struct listNode{ 
    int data; //ordered field 
    struct listNode *next; 
}; 

//prototypes 
void insertNode(struct listNode *Head, int x); 
int printList(struct listNode *Head, int x); 
int freeList(struct listNode *Head, int x); 

//main 
int main(){ 
    struct listNode Head = {0, NULL}; 
    int x = 1; 
    printf("This program will create an odered linked list of numbers greater" 
    " than 0 until the user inputs 0 or a negative number.\n"); 
    while (x > 0){ 
    printf("Please input a value to store into the list.\n"); 
    scanf("%d", &x); 
      insertNode(&Head, x); 
    } 
    printf("Program terminated.\n"); 
    system("PAUSE"); 
    } 
void insertNode(struct listNode * Head, int x){ 
    struct listNode *newNode, *current; 
    newNode = malloc(sizeof(struct listNode)); 
    newNode->data = x; 
    newNode->next = NULL; 
    current = Head; 
    while (current->next != NULL && current->data < x) 
    { 
     current = current->next; 
     } 

     if(current->next == NULL){ 
      current->next = newNode; 
     } 
     else{ 
      newNode->next = current->next; 
      current->next = newNode; 
     } 
}