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

  • Home
  • SEARCH
  • 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 4531720
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T13:56:21+00:00 2026-05-21T13:56:21+00:00

#include <iostream> using namespace std; struct Node { int item; // storage for the

  • 0
        #include <iostream>
    using namespace std;



    struct Node
    {
        int item;   // storage for the node's item
        Node* next;   // pointer to the next node 
    };

  Node* addNode(Node*& head, int data , int& count) 
{
    Node * q;     // new node
    q = new Node;  // allocate memory for the new mode
    q->item = data;  // inserting data for the new node
    q->next = head;   // point to previous node ?? how would i do that? ( am i doing it correctly?)
    count++; // keep track of number of node
    head = q;
    return q;
}



    int main()
    {
        int a, count=0;
        int data;
        bool repeat;
        Node *head= NULL;   
        //^^ assuming thats creating the first node ^^
        do
        {
        cout << "please enter the data for the next node" <<endl;
        cin >> data;
        addNode(head, data, count);
        cout << "do you wish to enter another node? (enter true or false)" << endl;
        cin >>repeat;
        }
       while (repeat == true);


       // assuming this is the print function  
          while(head != NULL)
        {
            cout << "output" << temp->item << endl;
            cout << temp->next << endl;
        }

        system("pause");
        return 0; 
    }

okey i tried adding a new element in the list how would i move the head around like a LIFO memory (stack) so the last element is on the very top..

any help would be appreciated ! The pointers and the nodes are messing with my brain lately ….

  • 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-05-21T13:56:22+00:00Added an answer on May 21, 2026 at 1:56 pm

    Since I’m not sure every answer completely answers it, here’s a linked list implementation (written without testig:

    // your (correct) structure
    struct Node
    {
        float item;   // storage for the node's item
        Node* next;   // pointer to the next node 
    };
    

    Now we need two pointers somewhere to look after the list:

    /* some pointers */
    struct List
    {
        Node* head;
        Node* tail;
    };
    

    Now we need to create some elements. As others have said, you can do that with new:

    /* create some elements we want to link in */
    Node* elem1 = new Node();
    Node* elem2 = new Node();
    Node* elem3 = new Node();
    /* maybe even set their properties! */
    elem1->item = 3.14;
    elem2->item = 3.14;
    elem3->item = 3.14;
    

    Notice how I didn’t try to add these elements to a list yet? That’s because I’ve got a function in mind which looks like this:

    void addtolist(List &list, Node* node)
    {
        /* if no head, initialise the list */
        if ( list->head == NULL )
        {
            list->head = node;
            list->tail = node;
        }
        else if ( list->head != NULL && list->tail != NULL )
        {
            /* access the tail element and set its 
               next to this ptr. 
               Move tail to this node */
            list->tail->next = node;
            list->tail = node;
        }
        else
        {
            /* probably raise an exception! */
        }
    }
    

    You can call this by doing this:

    List l;
    addtolist(l, elem1); /* etc */
    

    Deleting elements is somewhat more tricky, since you have to go to that element, remember its previous element, grab it’s next element, join them up and delete the Node* you’re on.

    Now for traversing lists… your terminology HEAD|TAIL reminds me of Erlang and tail recursion, where the current element is referred to as the head and the remainder the tail. If I write:

    Node* cur = l.head;
    while ( cur != NULL )
    {
        // do something with cur.item ? 
        cur = cur->next;
    }
    

    You can see this happening. Replacing cur with head here would be harmless thanks to the List struct.

    Finally, I’ve used a very C-like approach here, but there’s scope for templates:

    template<typename T>
    struct node
    {
        T item;   // storage for the node's item
        Node<T>* next;   // pointer to the next node 
    };
    

    and encapsulating the List struct as a class:

    template<typename T>
    class List
    {
    protected:
        Node<T>* head;
        Node<T>* tail;
    public:
    
        void addtolist(Node<T>* node);
        Node<T>* gethead();
        Node<T>* gettail();
    }
    

    Which brings you a little bit closer to std::list.

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

Sidebar

Related Questions

#include <iostream> using namespace std; struct Node { int item; // storage for the
#include <iostream> using namespace std; struct Node { char item; Node *next; }; void
#include <iostream> using namespace std; struct Node { char item; Node *next; }; void
#include <iostream> using namespace std; struct node { int v; node* next; node (int
#include <iostream> #include <vector> using namespace std; struct s_Astruct { vector <int> z; };
I have this C++ code: #include <iostream> using namespace std; struct MyItem { int
After writing the code below: #include <iostream> using namespace std; typedef struct Node {
#include <iostream> using namespace std; struct Term; struct Node; typedef Term* termPtr; typedef Node*
#include <iostream> using namespace std; int main() { struct list { string name; int
#include <iostream> #include <queue> using namespace std; struct Call { Call( int callNum, long

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.