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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:19:54+00:00 2026-05-18T07:19:54+00:00

I’m writing a linked list container for my homework. Using Qt 4.7 and gcc

  • 0

I’m writing a linked list container for my homework. Using Qt 4.7 and gcc 4.4, I’ve found some problems in the code that I guess they are related to memory management or garbage collection.

After using the << operator to display the list, all data of list is changed. for example, after construction and setting a list like l,

std::cout<<l<<std::endl;
std::cout<<l<<std::endl;

prints:

Data = [-10, 3, 2, 8, 1, -1, -2, ] // this is correct
Data = [0, 149560240, 149560192, 149558336, 149560256, 149558320, 149560208, ]

My linked list is:

#ifndef LINKEDLIST1_H_
#define LINKEDLIST1_H_

#include <iostream>

template<class T> class LinkedList1;
template<class T> class Node;

template<class T>
class Node
{
   friend class LinkedList1<T> ;
public:
   Node(const T& value) :
         Data(value), Next(NULL)
   {
   }
   Node() :
         Next(NULL)
   {
   }
   T Data;
   Node* Next;
};

template<class T>
class LinkedList1
{
public:
   LinkedList1() :
         size(-1), first(NULL)
   {
   }
   ~LinkedList1()
   {
      Node<T>* i = this->first;
      Node<T>* j = this->first;
      while(j!=NULL)
      {
         j=i->Next;
         delete i;
         i=j;
      }
   }
   // Operations on LinkedList
   Node<T>* First()
   {
      return first;
   }

   int Size()
   {
      return size + 1;
   }

   int Count()
   {
      int size = 0;
      Node<T>* current = this->firstFirst();
      while(current != NULL)
      {
         size++;
         current = current->Next;
      }
      this->size = size;
      return this->Size();
   }

   bool IsEmpty()
   {
      return this->Size() == 0;
   }

   void Prepend(Node<T>* value) //O(1)
   {
      value->Next = this->first;
      this->first = value;
      this->size++;
   }
   void Prepend(const T& value) //O(1)
   {
      Node<T>* item = new Node<T>(value);
      item->Next = this->first;
      this->first = item;
      this->size++;
   }
   void Append(Node<T>* value)
   {
      if(this->IsEmpty())
      {
         this->first = value;
         this->size++;
      }
      else
      {
         Node<T>* current = this->First();
         while(current->Next != NULL)
            current = current->Next;
         current->Next = value;
         value->Next = NULL;
         this->size++;
      }
   }
   void Append(const T& value)
   {
      Node<T>* temp= new Node<T>(value);
      this->Append(temp);
   }
   void Insert(Node<T>* location, Node<T>* value) //O(n)
   {
      Node<T>* current = this->first;
      Node<T>* before = current;
      while(current != NULL)
      {
         before = current;
         current = current->Next;
         if(current == location)
         {
            before->Next = value;
            value->Next = current;
            this->size++;
            break;
         }
      }
   }
   void Insert(Node<T>* location, const T& value)
   {
      Node<T>* temp = new Node<T>(value);
      this->Insert(location,temp);
   }

   Node<T>* Pop()
   {
      if(this->IsEmpty())
         return NULL;
      else
      {
         Node<T>* current = this->first;
         Node<T>* before = current;
         while(current->Next != NULL)
         {
            before = current;
            current = current->Next;
            before->Next = current;
         }
         before->Next = NULL;
         this->size--;
         return current;
      }
   }
   Node<T>* PopF()
   {
      if(!this->IsEmpty())
      {
         Node<T>* head = this->first;
         this->first = this->first->Next;
         this->size--;
         return head;
      }
      else
         return NULL;
   }
   Node<T>* Remove(Node<T>* location)
   {
      // validating by IsEmpty is not necessary for this operation,
      // while statement's condition guarantees validation
      Node<T>* current = this->first;
      Node<T>* before = current;
      while(current != NULL)
      {
         before = current;
         current = current->Next;
         before->Next = current;
         if(current == location)
         {
            before->Next = current->Next;
            current->Next=NULL;
            return current;
         }
      }
      return NULL; // Not found...
   }
   void Inverse()
   {
      if(this->IsEmpty())
         return;
      else
      {
         Node<T>* r = NULL;
         Node<T>* q = this->first;
         Node<T>* p = this->first;
         while(q != NULL)
         {
            p = p->Next;
            q->Next = r;
            r = q;
            q = p;
         }
         this->first = r;
      }
   }
   // Ordered insertion. implement this: foreach i,j in this; if i=vale: i+=vale, break; else if i<=value<=j: this.insert(j,value),break
   friend std::ostream& operator<<(std::ostream& out, const LinkedList1 item)
   {
      out<<"Data = [";
      Node<T>* current = item.first;
      while(current != NULL)
      {
         out << current->Data << ", ";
         current = current->Next;
      }
      out<<"]";
      return out;
   }

   void HelperOutput(std::ostream& out, const LinkedList1 item) const
   {
      out<<item;
   }

   Node<T>* operator[](const int& index)
   {
      int i=0;
      Node<T>* current = this->first;
      while(i<=index && current!=NULL)
      {
         if(i=index)
            return current;
         else
         {
            i++;
            current=current->Next;
         }
      }
   }
public:
   int size;
   Node<T>* first;

};

#endif /* LINKEDLIST1_H_ */

   friend std::ostream& operator<<(std::ostream& out, const LinkedList1 item)
   {
      out<<"Data = [";
      Node<T>* current = item.first;
      while(current != NULL)
      {
         out << current->Data << ", ";
         current = current->Next;
      }
      out<<"]";
      return out;
   }

first item in output of second call is always 0. So I Thinked I’ve set first to NULL in somewhere in code; but I checked out all of methods and there was nothing like that. In addition, printed value is not the pointer itself; it’s pointer’s Data. Somewhere somebody changes Datas of all Node<T>*s in my list.

Is this a memory management or garbage collection issue? if not, what I’m doing wrong?

  • 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-18T07:19:55+00:00Added an answer on May 18, 2026 at 7:19 am

    One thing that stands out here is that your signature is:

    std::ostream& operator<<(std::ostream& out, const LinkedList1 item)
    

    Instead of:

    std::ostream& operator<<(std::ostream& out, const LinkedList1& item)
    

    So, you are invoking a copy of your LinkedList. I suspect the issue here is that you have not correctly implemented your copy and assignment operators for LinkedList1, such that the the copy references the original content, and the destructor is making the original object into garbage.

    I would recommend adding the following to your definition of LinkedList1:

    private:
        // The following declarations disallow copy and assignment. Do not implement.
        LinkedList1(const LinkedList1&);
        LinkedList1& operator=(const LinkedList1&);
    

    Adding the above will lead to linker errors for the original signature that you had. I would then recommend passing the linked list by const reference, and your problem should disappear.

    As an aside, I notice that many of your functions can be made const but aren’t. For example, you have int Size() instead of int Size()const. One should generally mark as const anything that can be so-marked. This is known as “const-correctness” and can avoid a large number of issues.

    Also, minor style nitpick: you have if…else statements where one has braces and the other doesn’t. I would strongly recommend that you use braces in all cases as this leads to more readable code.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
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
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.