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

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

问题描述

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

推荐答案

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

您非常接近.

看看while循环上的条件-程序崩溃的原因是'Head'从未更新,因此条件始终为true.因此,程序只是将'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天全站免登陆