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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:30:29+00:00 2026-06-05T02:30:29+00:00

I’m trying to implement a linked list class in C++ and I got problem.

  • 0

I’m trying to implement a linked list class in C++ and I got problem. I have the += operator who adds new node.

the linked list class interface:

template <typename Type>

class LinkedList {
public:
    LinkedList<Type>* head;
//  linked list stracture
    Type data;
    LinkedList<Type>* next;
//  others ....
    size_t length;
public:
    LinkedList();
    ~LinkedList();
    void initializeHead(LinkedList<Type>* headPtr);
    size_t size() const;
    LinkedList& operator+=(const Type& add);
    void operator-=(const Type& remove);
    LinkedList<Type>& operator[] (const size_t index) const;
    bool operator== (const LinkedList<Type> &versus) const;
    friend ostream& operator<< (ostream& out,LinkedList& obj);
};

and here i have the += overload implement:

template <typename Type> LinkedList<Type>& LinkedList<Type>::operator +=(const Type& add) {
    // head ptr - :)
    LinkedList<Type>* p = head->next;
    // go to the end
    while(p) p = p->next;
    // now on end - create new..!!!
    try {
        p = new LinkedList<Type>;
    } catch (bad_alloc& e) {
        cout << "There\'s an allocation error....";
    } catch (...) {
        cout << "An unknown error.." << endl;
    }// fill and done
    p->data = add;
    p->next = NULL;
    // increment length .........
    ++head->length;
    // done ............
    return *p;
}

Additionally , I have “array” access overload method:

template <typename Type> LinkedList<Type>& LinkedList<Type>::operator [](const size_t index) const {
    if(index < 0 || index >= length) // invaild argument
        throw  exception();
    // continue
    LinkedList<Type>* p = head;
    for(size_t i = 0; i < index; ++i) p = p->next; // we are at what we want
    return *p;
}

All works correctly – I checked on the dibugger,

the problem is – += doesn’t save the new node in “head->next”, for some reason, after finish += method, head->next equal to null.

Do someone know why the new allocation don’t link to head->next?

Thanks a lot!!

  • 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-05T02:30:31+00:00Added an answer on June 5, 2026 at 2:30 am

    after while(p) p = p->next; p is NULL

    and next you do p = new LinkedList<Type>; but you don’t link the p into the head.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping 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.