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

The Archive Base Latest Questions

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

So I am trying to program in C++ a linked list but I’m not

  • 0

So I am trying to program in C++ a linked list but I’m not sure how to do it exactly. I understand the concept more or less and I saw a tutorial that gave me this code but it didn’t explain it too well. I was wondering if you all could help me with the next steps, explain to me what I just did, and explain to me how to continue. I want it to add elements, pop or push elements in a stacked manner, last in first out, and free elements at the end and free up the memory to prevent memory leaks. thank you.

    #include <iostream>
using namespace std;

struct node
{
    int num;
    node *link;
}*p;

void main()
{
    node *root;

    root = new node;
    root->num=5;
    root->link = p;
    p = root;

    node *q;

    for(q = p; q != NULL; q = q->link)
    {
        cout<<q->num;
        cout<<endl;
    }
}
  • 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-17T16:33:44+00:00Added an answer on June 17, 2026 at 4:33 pm

    First of all, since you already use the standard library (#include <iostream>), be informed that the standard library already have lists: (#include <list>) as well as other containers.

    You can find their documentation here

    In case you want to write yourself, use a proper approach: don’t put everything into main, but define a proper class and methods to manipulate the elements. You can refer to the standard documentation I linked as a “inspiration”.

    You will probably end-up with something like

    class list
    {
        struct element
        {
            element* pnext;
            int value;
        };
    
        element* root;
    
    public:
    
        list() :root() 
        {}
    
        ~list() 
        { while(root) pop(); }
    
        list(const list&) =delete;
    
        list& operator=(const list&) =delete;
    
        void push(int val) 
        { 
            element* p = new element;
            p->value = val;
            p->pnext = root;
            root = p;
        }
    
        void pop()
        {
            element* pe = root;
            root = pe->pnext;
            delete pe;
        }
    
        class index
        {
            element* p;
            index(element* s) :p(s) {}
            friend class list;
        };
    
        index start() const const 
        { return index(root); }
    
        index next(index i) const 
        { return index(i.p->pnext); }
    
        int at(index i) const 
        { return i.p->value; }
    
        bool is_end(index i) const 
        { return i.p == nullptr; }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying simple linked list Collection program, but it is not giving me
Im trying to create a Linked List in C but the program crashed due
I am trying to write a program to serialize a linked list to a
This is a program trying to make a linked list. #include <iostream> using namespace
I am trying to write a linked list program in C, however I keep
I'm trying to understand the Linux kernel linked list API. According to Linux Kernel
I'm trying to program a graph class using an adjacent list from an example
I'm trying to program an Android interface which uses an expandable list on the
I've been writing a program trying to find itself using the procps library. But
I've got a program that populates a STL linked list with structs, and I'm

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.