打印有序链表 [英] Print an ordered linked list

查看:16
本文介绍了打印有序链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Just did some editing on it, i tried what you said but it didnt work, so i tried something i am a little more familiar with but it doesnt seem to work correctly. It prints the information weirdly then crashes.. For exmaple: When i imput 9-8-7-6-5-4-3-2-1 then 0 to print, it prints back to me 0-0-0-9-1-2-3-4-5-6-7-8 and then crashes? when i imput 1-2-3-4-5-6-7-8-9 then 0 to print, it prints back to me 0-0-0-1-2-3-4-5-6-7-8-9 and then crashes.

#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.
");
     while (x > 0){
           printf("Please input a value to store into the list.
");
           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 
", *current);
          current = current->next;
    }
}

推荐答案

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

你很接近.

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

Take a look at the condition on your while loop - the reason your program crashes is that 'Head' is never updated, so the condition is always true. So the program just keeps setting 'current' equal to 'current->next' without stopping until you get to the end of your list, at which point 'current->next' is NULL and the program crashes.

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

If you change your while loop to check whether 'current' is NULL instead of 'Head', it will stop when it reaches the end of the list and your program won't crash.

添加一些关于修复显示链表的额外零的指针.

Adding some pointers on fixing the extra zeroes showing up the linked list.

struct listNode Head = {0, NULL};

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

At the beginning of your program, you're creating a node in your linked list with the value 0. So you've always got at least one 0 no matter what your input is. You might consider initializing Head to NULL instead. If you do that, you'll have to check for that condition in your insertNode function.

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

You're also getting some extra zeroes because you're checking your loop condition ('while(x > 0)') before you get the input that you use to make that decision ('scanf("%d", &x);'). You might want to consider changing that order by using a 'do...while' instead of 'while'. Take a look at http://www.cprogramming.com/tutorial/c/lesson3.html for an explanation of 'do...while' with examples.

这篇关于打印有序链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆