【问题标题】:Print an ordered linked list打印有序链表
【发布时间】:2012-10-25 18:35:01
【问题描述】:

刚刚对它进行了一些编辑,我尝试了你所说的但它没有工作,所以我尝试了一些我更熟悉的东西,但它似乎无法正常工作。它奇怪地打印信息然后崩溃..例如: 当我输入 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;
    }
}

【问题讨论】:

    标签: list linked-list sorted


    【解决方案1】:

    我建议创建一个迭代器,从第一个节点开始并转到下一个节点,直到下一个节点为空,并建议使用类似下一个而不是列表结尾(或有下一个)。

    然后打印你简单地继续遍历迭代器并打印出值。 要插入您从头项和迭代器开始并比较值。

    添加了一些伪代码,因为我不是真正的 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;
        }
    }
    

    【讨论】:

    • 你能给我一个例子,说明我如何从第一个节点开始,打印它,然后转到下一个节点吗?我知道如何通过数组来做这种事情,但不是通过结构/链表
    • 您只需持有对当前节点的引用,然后移动到下一项,只需使 currentNode = currentNode->next。还提供检查是否下一个!= null(下一个)
    • 当前节点不会是列表中最新的吗?我将如何通过列表进行反词。
    • @user1801067 为迭代器提供了一个示例类。
    • 刚刚用我目前拥有的内容编辑了原始帖子。我有它部分工作,但它需要调整
    【解决方案2】:
    int printList(struct listNode * Head){
    struct listNode *current = Head;
    while (Head != NULL){
          printf("%d \n", *current);
          current = current->next;
    }
    

    你已经很接近了。

    看看你的while循环的条件——你的程序崩溃的原因是'Head'永远不会更新,所以条件总是正确的。所以程序只是保持设置 'current' 等于 'current->next' 而不会停止,直到你到达列表的末尾,此时 'current->next' 为 NULL 并且程序崩溃。

    如果您更改 while 循环以检查 'current' 是否为 NULL 而不是 'Head',它将在到达列表末尾时停止并且您的程序不会崩溃。

    编辑:添加一些关于修复显示链接列表的额外零的指针。

    struct listNode Head = {0, NULL};
    

    在您的程序开始时,您在链接列表中创建一个值为 0 的节点。因此,无论您输入什么,您总是至少得到一个 0。您可能会考虑将 Head 初始化为 NULL。如果这样做,则必须在 insertNode 函数中检查该条件。

    你还会得到一些额外的零,因为你正在检查你的循环条件 ('while(x > 0)') 你得到你用来做出那个决定的输入 (' scanf("%d", &x);')。您可能需要考虑通过使用“do...while”而不是“while”来更改该顺序。看看http://www.cprogramming.com/tutorial/c/lesson3.html 以了解“do...while”的示例。

    【讨论】:

    • 解决了崩溃的问题。我如何解决它以一种时髦的方式订购它的问题?
    • 我添加了一些建议来修复您看到的额外零。
    猜你喜欢
    • 1970-01-01
    • 2015-01-18
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    相关资源
    最近更新 更多