Print an ordered linked list

空扰寡人 提交于 2019-12-02 08:57:32

问题


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

回答1:


I would suggest creating an iterator that starts at the first node and goes to the next node until the next node is null and suggest using a like next and not end of list (or has next).

Then to print you simple continue through the iterator and print out the value. To insert you start at the head item and iterator through and compare the values.

Added some pseudo code since I am not really a c++ programmer.

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



回答2:


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

You're pretty close.

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.

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.

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

struct listNode Head = {0, NULL};

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.

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.



来源:https://stackoverflow.com/questions/13240332/print-an-ordered-linked-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!