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 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

What does public static void mean in Java? I'm in the process of learning.
I'm working on one of my first projects that will span more than one
Im in the process of learning asp.net 3.5, currently on webparts and co. Ive
I'm in the process of learning Erlang. As an exercise I picked up the
I am in the process of learning JavaFX and I am looking for a
I'm in the process of learning WPF coming from WinForms development. I have a
I'm in the process of learning F# and am enjoying it so far. Almost
I'm very early in the iPhone development learning process. I'm trying to get my
My primary language right now is D, and I'm in the process of learning
There is a conversion process that is needed when migrating Visual Studio 2005 web

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.