Deque: swap first and last element without using STL - c++

I have to implement deque using linked list without stl.
Issue:
I got stucked with function swap, and I can't figure out where is a problem.
My code you can see below:
#include <iostream>
using namespace std;
struct Node
{
int mData;
Node* pPrev, *pNext;
Node()
{
pPrev = pNext = NULL;
}
Node(int value)
{
mData = value;
pPrev = pNext = NULL;
}
};
class Deque
{
private:
Node *pFront, *pRear;
int mSize;
public:
Deque()
{
pFront = pRear = NULL;
mSize = 0;
}
void pushFront(int data)
{
Node* newNode = new Node(data);
if (newNode == NULL)
{
cout << "Error";
}
else
{
if (pFront == NULL)
{
pRear = pFront = newNode;
}
else
{
newNode->pNext = pFront;
pFront->pPrev = newNode;
pFront = newNode;
}
mSize++;
}
}
void pushLast(int data)
{
Node* newNode = new Node(data);
if (newNode == NULL)
{
cout << "Error";
}
else
{
if (pRear == NULL)
{
pFront = pRear = newNode;
}
else
{
newNode->pPrev = pRear;
pRear->pNext = newNode;
pRear = newNode;
}
mSize++;
}
}
void deleteFront()
{
if (isEmpty())
{
cout << "Deque is empty";
}
else
{
Node* temp = pFront;
pFront = pFront->pNext;
if (pFront == NULL)
{
pRear = NULL;
}
else
{
pFront->pPrev = NULL;
}
delete temp;
mSize--;
}
}
void deleteLast()
{
if (isEmpty())
{
cout << "Deque is empty";
}
else
{
Node* temp = pRear;
pRear = pRear->pPrev;
if (pRear == NULL)
{
pFront = NULL;
}
else
{
pRear->pNext = NULL;
}
delete temp;
mSize--;
}
}
void swap()
{
if (isEmpty())
{
cout << "Deque is empty";
}
else
{
Node* tmp_head = pFront;
tmp_head->mData = pFront->mData;
tmp_head->pNext = pFront->pNext;
tmp_head->pPrev = pFront->pPrev;
Node* tmp_rear = pRear;
tmp_rear->mData = pRear->mData;
tmp_rear->pNext = pRear->pNext;
tmp_rear->pPrev = pRear->pPrev;
while (tmp_head->pNext != NULL && pRear->pPrev!=NULL) {
tmp_head = tmp_head->pNext;
tmp_rear = tmp_rear->pPrev;
}
pRear = tmp_head;
pRear->mData = tmp_head->mData;
pFront = tmp_rear;
pFront->mData = tmp_rear->mData;
}
}
bool isEmpty()
{
return (mSize == 0);
}
int getSize()
{
return mSize;
}
void show()
{
Node* node = pFront;
while (node != NULL)
{
cout << node->mData << " ";
node = node->pNext;
}
}
};
int main()
{
Deque deque;
deque.pushFront(1);
deque.pushFront(2);
deque.pushFront(3);
deque.show();
deque.swap();
deque.show();
}
Being quite new in c++ algorithms, thus don't judge me in case I did some mistakes:)If there is any questions regarding code, don't hesitate to ask.
Thank You in advance!

Related

Segmentation error when I try to sort linked list using the function MAX() [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 11 months ago.
Improve this question
so I've an assignment to sort single linked list ascending and descending wise here's the code, in my head it should work as getting the maximum and adding it into a new list and delete the max in order to find the new maximum of the old list.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
Node * createNode(int x)
{
Node * newnode = new Node;
newnode-> data = x;
newnode-> next = NULL;
return newnode;
}
class List {
Node *head;
public:
List()
{
head=NULL;
}
void Delete(int x)
{
Node* q;
Node* p = head;
while(head->data==x && head!=NULL)
{
q=head;
head=head->next;
delete(q);
}
p=head;
while(p->next!=NULL)
{
if(p->next->data==x)
{
q=p->next;
p->next=p->next->next;
delete(q);
}
else
{
p=p->next;
}
}
}
void InsertAtBeggining(int x)
{
Node* newnode = createNode(x);
if(head==NULL)
{
head=newnode;
}
else
{
newnode->next=head;
head=newnode;
}
}
void InsertLast(int x)
{
Node* newnode = createNode(x);
Node* p=head;
if(head==NULL)
{
head=newnode;
}
else
{
while(p->next!=NULL) // 4 5 6 7
{
p=p->next;
}
p->next=newnode;
newnode->data=x;
newnode->next=NULL;
}
}
int MAX()
{
Node *p=head;
int m=p->data;
if(head==NULL)
{
cout<<"List is empty! ";
}
else
{
while(p!=NULL)
{
if(p->data>m)
{
m=p->data;
p=p->next;
}
else{
p=p->next;
}
}
return m;
}
}
int MIN()
{
Node *p=head;
int m=p->data;
if(head==NULL)
{
cout<<"List is empty! ";
}
else
{
while(p!=NULL)
{
if(p->data<m)
{
m=p->data;
p=p->next;
}
else{
p=p->next;
}
}
return m;
}
}
int size()
{
int count = 0;
Node* current = head;
while (current != NULL)
{
count++;
current = current->next;
}
return count;
}
/* int locate(int x) {
node* p = head;
int c = 0;
while (p != NULL) {
if (p->data != x) {
p = p->next;
c++;
}
else {
return c;
}
}
return -1;
}
*/
List Ascending_sorting(List l)
{
List l2 = List();
int c=l.size();
while (c!=0)
{
int x=l.MAX();
l2.InsertAtBeggining(x);
l.Delete(MAX());
c--;
l2.print();
cout<<endl;
}
l2.print();
}
/*
List Descending_sorting(List l)
{
List l2 = List();
int c=l.size();
while (c!=0)
{
int x=l.MAX();
l2.InsertLast(x);
l.Delete(MAX());
c--;
}
cout<<"tany print"<<endl;
l2.print();
}
*/
void print()
{
Node *p=head;
while(p!=NULL)
{
cout<<p->data<<endl;
p=p->next;
}
}
};
int main()
{
List l;
l.InsertAtBeggining(9);
l.InsertAtBeggining(2);
l.InsertAtBeggining(3);
l.InsertAtBeggining(4);
l.InsertAtBeggining(1);
l.InsertLast(30);
l.InsertLast(20);
l.InsertLast(60);
l.InsertLast(10);
l.InsertLast(90);
int z;
z = l.MAX();
cout<<"max "<<z<<endl;
l.Ascending_sorting(l);
system("PAUSE");
return 0;
}
I'd like any help :) it's printing l2 inside the while loop but it fails outside of it, also it's not printing the 1.
you should check before derefrence pointers!
actually I changed these lines:
while((head != NULL) && head->data == x)
while((p != NULL) && p->next != NULL)
and after changed you have this code:
#include <iostream>
using namespace std;
struct Node
{
int data;
Node* next;
};
Node* createNode(int x)
{
Node* newnode = new Node;
newnode->data = x;
newnode->next = NULL;
return newnode;
}
class List
{
Node* head;
public:
List()
{
head = NULL;
}
void Delete(int x)
{
Node* q;
Node* p = head;
while((head != NULL) && head->data == x)
{
q = head;
head = head->next;
delete(q);
}
p = head;
while((p != NULL) && p->next != NULL)
{
if(p->next->data == x)
{
q = p->next;
p->next = p->next->next;
delete(q);
}
else
{
p = p->next;
}
}
}
void InsertAtBeggining(int x)
{
Node* newnode = createNode(x);
if(head == NULL)
{
head = newnode;
}
else
{
newnode->next = head;
head = newnode;
}
}
void InsertLast(int x)
{
Node* newnode = createNode(x);
Node* p = head;
if(head == NULL)
{
head = newnode;
}
else
{
while(p->next != NULL) // 4 5 6 7
{
p = p->next;
}
p->next = newnode;
newnode->data = x;
newnode->next = NULL;
}
}
int MAX()
{
Node* p = head;
int m = p->data;
if(head == NULL)
{
cout << "List is empty! ";
}
else
{
while(p != NULL)
{
if(p->data > m)
{
m = p->data;
p = p->next;
}
else
{
p = p->next;
}
}
return m;
}
}
int MIN()
{
Node* p = head;
int m = p->data;
if(head == NULL)
{
cout << "List is empty! ";
}
else
{
while(p != NULL)
{
if(p->data < m)
{
m = p->data;
p = p->next;
}
else
{
p = p->next;
}
}
return m;
}
}
int size()
{
int count = 0;
Node* current = head;
while(current != NULL)
{
count++;
current = current->next;
}
return count;
}
/* int locate(int x) {
node* p = head;
int c = 0;
while (p != NULL) {
if (p->data != x) {
p = p->next;
c++;
}
else {
return c;
}
}
return -1;
}
*/
List Ascending_sorting(List l)
{
List l2 = List();
int c = l.size();
while(c != 0)
{
int x = l.MAX();
l2.InsertAtBeggining(x);
l.Delete(MAX());
c--;
l2.print();
cout << endl;
}
l2.print();
}
/*
List Descending_sorting(List l)
{
List l2 = List();
int c=l.size();
while (c!=0)
{
int x=l.MAX();
l2.InsertLast(x);
l.Delete(MAX());
c--;
}
cout<<"tany print"<<endl;
l2.print();
}
*/
void print()
{
Node* p = head;
while(p != NULL)
{
cout << p->data << endl;
p = p->next;
}
}
};
int main()
{
List l;
l.InsertAtBeggining(9);
l.InsertAtBeggining(2);
l.InsertAtBeggining(3);
l.InsertAtBeggining(4);
l.InsertAtBeggining(1);
l.InsertLast(30);
l.InsertLast(20);
l.InsertLast(60);
l.InsertLast(10);
l.InsertLast(90);
int z;
z = l.MAX();
cout << "max " << z << endl;
l.Ascending_sorting(l);
system("PAUSE");
return 0;
}

error in remove method for singly linked list

#include <iostream>
using namespace std;
template <typename Object>
struct Node
{
Object data;
Node* next;
Node(const Object &d = Object(), Node *n = (Object)NULL) : data(d), next(n) {}
};
template <typename Object>
class singleList
{
public:
singleList() { init(); }
~singleList() { eraseList(head); }
singleList(const singleList &rhs)
{
eraseList(head);
init();
*this = rhs;
print();
contains(head);
}
void init()
{
theSize = 0;
head = new Node<Object>;
head->next = (Object)NULL;
}
void eraseList(Node<Object> *h)
{
Node<Object> *ptr = h;
Node<Object> *nextPtr;
while (ptr != (Object)NULL)
{
nextPtr = ptr->next;
delete ptr;
ptr = nextPtr;
}
}
int size()
{
return theSize;
}
void print()
{
int i;
Node<Object> *current = head;
for(i=0; i < theSize; ++i){
cout << current->data << " ";
current = current->next;
}
}
bool contains(int x)
{
Node<Object> *current = head;
for (int i = 0; i < theSize; ++i){
if (current->data == x){
return true;
}
current = current -> next;
}
return false;
}
bool add(Object x){
if(!contains(x)){
Node<Object> *new_node = new Node<Object>(x);
new_node->data = x;
new_node->next = head;
head = new_node;
//head->next = new_node;
theSize++;
return true;
}
return false;
}
bool remove(int x)
{
if(contains(x)){
Node<Object> *temp = head;
Node<Object> *prev = NULL;
if(temp != NULL && temp ->data == x){
head = temp->next;
delete temp;
return 0;
}else{
while(temp != NULL && temp->data != x){
prev = temp;
temp = temp->next;
}
if(temp ==NULL){
return 0;
}
prev->next = temp->next;
delete temp;
}
return true;
//theSize--;
}
return false;
}
private:
Node<Object> *head;
int theSize;
};
int main()
{
singleList<int> *lst = new singleList<int>();
lst->add(10);
lst->add(12);
lst->add(15);
lst->add(6);
lst->add(3);
lst->add(8);
lst->add(3);
lst->add(18);
lst->add(5);
lst->add(15);
cout << "The original linked list: ";
lst->print();
cout << endl;
lst->remove(6);
lst->remove(15);
cout << "The updated linked list: ";
lst->print();
cout << endl;
cout << "The number of node in the list: " << lst->size() << endl;
return 0;
}
so the output is supposed to be the following:
The original linked list: 5 18 8 3 6 15 12 10
The update linked list: 5 18 8 3 12 10
The number of node in the list: 6
my output gives the original linked list part but then gives a segmentation fault (core dumped) error. I am not sure where my code is wrong but i think it is in my remove().
Some help will definitely be appreciated.
on line 109 i needed to decrement size.
bool remove(int x)
{
if(contains(x)){
Node<Object> *temp = head;
Node<Object> *prev = NULL;
if(temp != NULL && temp ->data == x){
head = temp->next;
delete temp;
return 0;
}else{
while(temp != NULL && temp->data != x){
prev = temp;
temp = temp->next;
}
if(temp ==NULL){
return 0;
}
prev->next = temp->next;
delete temp;
theSize--;
}
return true;
//theSize--;
}
return false;
}
Answer after second update:
had to fix the remove()
#include <iostream>
using namespace std;
template <typename Object>
struct Node
{
Object data;
Node* next;
Node(const Object &d = Object(), Node *n = (Object)NULL) : data(d), next(n) {}
};
template <typename Object>
class singleList
{
public:
singleList() { init(); }
~singleList() { eraseList(head); }
singleList(const singleList &rhs)
{
eraseList(head);
init();
*this = rhs;
print();
contains(head);
}
void init()
{
theSize = 0;
head = new Node<Object>;
head->next = (Object)NULL;
}
void eraseList(Node<Object> *h)
{
Node<Object> *ptr = h;
Node<Object> *nextPtr;
while (ptr != (Object)NULL)
{
nextPtr = ptr->next;
delete ptr;
ptr = nextPtr;
}
}
int size()
{
return theSize;
}
void print()
{
int i;
Node<Object> *current = head;
for(i=0; i < theSize; ++i){
cout << current->data << " ";
current = current->next;
}
}
bool contains(int x)
{
Node<Object> *current = head;
for (int i = 0; i < theSize; ++i){
if (current->data == x){
return true;
}
current = current -> next;
}
return false;
}
bool add(Object x){
if(!contains(x)){
Node<Object> *new_node = new Node<Object>(x);
new_node->data = x;
new_node->next = head;
head = new_node;
//head->next = new_node;
theSize++;
return true;
}
return false;
}
bool remove(int x){
Node<Object> *pCur = head;
Node<Object> *pPrev = pCur;
while (pCur && pCur->data != x) {
pPrev = pCur;
pCur = pCur->next;
}
if (pCur==nullptr) // not found
return false;
if (pCur == head) { // first element matches
head = pCur->next;
} else {
pPrev->next = pCur->next;
}
// pCur now is excluded from the list
delete pCur;
theSize--;
return true;
}
private:
Node<Object> *head;
int theSize;
};
int main()
{
singleList<int> *lst = new singleList<int>();
lst->add(10);
lst->add(12);
lst->add(15);
lst->add(6);
lst->add(3);
lst->add(8);
lst->add(3);
lst->add(18);
lst->add(5);
lst->add(15);
cout << "The original linked list: ";
lst->print();
cout << endl;
lst->remove(6);
lst->remove(15);
cout << "The updated linked list: ";
lst->print();
cout << endl;
cout << "The number of node in the list: " << lst->size() << endl;
return 0;
}

Doubly Linked List fails to add or delete valid entries

I've run it many times. I tried fixing my deleteNode() and addNode(), but it did not work. The output showed me that it failed to add some valid entries in my list, which resulted in failing to delete these valid entries. Someone please help me find the errors... I think either my isEmpty() is wrong or the addNode got messed up.
// Add nodes and makes it work in any cases: backward/forward
bool LinkedList::addNode(int id, string str) {
bool result = false;
if (id >= 0 && !(idExists(id))) {
Node *current = head;
Node *temp = new Node;
temp->data.data = str;
temp->data.id = id;
temp->forward = NULL;
temp->back = NULL;
// Kinds of adding cases
if(head == NULL) { // Check if list is empty
addHead(temp, current);
result = true;
} else {
while(temp->data.id > current->data.id && current->forward != NULL) {
current = current->forward;
}
// Backward
if(current->back == NULL) {
if(temp->data.id > current->data.id) {
if(current->forward == NULL) {
addTail(temp, current);
} else {
addMiddle(temp, current);
}
} else {
addHead(temp, current);
}
result = true;
// Forward
}else if(current->forward == NULL) {
if (temp->data.id > current->data.id) {
addTail(temp, current);
} else {
addMiddle(temp, current);
}
result = true;
}else {
if(temp->data.id > current->data.id) {
addMiddle(temp, current);
result = true;
}
}
}
}
return result;
}
void LinkedList::addHead(Node *temp, Node *current) {
if (head != NULL){
temp->forward = current;
current->back = temp;
head = temp;
} else {
head = temp;
}
}
void LinkedList::addMiddle(Node *temp, Node *current) {
temp->forward = current;
temp->back = current->back;
current->back->forward = temp;
current->back = temp;
}
void LinkedList::addTail(Node *temp, Node *current) {
current->forward = temp;
temp->back = current;
}
// Delete list
bool LinkedList::deleteNode(int id){
bool result = false;
if (idExists(id)) {
Node *current = head;
while (current->forward != NULL && current->data.id != id) {
current = current->forward;
}
if (current->data.id == id && current->forward == NULL) {
if (current->back == NULL) { // Delete head
delete current;
head = NULL;
} else { // delete tail
deleteTail(current);
}
result = true;
} else if (current->data.id == id) {
if (current->back == NULL)
deleteHead(current);
else // delete middle
deleteMiddle(current);
result = true;
}
}
return result;
}
// Helper delete functions
void LinkedList::deleteHead(Node *current) {
head = current->forward;
head->back = NULL;
delete current;
}
void LinkedList::deleteMiddle(Node *current) {
current->back->forward = current->forward;
current->forward->back = current->back;
delete current;
}
void LinkedList::deleteTail(Node *current) {
current->back->forward = NULL;
delete current;
}
bool LinkedList::getNode(int id, Data *data) {
bool didGetNode = false;
if (idExists(id)) {
Node *current = head;
while (current->forward != NULL && current->data.id != id) {
current = current->forward;
}
data->id = current->data.id;
data->data = current->data.data;
didGetNode = true;
}
return didGetNode;
}
// Check whether or not the id exists
bool LinkedList::idExists(int id){
bool exists = false;
if (head != NULL){
Node *current = head;
while (current->forward != NULL && current->data.id != id) {
current = current->forward;
}
if (current->data.id == id) {
exists = true;
}
}
return exists;
}
You probably want to be passing a pointer to a pointer (**) or a pointer to a reference (*&) in functions where you are wanting to make a change to what the address of the node is containing. I hope that this helps you visualize it.
For example:
struct Node { };
void setNull1(Node* temp)
{
temp = nullptr;
}
void setNull2(Node** temp)
{
(*temp) = nullptr;
}
void setNull3(Node*& temp)
{
temp = nullptr;
}
int main()
{
Node* tmp = new Node;
setNull1(tmp);
if (tmp == nullptr)
{
cout << "NULLPTR";
} // tmp is not nullptr
Node* tmp1 = new Node;
setNull2(&tmp1);
if (tmp1 == nullptr)
{
cout << "NULLPTR";
} // tmp1 is nullptr
Node* tmp2 = new Node;
setNull3(tmp2);
if (tmp2 == nullptr)
{
cout << "NULLPTR";
} // tmp2 is nullptr
}
You should also consider writing nullptr instead of NULL.

Logical Error in Program

I tried using cout at various stages to figure but in vain. The program crashes after flushing the first two values in the reversed list. It prints a garbage value as the third value and before it can print the last two values, it crashes.
#include <iostream>
using namespace std;
class LLStack {
public:
struct Node {
int data;
Node* next;
Node(int n) {
data = n;
next = 0;
}
Node(int n, Node* node) {
data = n;
next = node;
}
};
LLStack();
LLStack(const LLStack&);
LLStack& operator = (const LLStack&);
~LLStack();
void push(int);
int pop();
int top();
bool isEmpty();
void flush();
private:
Node* head;
};
LLStack::LLStack() {
head = 0;
}
LLStack::LLStack(const LLStack& s) {
head = new Node(NULL);
head->data = s.head->data;
if (s.head->next != NULL) {
head->next = new Node(*(s.head->next));
}
else {
head->next = new Node(NULL);
}
}
LLStack::~LLStack() {
this->flush();
}
LLStack& LLStack::operator = (const LLStack& s) {
this->head = new Node(NULL);
this->head->data = s.head->data;
if (s.head->next != NULL) {
this->head->next = new Node(*(s.head->next));
}
else {
this->head->next = new Node(NULL);
}
return *this;
}
void LLStack::push(int x) {
if (head == 0) head = new Node(x);
else {
Node* temp = new Node(x, head);
head = temp;
}
}
int LLStack::pop() {
if (head == 0) {
cout << "\n\nNo elements to pop\n\n";
return -1;
}
else {
Node* temp = head;
int n = temp->data;
head = temp->next;
delete temp;
return n;
}
}
int LLStack::top() {
if (head == 0) {
cout << "\n\nNo elements in the stack\n\n";
return -1;
}
else {
return head->data;
}
}
bool LLStack::isEmpty() {
return (head == 0);
}
void LLStack::flush() {
if (head == 0) {
cout << "\n\nNo elements in the Stack to flush\n\n";
return;
}
cout << "\n\nFlushing the Stack: ";
Node* temp = 0;
while (head != 0) {
temp = head;
cout << temp->data << " ";
head = head->next;
delete temp;
}
cout << endl << endl;
}
void reverseStack(LLStack& s1) {
LLStack s2;
while (!s1.isEmpty()) {
s2.push(s1.pop());
}
s1 = s2;
}
int main() {
LLStack s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
reverseStack(s);
cout << "\n\nFlushing s:\n";
s.flush();
return 0;
}
I tried using cout at various stages to figure but in vain. The program crashes after flushing the first two values in the reversed list. It prints a garbage value as the third value and before it can print the last two values, it crashes.
#include <iostream>
using namespace std;
class LLStack {
public:
struct Node {
int data;
Node* next;
Node(int n) {
data = n;
next = 0;
}
Node(int n, Node* node) {
data = n;
next = node;
}
};
LLStack();
LLStack(const LLStack&);
LLStack& operator = (const LLStack&);
~LLStack();
void push(int);
int pop();
int top();
bool isEmpty();
void flush();
private:
Node* head;
};
LLStack::LLStack() {
head = 0;
}
LLStack::LLStack(const LLStack& s) {
head = new Node(NULL);
head->data = s.head->data;
if (s.head->next != NULL) {
head->next = new Node(*(s.head->next));
}
else {
head->next = new Node(NULL);
}
}
LLStack::~LLStack() {
this->flush();
}
LLStack& LLStack::operator = (const LLStack& s) {
this->head = new Node(NULL);
this->head->data = s.head->data;
if (s.head->next != NULL) {
this->head->next = new Node(*(s.head->next));
}
else {
this->head->next = new Node(NULL);
}
return *this;
}
void LLStack::push(int x) {
if (head == 0) head = new Node(x);
else {
Node* temp = new Node(x, head);
head = temp;
}
}
int LLStack::pop() {
if (head == 0) {
cout << "\n\nNo elements to pop\n\n";
return -1;
}
else {
Node* temp = head;
int n = temp->data;
head = temp->next;
delete temp;
return n;
}
}
int LLStack::top() {
if (head == 0) {
cout << "\n\nNo elements in the stack\n\n";
return -1;
}
else {
return head->data;
}
}
bool LLStack::isEmpty() {
return (head == 0);
}
void LLStack::flush() {
if (head == 0) {
cout << "\n\nNo elements in the Stack to flush\n\n";
return;
}
cout << "\n\nFlushing the Stack: ";
Node* temp = 0;
while (head != 0) {
temp = head;
cout << temp->data << " ";
head = head->next;
delete temp;
}
cout << endl << endl;
}
void reverseStack(LLStack& s1) {
LLStack s2;
while (!s1.isEmpty()) {
s2.push(s1.pop());
}
s1 = s2;
}
int main() {
LLStack s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
reverseStack(s);
cout << "\n\nFlushing s:\n";
s.flush();
return 0;
}
Please run it and you'll understand for yourself.
The problem is in function:
void reverseStack(LLStack& s1) {
LLStack s2;
while (!s1.isEmpty()) {
s2.push(s1.pop());
}
s1 = s2;
}
Here you are creating the local stack "s2". When the s2 goes out of scope, it calls the destructor and flushes the whole stack. Better you should remove the flush call from destructor or create the global "s2". Best option will be implement the other logic to reverse the stack.

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"