簡體   English   中英

訂購鏈表打印

[英]Ordered linked list printing

我正在做我的家庭作業,這就是我已經得到的。 我現在需要知道如何打印出已列入列表的信息。 我需要重新配置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;
        }
}

啟動時, 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;
 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;
}

你的impl有三個問題:

  1. 你應該在最后添加新的數字,所以你應該有兩個指向列表的指針:一個指向頭部,另一個指向尾部(你也可以在列表頂部添加,然后你只需要一個指針,但那么列表是反向排序的)

  2. 指針鏈代碼是錯誤的,我想你想嘗試在頭節點之后插入新元素,所以你的代碼應該寫成:

    電流=報頭 - >下; //節點Header-> next當前指向

    報頭 - >下一= newNode; //讓header-> next指向新的ele

    newNode->下一=電流; //讓新的ele下一個指向舊的頂級元素

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

    if(header == 0)

      header=newNode 

    其他

    //做2寫的東西。

所有這一切都可以用不同的方式完成,但這是該列表問題的常見方法......(嗯,不知何故,代碼的4個前導空白不起作用?)

讓它工作,只需要弄清楚如何制作printList和freeList函數

#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;
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM