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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:36:43+00:00 2026-06-17T21:36:43+00:00

I am trying to teach myself linked-lists with node structs and was hoping someone

  • 0

I am trying to teach myself linked-lists with node structs and was hoping someone could help me with this. I would take input from the command line and it would make me a nested list and I could output it.

Example:

Input: “1 2 3 4 5”
Output:”1 2 3 4 5″

There are two things I am having trouble with:
1) When I run the program I keep getting warning: ‘typedef’ was ignored in this declaration [enabled by default]
How can I get rid of this?

EDIT: I have changed this to typedef struct Node* NodePtr;

2) My code is not working properly. How can I fix this? I am trying to teach myself linked lists in C++.

typedef struct Node;
typedef Node* NodePtr;
struct Node{
    int x;
    NodePtr next;
};

int main ()
{
    int n;
    NodePtr head, ptr = NULL;
    head = ptr;
    while (cin >> n){
        ptr = new Node;
        ptr->x = n;
        ptr->next = NULL;
        ptr = ptr->next;
    }

    NodePtr bling = head;
    while(bling != NULL){
        cout << bling->x << endl;
        bling = bling->next;
    }
    return 0;
}

Ideally what I want to do is to make a linked-list like the following.

1 -> 2 -> 3 -> NULL.
  • 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-17T21:36:44+00:00Added an answer on June 17, 2026 at 9:36 pm

    First, regarding the declaration of your structure and the pointer typedef you seem to want, there are a number of ways of doing this. The following will work in C or C++.

    // declare NodePtr as a pointer to Node, currently an incomplete type
    //  C and C++ both allow you to declare a pointer to damn-near anything
    //  so long as there is an understanding of what it *will* be, in this
    //  case, a structure called Node.
    typedef struct Node *NodePtr;
    
    // Now declare the structure type itself
    struct Node
    {
        int x;
        NodePtr next;
    };
    

    That said, I honestly do not recommend doing this. Most engineers want a clear and syntax-visible definition that screams to them, “THIS IS A POINTER!” You may be different. I, personally would simply prefer this:

    struct Node
    {
        int x;
        struct Node *next; // omit the 'struct' for C++-only usage
    };
    

    So long as you, and equally important, other engineers reading your code, understand your usage of NodePtr as a pointer-to-node, then go with what works best in your situation. Pointer type declaration is near-religious to some, so just keep that in mind. Some prefer seeing those asterisks (I being one), some may not (sounds like you =P).

    Note: there is one place that using a typedefed pointer-type can be beneficial in avoiding potential errors: multiple variable declarations. Consider this:

    Node* a, b;     // declares one Node* (a), and one Node (b)
    

    Having a typedef struct Node *NodePtr; allows this:

    NodePtr a, b;   // declares two Node*; both (a) and (b)
    

    If you spend enough time writing code in C the former of these will come back to bite you enough times you learn to not make that mistake, but it can still happen once in awhile.


    The Load Loop

    Regarding the load-loop for piecing together your list, you’re not wiring up your list correctly, and frankly there are a million ways to do it, one being the one below. This does not require you to clean out “an extra node”. Nor does it require any if (head){} else{} block structure to avoid said-same condition. Consider what we’re really trying to do: create nodes and assign their addresses to the right pointers:

    NodePtr head = NULL;     // always the head of the list.
    NodePtr* ptr = &head;    // will always point to the next pointer to assign.
    int n;
    while (cin >> n)
    {
        *ptr = new Node;
        (*ptr)->x = n;
        ptr = &(*ptr)->next;
    }
    
    // note this always terminates the load with a NULL tail.
    (*ptr)->next = NULL;
    

    How It Works

    1. Initialize the head pointer to NULL
    2. Initializer a Node pointer-pointer (yes a pointer to a pointer) to point to the head pointer. This pointer-to-pointer will always hold the address of the target pointer that is to receive the address of the next dynamic-allocated node. Initially, that will be the head pointer. In the above code, this pointer-to-pointer is the variable: ptr.
    3. Begin the while-loop. For each value read, allocate a new node, saving it in the pointer that is pointed-to by ptr (thus the *ptr). On the first iteration this holds the address of the head pointer, so the head variable will get our new node allocation. On all subsequent iterations, it contains the address of the next pointer of the last node inserted. Incidentally, saving the address of this new target pointer is the last thing that is done in the loop before we move to the next allocation cycle.
    4. Once the loop is complete, the last node inserted needs to have its next pointer set to NULL to ensure a properly terminated linked list. This is mandatory. We conveniently have a pointer to that pointer (the same one we’ve been using all this time), and thus we set the pointer it “points to” to NULL. Our list is terminated and our load is complete. Brain Food: What pointer will it be pointing to if the load loop never loaded any nodes? Answer: &head, which is exactly what we want (a NULL head pointer) if our list is empty.

    Design

    I hope this will help better explain how it works through three full iterations of the loop.

    Initial configuration

          head ===> NULL;
    ptr --^
    

    After one iteration:

    head ===> node(1)
              next
    ptr ------^
    

    After two iterations

    head ===> node(1)
              next ===> node(2)
                        next
    ptr ----------------^
    

    After three iterations

    head ===> node(1)
              next ===> node(2)
                        next ===> node(3)
                                  next
    ptr --------------------------^
    

    If we stopped at three iterations, the final termination assignment (*ptr = NULL;), gives:

    head ===> node(1)
              next ===> node(2)
                        next ===> node(3)
                                  next ===> NULL;
    ptr --------------------------^
    

    Notice that head never changes once the first iteration is finished (it always points to the first node). Also notice that ptr always holds the address of the next pointer that is to be populated, which after the initial iteration (where it started as the address of our head pointer), will always be the address of the next pointer in the last node added.

    I hope that gives you some ideas. It is worth noting that pairing these two pointers (the head pointer and the ptr pointer) into their own structure and having the appropriate management functions defines the textbook Queue; where one end is only for insertions (ptr) one is for extractions (head) and the container does not allow random access. There isn’t much need for such a thing these days with the standard library container adapters like std::queue<>, but it does provide an interesting adventure into a good use of pointer-to-pointer concepts.


    Complete Working Sample

    This sample just loads our queue with 20 elements, prints them, then cleans out the queue and exits. Adapt to your usage as needed (hint: like change the source of the incoming data perhaps)

    #include <iostream>
    using namespace std;
    
    // declare NodePtr as a pointer to Node, currently an incomplete type
    //  C and C++ both allow you to declare a pointer to damn-near anything
    //  so long as there is an understanding of what it *will* be, in this
    //  case, a structure called Node.
    typedef struct Node *NodePtr;
    
    // Now declare the structure type itself
    struct Node
    {
        int x;
        NodePtr next;
    };
    
    int main()
    {
        // load our list with 20 elements
        NodePtr head = NULL;
        NodePtr* ptr = &head;
        for (int n=1;n<=20;++n)
        {
            *ptr = new Node;
            (*ptr)->x = n;
            ptr = &(*ptr)->next;
        }
    
        // terminate the list.
        *ptr = NULL;
    
        // walk the list, printing each element
        NodePtr p = head;
        while (p)
        {
            cout << p->x << ' ';
            p = p->next;
        }
        cout << endl;
    
        // free the list
        while (head)
        {
            NodePtr victim = head;
            head = head->next;
            delete victim;
        }
    
        return 0;
    }
    

    Output

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to teach myself a bit of Javascript and made this collection
I have been trying to teach myself MEF, starting with this tutorial: http://blogs.msdn.com/b/brada/archive/2008/09/29/simple-introduction-to-composite-applications-with-the-managed-extensions-framework.aspx There
Trying to teach myself C++ (I normally use Python) and wrote this code. #include
I'm just trying to teach myself how to use Linq. This is what I
i am trying to teach myself SQL and of course I would like to
I'm are trying to teach myself to be better at programming. Part of this
I'm trying to teach myself .Net MVC 3, and am following this tutorial: http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3
I am trying to teach myself Smalltalk. A tutorial has this example of a
I'm trying to teach myself C by coding up a linked list. I'm new
I am trying to teach myself jquery while reading someone else's code, and I

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.