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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:39:32+00:00 2026-05-25T21:39:32+00:00

I am writing a linked list template class in C++ as an exercise for

  • 0

I am writing a linked list template class in C++ as an exercise for myself to help me get back into C++ programming. I’ve got the following class definition:

template <typename T>
class List
{
public:
    List();
    ~List();

    void append(T data);
    void display();
    int  count();

private:
    struct Node
    {
        T data;
        Node *next;
    } *head;
};

I have two versions of the append method – one that works and one that doesn’t. I can’t figure out what the difference, in terms of the operations performed, is, and why the second one doesn’t work. Here’s the one that works:

template <typename T>
void List<T>::append(T data)
{
    if (head == NULL)
    {
        head = new Node;
        head->data = data;
        head->next = NULL;
    }
    else
    {
        Node *p = head, *q;
        while (p != NULL)
        {
            q = p;
            p = p->next;
        }

        p = new Node;
        p->data = data;
        p->next = NULL;
        q->next = p;
    }
}

And here’s the one that doesn’t seem to actually add any elements to the list:

template <typename T>
void List<T>::append(T data)
{
    Node *p = head, *q = head;

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

    p = new Node;
    p->data = data;
    p->next = NULL;
    if (q != NULL)
    {
        q->next = p;
    }
}

Any ideas as to why the second version doesn’t add any elements? I’ve been trying it with type T as int.

P.S. Neither version gives any errors or warnings during compilation, nor during runtime.

  • 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-25T21:39:32+00:00Added an answer on May 25, 2026 at 9:39 pm

    The second method only handles the case where the list is non-empty.

    When the list is empty, the line q->next = p; is never reached, so the new element is leaked with no pointer existing to it after p goes out of scope.

    What you want, if you would like to eliminate the special case for empty list, is a Node **, like thus:

    template <typename T>
    void List<T>::append(T data)
    {
        Node** q = &head; /* head acts as the first Node::next link */
    
        /* invariant: q points to some Node::next field (or head, which acts like one) */
        while (*q)
            q = &(*q)->next;
    
        /* invariant: q points to the Node::next field at the end of the chain, which is currently NULL */
        *q = new Node { data, nullptr };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am writing a linked list data type. I have an inner class
For a CS class I am writing a linked list implementation of a linked
I'm writing an immutable linked list class in C, but one method is mysteriously
I'm writing a simple linked list based memory manager in the form: ...Header|Block|Header|Block... with
I am writing a class that is linked to an external resource. One of
I was writing two implementations of a Linked List for an assignment, a Doubly
I'm writing a simple linear linked list implementation in PHP. This is basically just
I'm thinking of writing few blogs on arrays and linked list and for that
Visual Studio 2008 - compiling in C I am writing a linked list application
I'm writing a function in C that takes in a linked list and a

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.