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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:25:20+00:00 2026-05-31T22:25:20+00:00

I am trying to write a code for a linked list but when I

  • 0

I am trying to write a code for a linked list but when I try to add a node the code gets stuck at the line immediately after the while loop in the add() function, reporting an access violation error. What’s wrong?

#include<iostream>

template <class T>
class linkedlist
{
    struct node
    {
        T data;
        node *lp;
    }*p;
public:
    linkedlist();
    void add(T t);
};

template<class T>
void linkedlist<T>::add(T t)
{
    node *r,*q;
    r = q = p;

    while(p!= NULL)
    {
        q = p;
        p = p->lp;
    }

    q->lp = new node;
    q->lp->data = t;
    p = r;
}

template<class T>
linkedlist<T>::linkedlist()
{
    p = NULL;
}

int main()
{
    linkedlist<int> l1;
    l1.add(3);
}
  • 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-31T22:25:21+00:00Added an answer on May 31, 2026 at 10:25 pm

    You initialize p to NULL in the constructor, then try to dereference it in add (via q):

    r = q = p;
    // the while loop will not be executed as p == NULL
    q->lp = new node;
    

    You must initialize p first – either during construction (in which case your “empty” list won’t be physically empty, so you must deal with this specifically, e.g. when iterating / removing elements), or you should check in add for the case when p == null and handle it differently.

    Side note: p is supposed to point to your head element, so it is risky to use it also to iterate through the list in add, then restore its original value (stored in r). Why not simply leave it always intact, and use r for iterating? One less chance for bugs.

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

Sidebar

Related Questions

I m trying write code that after reset set up rrpmax as 3000. It
I'm very new to pointers and linked lists but I'm trying to write a
I am trying to write a code for a custom hashmap (array of linked
I wrote the following code when trying to make a doubly-linked list with an
I am trying to write a linked list program in C, however I keep
I'm trying to write code for an orthogonal linked sparse matrix. Here's the question:
I'm trying to write code in my controller that when run, will create a
I am trying to write code for getting my cords on the mobile. I
I'm trying to write code that will load an image from a resource, and
I've been trying to write code that loads a .png file, attaches hotspot information,

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.