2012-11-05 104 views
1

只做了一些編輯,我試着你說的但它沒有工作,所以我嘗試了一些我更熟悉一些,但它似乎沒有正常工作。它打印信息奇怪然後崩潰..例如: 當我輸入9-8-7-6-5-4-3-2-1然後0打印,它打印回給我0-0-0-9 -1-2-3-4-5-6-7-8然後崩潰? 當我輸入1-2-3-4-5-6-7-8-9然後0打印,它打印回給我0-0-0-1-2-3-4-5-6-7 -8-9然後崩潰。打印一個有序的鏈接列表

#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 freeList(struct listNode *Head, int x); 

//main 
int main(){ 
    struct listNode Head = {0, NULL}; 
    int x = 1; 
    int ret = 0; 
    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); 
    } 
    ret = printList(&Head); 
    } 
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; 
     } 
} 
int printList(struct listNode * Head){ 
    struct listNode *current = Head; 
    while (Head != NULL){ 
      printf("%d \n", *current); 
      current = current->next; 
    } 
} 

回答

0

我建議創建開始於第一個節點,並進入到下一個節點迭代,直到下一個節點爲null,並且使用像下一個建議,而不是最終名單(或旁邊)。

然後打印你簡單的繼續通過迭代器並打印出值。 插入你開始頭項目和迭代器通過並比較值。

增加了一些僞代碼,因爲我不是一個真正的C++程序員。

class iterator 
{ 
    //provide a construction method for this 
    listNode current = Head; 
    listNode getValue() 
    { 
     return current; 
    } 

    void next() 
    { 
     //probably want to include some checks for validity here 
     current = current->next; 
    } 

    boolean hasNext() 
    { 
     return current->next != null; 
    } 
} 
+0

你能給我一個例子,說明我是如何從第一個節點開始的,打印出來並轉到下一個節點?我知道如何通過數組來做這種事情,但不通過結構體/鏈接列表 – user1801067

+0

您只需保存對當前節點的引用,然後移動到下一個項目就是使currentNode = currentNode-> next。還提供一個檢查,看下一個!= null(下一個) –

+0

是否不會將當前節點放在列表中最近放置的位置?我將如何通過列表進入後退字詞。 – user1801067

0
int printList(struct listNode * Head){ 
struct listNode *current = Head; 
while (Head != NULL){ 
     printf("%d \n", *current); 
     current = current->next; 
} 

你是八九不離十。

看看你的while循環的條件 - 你的程序崩潰的原因是'Head'從不更新,所以條件總是如此。因此,程序只是將「當前」設置爲等於「當前 - >下一個」,而不會停止,直到您到達列表的末尾,此時「當前 - >下一個」爲NULL並且程序崩潰。

如果您更改while循環以檢查'current'是否爲NULL而不是'Head',它將在到達列表末尾時停止,並且程序不會崩潰。

編輯:添加一些指針修復顯示鏈表的額外零。

struct listNode Head = {0, NULL}; 

在你的程序的開始,你創建你的鏈接列表的節點,其中值0。所以,你總是有至少一個0不管你輸入的是什麼。您可以考慮將Head初始化爲NULL。如果你這樣做,你必須在insertNode函數中檢查這個條件。你得到一些額外的零,因爲你正在檢查你的循環條件('while(x> 0)')之前你得到你用來作出決定的輸入('scanf(「%0) d「,& x);')。您可能需要考慮通過使用'do ... while'而不是'while'來更改該順序。看看http://www.cprogramming.com/tutorial/c/lesson3.html以獲取「do ... while」的例子的解釋。

+0

這解決了崩潰的問題。我如何解決它以一種時髦的方式訂購它的問題? – user1801067

+0

我添加了一些建議來修復你所看到的額外的零。 –