Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8787173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:49:13+00:00 2026-06-13T21:49:13+00:00

I am trying to create a linked list that will take the input from

  • 0

I am trying to create a linked list that will take the input from the user, order it, and print it out once the user inputs 0 or a negative number. Somewhere my code is adding a “0” to the begining of the print loop.
Example: I input 1-2-3-4-5. The program then returns 0-1-2-3-4-5.
Example2: I input 1-2-3-4-5. The program then returns 0-5-1-2-3-4. Which is also a problem for me because i eventually need to make the program order the inputed values from least to greatest. But for right now im focused on getting it to take the input 1-2-3-4-5 and print 1-2-3-4-5.

#include <stdio.h>
#include <stdlib.h>

struct listNode{
  int data;   
  struct listNode *next;
};

//prototypes
void insertNode(struct listNode *Head, int x);
void printList(struct listNode *Head);
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);
           if (x > 0){
           insertNode(&Head, x);
           }
     }
     printList(&Head);
     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;
        }
}
void printList(struct listNode * Head){
    struct listNode *current = Head;
    while (current != NULL){
          if(current > 0){
               printf("%d \n", *current);
          }
          current = current->next;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:49:15+00:00Added an answer on June 13, 2026 at 9:49 pm

    It has a zero in the list because you put it there:

    struct listNode Head = {0, NULL};
    

    If you want a quick fix, change the line in printList() and anything else that processes the list from:

    struct listNode *current = Head;
    

    to:

    struct listNode *current = Head->next;
    

    This will start at the second element of the list, ignoring the one you put there to start with.


    However, a better way is probably not to have that extraneous element at all:

    #include <stdio.h>
    #include <stdlib.h>
    
    struct listNode {
        int             data;
        struct listNode *next;
    };
    
    // Prototypes (freeList removed since not defined).
    
    void insertNode(struct listNode **pHead, int val);
    void printList(struct listNode *Head);
    
    // Main program for testing.
    
    int main(void) {
        // List initially empty.
    
        struct listNode *Head = NULL;
    
        int x = 1;
        puts("This program will create an ordered linked list");
        puts("    of numbers greater than 0 until the user");
        puts("    enters 0, a negative number, or a non-integer.");
        for(;;) {
              puts("Please input a value to store into the list.");
              if ((scanf("%d", &x) != 1) || (x <= 0)) break;
              insertNode(&Head, x);
         }
         printList(Head);
    }
    
    void insertNode(struct listNode **pHead, int val){
        struct listNode *newNode, *current, *previous;
    
        // Allocate new node, should really check for failure here.
    
        newNode = malloc (sizeof (struct listNode));
        newNode->data = val;
        newNode->next = NULL;
    
        // Handle inserting into empty list.
    
        if (*pHead == NULL) {
            *pHead = newNode;
            return;
        }
    
        // Find node to insert before.
    
        current = *pHead;
        while (current != NULL && current->data < val)  {
            previous = current;
            current = current->next;
        }
    
    
        // Handle inserting at start of list.
    
        if (current == *pHead) {
            newNode->next = *pHead;
            *pHead = newNode;
            return;
        }
    
        // Handle inserting at end of list.
    
        if (current == NULL) {
            previous->next = newNode;
            return;
        }
    
        // Handle inserting somewhere inside the list.
    
        newNode->next = current;
        previous->next = newNode;
    }
    
    void printList (struct listNode *Head) {
        struct listNode *current = Head;
    
        if (current == NULL) {
            puts ("There are no numbers.");
            return;
        }
    
        puts ("Numbers are:");
        while (current != NULL) {
            printf ("   %d\n", current->data);
            current = current->next;
        }
    }
    

    There’s a couple of other things I’ve cleaned up there as well, such as changing *current to the more explicit current->data, passing a pointer to the head so you can change it, and making a slight modification to the main input loop. Here’s a sample run:

    This program will create an ordered linked list
        of numbers greater than 0 until the user 
        inputs 0 or a negative number.
    Please input a value to store into the list.
    4
    Please input a value to store into the list.
    1
    Please input a value to store into the list.
    8
    Please input a value to store into the list.
    5
    Please input a value to store into the list.
    6
    Please input a value to store into the list.
    3
    Please input a value to store into the list.
    2
    Please input a value to store into the list.
    9
    Please input a value to store into the list.
    7
    Please input a value to store into the list.
    0
    Numbers are:
       1 
       2 
       3 
       4 
       5 
       6 
       7 
       8 
       9 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a singly linked list from an input text file for
I'm trying to create a method that will add a node to my linked
I'm getting an error when trying to create the linked list that says: Exception
Im trying to create a linked list in c. The twist is that I
I am trying to create a script that would list all the linked libraries
I'm trying to create a linked list that is sorted by both name and
Im trying to create linked-list insert function that takes a list (or more correctly
I am trying to create a linked list where you can enter items into
Hello I am trying to create a for loop that loops through the linked
I am trying to format a linked list so that it prints 5 nodes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.