Linked List Copy/Move semantic C++ - c++

so I have this code for linked list below. I need to create copy/move constructors and operators.
I am having troubles how to make it the right way.
I know the code isn't perfect, I will appreciate all tips, but I want to focus mainly on copy/move semantic.
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
class List {
class Node {
Node* next;
string text;
int index;
friend class List;
public:
Node(const string& value, int i) : next(nullptr), index(i), text(value) {}
friend ostream& operator<< (ostream& wy, const Node& wzl) {
if (wzl.next) return wy << wzl.text << ", " << *wzl.next;
else return wy << wzl.text;
}
};
Node* head;
int _size(Node* node, int size = 0) {
if (node == NULL) {
return size;
} else {
_size(node->next, size+1);
}
}
void _insert(Node* node, const string& value, int index) {
if (node->next == NULL || node->next->index > index) {
if (node->index == index) {
node->text = value;
} else {
Node* element = new Node(value, index);
element->next = node->next;
node->next = element;
}
} else {
_insert(node->next, value, index);
}
}
string _read(Node* node, int index) {
if (node->next != NULL && node->next->index <= index) {
return _read(node->next, index);
} else if (node->index == index) {
return node->text;
} else {
throw invalid_argument("No such index");
}
}
void _remove(Node* node, int index) {
if (node->next != NULL && node->next->index < index) {
_remove(node->next, index);
} else if (node->next->index == index) {
Node* temp;
if (node->next->next != NULL) {
int temp_index = node->next->next->index;
temp = new Node(node->next->next->text, temp_index);
if (node->next->next->next != NULL) {
temp->next = node->next->next->next;
} else {
temp->next = NULL;
}
temp->index = node->next->next->index;
} else {
temp = nullptr;
}
delete node->next;
node->next = temp;
} else {
throw invalid_argument("No such index");
}
}
public:
List() : head(nullptr){};
List(const List &lst) : head(nullptr) {
Node* tmp_lst = lst.head;
Node* tmp_this = this->head;
while (tmp_lst != NULL) {
// cerr << this->head->text;
tmp_this = new Node(tmp_lst->text, tmp_lst->index);
tmp_this = tmp_this->next;
tmp_lst = tmp_lst->next;
}
}
List(List&& lst);
List(initializer_list<string> lst) : List() {
Node* tmp;
int pos = 0;
for (auto element : lst) {
if (this->head != NULL){
tmp->next = new Node(element, pos);
tmp = tmp->next;
pos++;
} else {
this->head = new Node(element, pos);
tmp = this->head;
pos++;
}
}
};
List& operator= (const List& lst) {
if (this != &lst) {
delete this->head;
this->head = nullptr;
Node* tmp_lst = lst.head;
Node* tmp_this = this->head;
while (tmp_lst != NULL) {
tmp_this = new Node(tmp_lst->text, tmp_lst->index);
tmp_this = tmp_this->next;
tmp_lst = tmp_lst->next;
}
}
return *this;
}
List& operator= (List&& lst);
~List(){
delete head;
};
void insert(const string& value, int pos) {
if (pos < 0) {
throw invalid_argument("Position cant be negative");
}
if (this->head == NULL) {
Node* new_head = new Node(value, pos);
this->head = new_head;
} else if (this->head->index > pos) {
Node* new_head = new Node(value, pos);
new_head->next = this->head;
this->head = new_head;
} else {
_insert(this->head, value, pos);
}
}
string read(int pos) {
return _read(this->head, pos);
}
int size() {
return _size(this->head);
}
void remove(int pos) {
return _remove(this->head, pos);
}
public:
friend ostream& operator<< (ostream& wy, const List& lst) {
if (lst.head) return wy << "(" << *lst.head << ")";
else return wy << "()";
}
};
int main() {
return 0;
}

You List copy-constructor is severely flawed:
You initialize tmp_this to the value of this->head which is a null pointer
The above doesn't matter because the first thing you do in the loop is reassign tmp_this to point to a new Node object.
Then you immediately discard that pointer, by reassigning tmp_this to point to tmp_this->next which is a null pointer.
And you don't link anything into the list.
A working function could look something like
List(const List &lst) : head(nullptr) {
Node* tmp_lst = lst.head;
Node** tmp_current = &head;
while (tmp_lst != NULL) {
Node* tmp_this = new Node(tmp_lst->text, tmp_lst->index);
// Would prefer to use the copy-constructor here too, instead of the above
// Node* tmp_this = new Node(tmp_lst);
// This links the new node into the end of the list
*tmp_current = tmp_this
tmp_current = &tmp_this->next;
tmp_lst = tmp_lst->next;
}
}

Related

class template specialization for linked list char*

I have the following class:
template<class T>
struct Node {
Node<T>* next;
T data;
};
template<class T>
class LinkedList
{
private:
Node<T>* first;
Node<T>* last;
public:
LinkedList<T>() : first(NULL), last(NULL) {}
LinkedList<T>(const LinkedList& other)
{
operator=(other);
}
~LinkedList()
{
Node<T>* tmp = first->next;
while (!first)
{
tmp = first;
first = first->next;
free(tmp);
}
}
void add(const T& data) {
Node<T>* tmp = new Node<T>;
tmp->data = data;
tmp->next = NULL;
if (!first) {
first = tmp;
last = first;
}
else
{
last->next = tmp;
last = tmp;
}
}
Node<T>* getFirst() const
{
return first;
}
LinkedList<T>& operator=(const LinkedList<T>& other)
{
Node<T>* tmp = other.first;
while (tmp)
{
add(tmp->data);
tmp = tmp->next;
}
return *this;
}
LinkedList<T>& operator-=(T value)
{
while (first && first->data == value)
{
Node<T>* tmp = first;
first = first->next;
free(tmp);
}
for (Node<T>* curr = first; curr != NULL; curr = curr->next)
{
while (curr->next != NULL && curr->next->data == value)
{
Node<T>* tmp = curr->next;
if (tmp == last)
{
last = curr;
}
curr->next = tmp->next;
free(tmp);
}
}
return *this;
}
LinkedList<T>& operator+=(const T& value)
{
add(value);
return *this;
}
T get(int index) {
if (index == 0) {
return this->first->data;
}
else {
Node<T>* curr = this->first;
for (int i = 0; i < index; ++i) {
curr = curr->next;
}
return curr->data;
}
}
T operator[](int index) {
return get(index);
}
bool isInList(T value)
{
Node<T>* tmp = first;
while (tmp)
{
if (tmp->data == value)
return true;
tmp = tmp->next;
}
return false;
}
};
This current template implementation does not support the char* type, so I want to add a template specialization for the char* type.
Adding the following specialization:
template <>
LinkedList<char*>& LinkedList<char*>::operator+=(const char*& value)
{
}
gives the following error:
Error C2244 'LinkedList<char *>::operator +=': unable to match function definition to an existing declaration
How can I fix it?

How to Implement a Iterator class in a LinkedList class

New to c++ and I'm having trouble implementing an Iterator class in my LinkedList. I have a Iterator class defined in the private section of my LinkedList class as follows:
cs_linked_list.h
#ifndef LINKED_LIST_H_
#define LINKED_LIST_H_
#include <initializer_list>
#include <iostream>
namespace cs {
template <typename T>
class LinkedList {
struct Node; // forward declaration for our private Node type
public:
/**Constructs an empty list.**/
LinkedList(){
head_ = nullptr;
}
/**
* #brief Constructs a list from a range.
*
*/
template <class InputIterator>
LinkedList(InputIterator first, InputIterator last) {
for (; first != last; ++first) this->push_back(*first);
}
/** Constructs a list with a copy of each of the elements in `init_list`, in the same order. */
LinkedList(std::initializer_list<T> init_list) {
this->operator=(init_list); // call initializer list assignment
}
/**Constructs a container with a copy of each of the elements in another, in the same order**/
LinkedList(const LinkedList<T>& another){
//TODO
this->operator=(another);
}
/** Destroys each of the contained elements, and deallocates all memory allocated by this list. */
~LinkedList() {
while (this->head_) {
Node* old_head = this->head_;
this->head_ = old_head->next;
delete old_head;
}
}
/**Returns the number of elements in this list.Node *head**/
size_t size() const{ //DONE
//TODO
Node *temp = head_;
size_t len = 0;
while (temp != nullptr){
len++;
temp = temp->next;
}
return len;
}
/**Returns whether the list container is empty (that is, whether its size is 0). **/
bool empty() const{ //DONE
if(size_ == 0){
return true;
} else{
return false;
}
}
/** Appends a copy of `val` to this list. */
void push_back(const T& val) {
Node* new_node = new Node{val};
if (this->size_ == 0) {
this->head_ = this->tail_ = new_node;
} else {
this->tail_->next = new_node;
new_node->prev = this->tail_;
this->tail_ = new_node;
}
++this->size_;
}
/** Prepends a copy of `val` to this list. */
void push_front(const T& val) {
Node* new_node = new Node{val};
if (this->size_ == 0) {
this->head_ = this->tail_ = new_node;
} else {
new_node->next = this->head_;
this->head_->prev = new_node;
this->head_ = new_node;
}
++this->size_;
}
/**Returns a reference to the value in the first element in this list.**/
T& front() const{
return head_->data;
}
/**Returns a reference to the value in the last element in this list. **/
T& back() const{
return tail_->data;
}
/**Deletes the first value in this list. **/
void pop_front(){
Node *temp = head_;
if(empty()){
return;
}
if(temp == tail_){
return;
}
head_ = head_->next;
if (head_ != nullptr) {
head_->prev = nullptr;
} else {
tail_ = nullptr;
}
delete temp;
}
/**Deletes the last value in this list**/
void pop_back(){
if(empty()){
return;
}
if(head_ == tail_){
return;
}
Node *temp = head_;
while(temp->next->next != nullptr){
temp = temp->next;
}
tail_ = temp;
delete tail_->next;
tail_->next = nullptr;
size_--;
}
/**resizes the list so that it contains n elements.**/
void resize(std::size_t n){
//TODO
for (size_t i = 0; i < n; i++){
push_back('\0');
}
}
/**resizes the list so that it contains n elements**/
void resize(std::size_t n, const T &fill_value){
//TODO
for (size_t i = 0; i < n; i++) {
push_back(fill_value);
}
}
/**Removes from the container all the elements that compare equal to val. **/
void remove(const T &val){
//TODO
Node *p1 = head_;
Node *p2 = nullptr;
if(p1 != nullptr && p1->data == val){
head_ = p1->next;
delete p1;
} else {
while (p1 != nullptr && p1->data != val){
p2 = p1;
p1 = p1->next;
}
if (p1 == nullptr) {
return;
}
p2->next = p1->next;
delete p1;
}
}
/**Removes duplicate values from this list**/
void unique(){
//TODO
Node *temp = head_;
while (temp != nullptr && temp->next != nullptr) {
Node *temp2 = temp;
while (temp2->next != nullptr) {
if (temp->data == temp2->next->data) {
Node *temp3 = temp2->next;
temp2->next = temp2->next->next;
delete temp3;
} else {
temp2 = temp2->next;
}
}
temp = temp->next;
}
}
/**Deletes all values in this list.**/
void clear(){
//TODO
while (head_ != nullptr){
Node *temp = head_;
head_ = head_->next;
delete temp;
}
tail_ = nullptr;
size_ = 0;
}
/**Reverses the order of the elements in this list.**/
void reverse(){
//TODO
Node *p1 = head_;
Node *p2 = nullptr;
while (p1 != nullptr) {
Node *temp = p1->next;
p1->next = p2;
p2 = p1;
p1 = temp;
}
head_ = p2;
}
/** Replaces the contents of this list with a copy of each element in `init_list`. */
LinkedList& operator=(std::initializer_list<T> init_list) {
this->size_ = 0;
for (auto&& val : init_list)
this->push_back(val);
return *this;
}
/**Replaces the contents of this list with a copy of each element in another, in the same order.**/
LinkedList& operator=(const LinkedList& another){
//TODO
if (this != &another) {
this->clear();
Node *temp = another.head_;
this->size_ = 0;
while (temp) {
this->push_back(temp->data);
temp = temp->next;
}
}
return *this;
}
/**Compares this list with another for equality.**/
bool operator==(const LinkedList &another){ //DONE
//TODO
auto comp = head_;
auto comp2 = another.head_;
while(comp != nullptr){
if(comp != comp2){
return false;
}
comp = comp->next;
comp2 = comp2->next;
}
return true;
}
/**Compares this list with another for inequality. **/
bool operator!=(const LinkedList &another){ //DONE
//TODO
auto comp = head_;
auto comp2 = another.head_;
while(comp != nullptr){
if(comp != comp2){
return true;
}
comp = comp->next;
comp2 = comp2->next;
}
return false;
}
/** Inserts this list into an ostream, with the format `[element1, element2, element3, ...]` */
friend std::ostream& operator<<(std::ostream& out, const LinkedList& list) {
out << '[';
for (Node* cur = list.head_; cur; cur = cur->next) {
out << cur->data;
if (cur->next)
out << ", ";
}
out << ']';
return out;
}private:
struct Node {
T data;
Node* next = nullptr;
Node* prev = nullptr;
};
Node* head_ = nullptr;
Node* tail_ = nullptr;
std::size_t size_ = 0;
class Iterator {
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = T;
using difference_type = int;
using pointer = T*;
using reference = T&;
// Default constructor
Iterator() {
//TODO
n = nullptr;
}
// Copy constructor
Iterator(const Iterator& other) {
//TODO
this->operator=(other);
}
//Destructor if needed
~Iterator() {
//TODO
while (this->list.head_){
Node *old_head = this->list.head_;
this->list.head_ = old_head->next;
delete old_head;
}
}
// Copy assign
Iterator& operator=(const Iterator& that) {
//TODO
if(this != &that){
this->list.clear();
Node *temp = that.list.head_;
this->list.size_ = 0;
while (temp){
this->list.push_back(temp->data);
temp = temp->next;
}
}
return *this;
}
// Prefix increment
Iterator& operator++() {
//TODO
this->n = this->n->next;
return *this;
}
// Postfix increment
Iterator operator++(int) {
Iterator tmp(*this);
this->operator++();
return tmp;
}
// Prefix decrement
Iterator& operator--() {
//TODO
this->n = this->n->prev;
return *this;
}
// Postfix decrement
Iterator operator--(int) {
Iterator tmp(*this);
this->operator--();
return tmp;
}
// Inequality
bool operator!=(Iterator that) const {
return !(this->operator==(that));
}
// Equality
bool operator==(Iterator that) const {
//TODO
auto temp = list.head_;
auto temp2 = that.list.head_;
while(temp != nullptr){
if(*temp != *temp2){
return false;
}
temp = temp->next;
temp2 = temp2->next;
}
return true;
}
// lvalue dereference
T& operator*() const {
//TODO
return this->n->data;
}
// referring
Iterator* operator->() const {
return this;
}
Iterator begin(){
//TODO
return Iterator(list.head_->next);
}
Iterator end(){
//TODO
return Iterator(list.tail_);
}
private:
Node *n;
LinkedList<T> list;
};
};
} // namespace cs
#endif // LINKED_LIST_H_
Main:
#include "cs_linked_list.h"
#include <iostream>
int main() {
/***TESTING***/
cs::LinkedList<int> list;
// Add few items to the end of LinkedList
list.push_back(1);
list.push_back(2);
list.push_back(3);
std::cout << "Traversing LinkedList through Iterator" << std::endl;
for ( cs::LinkedList<int>::Iterator iter = list.begin();iter != list.end(); iter++) {
std::cout << *iter << " ";
}
std::cout << std::endl;
return 0;
}
Since my Iterator class is private I can't seem to implement my Begin() and End() functions. Should I Make it public or am I missing one crucial step. Instructions say to define a Iterator class in the private section of my LinkedList class.

Threads on xcode while compiling the c++ code

so I was trying to build this project on deque using the doubly linked list. but when I build it. it says build but gives threads and doesn't give the output as required.
I have re-implemented the major problem(copy constructor) and all the functions again and again but then it still gives me new threads every time.
this is the header file.
#pragma once
#include <stdexcept>
using namespace std;
class Node
{
public:
int data;
Node* next;
Node* previous;
Node();
Node(const int &x);
};
class Deque
{
public:
Deque();
Deque(const Deque &d);
Deque &operator= (const Deque &d);
~Deque();
void insertFront(const int &x);
void insertBack(const int &x);
int removeFront();
int removeBack();
int peekFront();
int peekBack();
bool empty() const;
int size()const;
friend ostream& operator << (ostream &out, const Deque &d);
private:
Node* front;
Node* rear;
};
this will be the .cpp (implementation file.)
//
// Deque_cmpt225.cpp
// Deque_cmpt225
//
// Created by Aryan Arora on 2017-10-09.
// Copyright © 2017 Aryan Arora. All rights reserved.
//
#include "Deque_cmpt225.h"
#include <iostream>
#include <stdexcept>
using namespace std;
Node:: Node()
{
previous = nullptr;
next = nullptr;
data = 0;
}
Node:: Node(const int &x)
{
Node();
data = x;
}
Deque:: Deque() //Empty Deque.
{
front = nullptr;
rear = nullptr;
}
Deque:: ~Deque()
{
if (this->empty())
return;
else{
Node* temp;
while (this->front->next != nullptr){
temp = this->front;
this->front = this->front->next;
delete temp;
}
temp = this->front;
this->front = nullptr;
this->rear = nullptr;
delete temp;
}
}
Deque:: Deque (const Deque &d) //Copy Constructor
{
if (d.empty()) //Deque is empty.
{
return;
}
Node* temp = d.front;
int x;
if (temp->next == nullptr) //Deque of just one node
{
x = temp->data;
Node *n1 = new Node (x);
n1->next = nullptr;
n1->previous = nullptr;
this->front = n1;
this->rear = n1;
}
else //Deque has more than one node
{
while (temp!= nullptr)
{
this->insertBack(temp->data);
temp = temp -> next;
}
}
}
Deque& Deque:: operator=(const Deque &d) //============================check again
{
if (this == &d)
return *this;
else
{
this->~Deque(); //DELETING THE DEQUE
Node* temp = d.front; //COPYING EACH NODE
while (temp != NULL)
{
this->insertBack(temp->data); //INSERTING AT THE BACK
temp = temp->next; //POINTING TEMP TO NEXT NODE
}
}
return *this;
}
void Deque:: insertFront(const int &x)
{
Node* temp = new Node(x);
temp->next = nullptr;
temp->previous = nullptr;
if (empty())
{
this->front = temp;
this->rear = temp;
}
else
{
temp->next = this->front;
temp->previous = nullptr;
this->front->previous = temp;
this->front = temp;
}
}
void Deque:: insertBack(const int &x)
{
Node* temp = new Node(x);
temp->next = nullptr;
temp->previous = nullptr;
if (empty())
{
this->front = temp;
this->rear = temp;
}
else
{
temp->next = nullptr;
temp->previous = this->rear;
this->rear->next = temp;
this->rear = temp;
}
}
int Deque:: removeFront()
{
if (empty()) //=================runtime error
{
throw std::runtime_error("The que is empty.");
}
else{
Node* temp;
temp = this->front;
int x = temp->data;
if ( this->front->next != nullptr )
{
this->front = this->front->next;
this->front->previous = nullptr;
}
else
{
this->front = nullptr;
this->rear = nullptr;
}
delete temp;
return x;
}
}
int Deque:: removeBack()
{
if (empty()) //=================runtime error
{
throw std::runtime_error("The que is empty.");
}
else{
Node* temp = this->rear;
int x = temp->data;
if ( this->rear->previous != nullptr )
{
this->rear = this->rear->previous;
this->rear->next = nullptr;
}
else
{
this->rear = nullptr;
this->front = nullptr;
}
delete temp;
return x;
}
}
int Deque:: peekFront()
{
if (empty()) //=================runtime error
{
throw std::runtime_error("The que is empty.");
}
else
{
return this->front->data;
}
}
int Deque:: peekBack()
{
if (empty()) //=================runtime error
{
throw std::runtime_error("The que is empty.");
}
else
{
return this->rear->data;
}
}
bool Deque:: empty() const
{
if (this->front == nullptr && this->rear == nullptr)
return true;
else
return false;
}
int Deque:: size() const
{
Node* temp = this->front;
int count = 0;
while (temp != nullptr)
{
count++;
temp = temp->next;
}
return count;
}
ostream& operator << (ostream &out, const Deque &d)
{
Node* temp = d.front;
out << "NULL -> ";
while (temp != nullptr)
{
out << temp->data << " <-> ";
temp= temp->next;
}
out << "<- NULL" << endl;
return out;
}
thanks in advance.
Your code has many problems..
Your Node constructor doesn't delegate properly..
Node::Node()
{
previous = nullptr;
next = nullptr;
data = 0;
}
Node::Node(const int &x)
{
Node(); //Creates a temporary node that gets destroyed immediately..
data = x;
}
It's much simpler if you change it to:
Node::Node() : Node(0) //Delegating constructor.
{
}
Node::Node(const int &x) : previous(nullptr), next(nullptr), data(x)
{
}
This point isn't really a problem, but worth mentioning.. You keep setting the Node pointers to nullptr right after construction. This is not necessary because your constructor already does that..
Node* temp = new Node(x);
temp->next = nullptr; //Not needed anymore with the above fixes.
temp->previous = nullptr; //Not needed anymore with the above fixes.
You never initialize your variables in the copy constructor..
In your constructor, you have (I changed it, but it has the same meaning):
Deque::Deque() : front(nullptr), rear(nullptr)
{
}
But in your copy constructor, you have:
Deque::Deque(const Deque &d)
{
//Other code here.. You never initialized front and rear to nullptr..
}
You never set front and rear to nullptr so empty() returns false since they are a "random" uninitialized value.. Then in insertBack you go on to access this and boom.. Access Violation.
To fix it, you do:
Deque::Deque(const Deque &d) : front(nullptr), rear(nullptr)
{
//Other code here..
}
Next issue is that your copy assignment operator is calling the destructor!
Deque& Deque::operator=(const Deque &d)
{
if (this == &d)
return *this;
else
{
this->~Deque() //YOU CANNOT DO THIS.. Create a private member function for cleaning up.. Then call that function in your destructor and call that function here.. You cannot invoke the destructor like this.
}
//....
}

Object object = *this seg fault (C++)

I'm having a segmentation fault on the following piece of code:
main.cpp
List list;
Movie *m = new Movie();
list+m; //For testing the operator +
EDIT: Full List.cpp with all functions
#include "List.h"
using namespace std;
List::List() : head(0),tail(0),size(0) { }
List::~List()
{
Node *current = head;
while( current != 0 ) {
Node* next = current->getNext();
delete current;
current = next;
}
head = 0;
}
void List::addMovie(Movie *movie)
{
Node *curNode = new Node(movie);
if(head == NULL)
{
head = curNode;
tail = curNode;
curNode->setNext(NULL);
curNode->setPrevious(NULL);
}
else
{
curNode->setNext(NULL);
tail->setNext(curNode);
curNode->setPrevious(tail);
tail = curNode;
}
size++;
}
Node *List::first()
{
return head;
}
Node *List::last()
{
return tail;
}
void List::addMovie(List & list)
{
Node *curNode = list.first();
while(curNode)
{
addMovie(curNode->getContent());
curNode = curNode->getNext();
}
}
void List::removeMovie(Movie *movie)
{
if(size == 0) return;
bool found = false;
Node *curNode = head;
while(curNode)
{
if(curNode->getContent()->getTitle().compare(movie->getTitle()) == 0)
{
found = true;
break;
}
curNode = curNode->getNext();
}
if(!found) return;
if(curNode == head && curNode == tail)
{
head = NULL;
tail = NULL;
delete curNode;
return;
}
if(curNode == head && curNode != tail)
{
head = curNode->getNext();
curNode->getNext()->setPrevious(NULL);
delete curNode;
return;
}
if(curNode != head && curNode == tail)
{
tail = curNode->getPrevious();
curNode->getPrevious()->setNext(NULL);
delete curNode;
return;
}
if(curNode != head && curNode != tail)
{
curNode->getPrevious()->setNext(curNode->getNext());
curNode->getNext()->setPrevious(curNode->getPrevious());
delete curNode;
return;
}
size--;
}
void List::removeMovie(List &list)
{
Node *curNode = list.first();
while(curNode)
{
removeMovie(curNode->getContent());
curNode = curNode->getNext();
}
}
int List::getSize()
{
return size;
}
Movie *List::findMovie(string title)
{
Node *curNode = head;
while(curNode)
{
if(title.compare(curNode->getContent()->getTitle()) == 0) return curNode->getContent();
curNode = curNode->getNext();
}
return NULL;
}
void List::clean()
{
head = tail = NULL;
size = 0;
}
void List::operator =(const List& list)
{
head = list.head;
tail = list.tail;
size = list.size;
}
void List::operator +=(Movie *movie)
{
addMovie(movie);
}
void List::operator +=(const List& list)
{
Node *curNode = list.head;
while(curNode)
{
addMovie(curNode->getContent());
curNode = curNode->getNext();
}
}
List List::operator +(Movie *m)
{
cout << "ok" << endl;
List list = *this;
cout << "ok" << endl;
//list += m;
//Node *f = list.first();
/*while(f)
{
cout << f->getContent()->getTitle() << endl;
f = f->getNext();
}*/
//return list;
//Crashing after this
}
List List::operator +(const List& list)
{
List tmp = *this;
tmp.size++;
return tmp;
}
I get a seg fault after doing list+m, getting this while debugging:
No source available for "libstdc++-6!_ZNKSs7_M_dataEv() at 0x6fc5e26c"

Linked list outputting different values to cout

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