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 8566445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:39:22+00:00 2026-06-11T17:39:22+00:00

The requirement is an input function that returns a Node* to the root of

  • 0

The requirement is an input function that returns a Node* to the root of a linked list that input creates from the user’s input.
Like:

Node *input()
{
     Node *root; //not yet allocated
     while(1)
     {    
           //take in numbers from the user and correspondingly add them 
           //to the linked list and quit when anything else is entered

           if(input character is a number)
           {
                //allocate memory to a fresh pointer and push the number to it
                //and add this new Node to the LL
           }
     }
     return root;
}

-> There is the solution of pre-allocating memory to root and pushing the 1st number to it before the while loop body. But here, if the user won’t enter anything, that must deleted right away.

-> Also, there is a possible way of checking if root is NULL in the while loop, and if it is, allocating memory , so that it happens only once.

But I would like to know if there is a solution that will eliminate that odd situation arising from the root of the LL.

-> Maybe I could keep a dummy node at the 1st that doesn’t hold any values.

But apart from that?
If there is a better method of doing the whole thing, please suggest it.

  • 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-11T17:39:24+00:00Added an answer on June 11, 2026 at 5:39 pm

    You can do something like:

    struct Node* root = 0; // don't forget to initialize
    
    while (...) {
      if (...) {
        struct Node * item = ... ;  // allocate
        item->data = ...;
        item->next = root;
        root = item;
      }
    }
    
    return root;
    

    And you’re done, no special case required for the first element. The end of list is indicated by a null next pointer.

    If you want the list to be in insertion order, without traversing the whole list each time, you could:

    struct Node* root = 0; // don't forget to initialize
    struct Node* tail = 0;
    
    while (...) {
      if (...) {
        struct Node * item = ... ;  // allocate
        item->data = ...;
        item->next = 0;
    
        if (tail)
          tail->next = item;
        tail = item;
    
        if (!root)
          root = item;
      }
    }
    
    return root;
    

    No special-case allocation, but special-case assignments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Requirement is like: I want to take input values from user in frame but
I have a function that returns me only the fridays from a range of
I have the following requirement: Based on some user input, I need to generate
I have a function with many input parameters, and I need a function that
I am using JQuery with JSF+RF3.3 My requirement is that when a user clicks
My java/groovy program receives table names and table fields from the user input, it
Requirement : Should update MySQL database with that of MS Sql Server updates which
I would like to convert a string input representing numbers to the respective numeric
I got a strange requirement: function TextLimit(elem, maxChars) { ... } Where elem is
Consider the need for a function in C# that will test whether a string

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.