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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:43:33+00:00 2026-05-22T18:43:33+00:00

I am in process of learning c++. I am working on creating a linkedlist

  • 0

I am in process of learning c++. I am working on creating a linkedlist data structure. One of the functions that displays the values of nodes in the structure does not work. For some reason the while loop that traverses through nodes doesn’t work in the display function, hence I can’t see the values in those nodes. Does anyone see what the problem is? I’ve been staring at the code for a while and not sure what is wrong here.
Thanks for your help in advance.
Header:

// linklist.h
// class definitions


#ifndef LINKLIST_H
#define LINKLIST_H

class linklist
 {
 private:
     // structure containing a data part and link part
     struct node
     {
         int data;
         node *link;
     }*p;

 public:

     linklist();
     void append(int num);
     void addatbeg(int num);
     void addafter(int loc, int num);
     void display();
     int count();
     void del(int num);
     ~linklist();
 };

#endif

.cpp file

// LinkedListLecture.cpp
// Class LinkedList implementation

    #include"linklist.h"
    #include<iostream>

using namespace std;


     // initializes data member
     linklist::linklist()
     {
         p =NULL;
     }

     // adds a node at the end of a linked list
     void linklist::append(int num)
     {
          node *temp, *r;
         // if the list is empty, create first node
         if(p==NULL)
         {
                 temp = new node;
             temp->data = num;
             temp->link = NULL;      
         }
         else
         {
             // go to last node
             temp = p;
             while(temp->link!=NULL)
                 temp =  temp->link;
             // add node at the end
             r = new node;
             r->data=num;
             r->link=NULL;
             temp->link=r;
         }
     }



 // displays the contents of the linked list
 void linklist::display()
 {
     node *temp = p;
     cout<< endl;
     // traverse the entire linked list
     while(temp!=NULL) // DEBUG: the loop doesn't work
     {
         cout<<temp->data<<" ";
         temp = temp->link;
     }

     void main() 
 {
     linklist l;

     l.append(14);
     l.append(30);
     l.append(25);
     l.append(42);
     l.append(17);
     cout<<"Elements in the linked list:";
     l.display(); // this function doesn't work
     system("PAUSE");
 }
  • 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-22T18:43:34+00:00Added an answer on May 22, 2026 at 6:43 pm

    You never set p to a non NULL value.

        if(p==NULL)
         {
             p = new node;
             p->data = num;
             p->link = NULL;      
         }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On the process of learning Assembly i got one question If i do the
I'm in the process of learning C++. But there's so much more that I
So, I'm in the process of learning node, and am finding that for testing,
After 3 days of working on one 'Dragable Windows Interface' i discovered that the
I am in the process of learning MySQL and querying, and right now working
I'm currently working on a project that includes Gaussian Processes for Machine Learning. Considering
I am working on building a lightweight MVC, mainly for the learning process but
Just learning C++, encountered an issue that i'm not really sure why the following
So I'm in the process of learning PHP and am working on a login
Im the process of learning C++. I got stuck debugging some of my code

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.