error in remove method for singly linked list - c++

#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;
}

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;
}

UpdateByValue Function is not doing anything?

#include <iostream>
#include <string>
using namespace std;
class Person{
public:
string name;
int age, height, weight;
Person(string name = "empty", int age = 0, int height = 0, int weight = 0) {
this->name = name;
this->age = age;
this->height = height;
this->weight = weight;
}
Person operator = (const Person &P) {
name = P.name;
age = P.age;
height = P.height;
weight = P.weight;
return *this;
}
void setAge(int a){
age = a;
}
int getAge(){
return age;
}
friend ostream& operator<<(ostream& os, const Person& p);
};
ostream& operator<<(ostream& os, Person& p) {
os << "Name: " << p.name << " " << "Age: " << p.age << " " << "Height: " << p.height << " " << "Weight: " << p.weight << "\n";
return os;
};
class Node {
public:
Person* data;
Node* next;
Node(Person*A) {
data = A;
next = nullptr;
}
};
class LinkedList {
public:
Node * head;
LinkedList() {
head = nullptr;
}
void InsertAtHead(Person*A) {
Node* node = new Node(A);
node->next = head;
head = node;
}
void InsertAtEnd(Person*A) {
if (head == nullptr) {
InsertAtHead(A);
}
else {
Node* node = new Node(A);
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = node;
}
}
void InsertAtPosition(Person*A, int pos) {
if (head == nullptr) {
InsertAtHead(A);
}
else {
Node* node = new Node(A);
Node* temp = head;
for (int i = 1; i < pos - 1; i++) { temp = temp->next; }
node->next = temp->next;
temp->next = node;
}
}
void DeleteByValue(string search_name) {
Node* temp = head;
Node* prev = nullptr;
while (temp != nullptr) {
if (temp->data->name == search_name) {
if (prev != nullptr) {
prev->next = temp->next;
}
else {
head = temp->next;
}
delete temp;
temp = nullptr;
}
else {
prev = temp;
temp = temp->next;
}
}
}
void DeleteFromHead() {
if (head != nullptr) {
Node* temp = head;
head = head->next;
delete temp;
}
}
void DeleteFromEnd() {
Node* prev = nullptr;
Node* temp = head;
if (head == nullptr) { cout << "Nothing to delete" << endl; }
else if (head->next == nullptr) { DeleteFromHead(); }
else {
while (temp->next != nullptr) {
prev = temp;
temp = temp->next;
}
prev->next = nullptr;
delete temp;
}
}
void DeleteAtPosition(int pos) {
Node* prev = nullptr;
Node* temp = head;
if (head == nullptr) { cout << "Nothing to delete" << endl; }
else if (pos == 1) { DeleteFromHead(); }
else {
for (int i = 1; i < pos; i++) {
prev = temp;
temp = temp->next;
}
prev->next = temp->next;
delete temp;
}
}
void UpdateAtPosition(Person*A, int pos) {
if (head == nullptr) { cout << "No element in the list"; return; }
if (pos == 1) { head->data = A; }
else {
Node* temp = head;
for (int i = 1; i < pos; i++) {
temp = temp->next;
}
temp->data = A;
}
}
void UpdateByValue(string name, int newAge) {
Node* temp = head;
Person* p = new Person();
while(temp != nullptr){
if(temp->data->name == name){
p->setAge(newAge);
}else{
temp = temp->next;
}
}
}
void Print() {
Node* temp = head;
while (temp != nullptr) {
cout << *(temp->data);
temp = temp->next;
}
cout << endl;
}
};
int main() {
LinkedList* list = new LinkedList();
list->InsertAtHead(new Person("Samantha", 20, 63, 115)); list->Print();
list->InsertAtEnd(new Person("Chris", 19, 70, 200)); list->Print();
list->DeleteByValue("Chris"); list->Print();
list->UpdateByValue("Samantha", 21); list->Print();
return 0;
}
I am new to C++ so excuse any poorly written code, but I am trying to use the function UpdateByValue to update the age of Samantha. It may look very wrong right now, but I have tried 20 different things and cannot figure out what I am doing wrong. I used to go to school at a community college where I learned Java so Im catching up to everyone in C++. A lot of it is similar but I struggle with little things like this. Could anyone explain to me how to fix the UpdateByValue function so that it will change the age of my Person object of choice? I want to be able to type the name as the first parameter and change the age of that person with the second parameter. If something is unclear and needs more explaining please let me know, I just need help. Thanks in advance, and please feel free to give any other constructive criticism. I am trying to get as good as I can.
Let's take a walk through UpdateByValue. I'll comment as we go.
void UpdateByValue(string name, int newAge) {
Node* temp = head;
Person* p = new Person();
while(temp != nullptr){ // keep looking until end of list
if(temp->data->name == name){ // found node with name
p->setAge(newAge); // update a different node
// never advance node so we can't exit function
}else{
temp = temp->next;
}
}
}
Try instead
void UpdateByValue(string name, int newAge) {
Node* temp = head;
// Person * p is not needed
while(temp != nullptr){ // keep looking until end of list
if(temp->data->name == name){ // found node with name
temp->data->setAge(newAge); // update the found node
return; // done searching. Exit function
}else{
temp = temp->next;
}
}
}

Linked List insertion/deletion

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* head = NULL;
int size;
Node* tail = NULL;
void printLinkedList() {
Node *search = head;
if (head == NULL) {
cout << "linkedlist is empty" << endl;
}
else {
while (search != NULL){
cout << search->data << endl;
search = search->next;
}
}
}
int sizeLinkedList() {
size = 1;
Node* current = head;
while (current->next != NULL) {
current = current->next;
size = size + 1;
}
cout << size << endl;
return size;
}
Node *getNode(int position){
Node *current = head;
for (int i = 0; i<position; i++)
{
current = current->next;
}
return current;
}
void appendNode(int n) {
Node *newNode = new Node; //creating new node
newNode->data = n;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
return;
}
else {
Node *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
void insertNode(int n, int position) {
Node *newNode = new Node;
newNode->data = n;
newNode->next = NULL;
int size = sizeLinkedList();
if (position = 0){
if (head == NULL) {
head = newNode;
}
else{
newNode->next = head;
head = newNode;
}
}
else if (position == size) {
appendNode(n);
}
else {
Node *prevNode = getNode(position-1);
Node *nextNode = getNode(position);
prevNode->next = newNode;
newNode->next = nextNode;
}
}
void deleteNode(int position) {
Node *currentNode;
int size = sizeLinkedList();
if (size == 0) {
return;
}
if (position == 0) {
currentNode = head->next;
head = currentNode;
}
else if (position == size-1) {
getNode(position - 1)->next = NULL;
delete getNode(position);
}
else {
getNode(position - 1)->next = getNode(position+1);
delete getNode(position);
}
}
//making a dynamic array only via pointers in VC++
void makeArray() {
int* m = NULL;
int n;
cout << "how many entries are there?"<<endl;
cin >> n;
m = new int[n];
int temp;
for (int x = 0; x < n; x++){
cout << "enter item:"<< x+1<< endl;
cin >> temp;
*(m + x) = temp;
}
for (int x = 0; x < n; x++){
cout << x+1 + ":" << "There is item: "<<*(m+x) << endl;
}
delete[]m;
}
int main() {
int x;
//makeArray();
appendNode(1);
appendNode(2);
appendNode(32);
appendNode(55);
appendNode(66);
//insertNode(2, 0);
printLinkedList();
deleteNode(3);
printLinkedList();
sizeLinkedList();
cin >> x;
}
Im just trying to code a Linked List with a couple of functions for practice
My Delete function, the last else statement isnt working, and logically I cant figure out why,
as for my insert function none of the statements are working, not even at head or position 0. However appending items, returning size, printing the list, deleting the first and last elements works.
thanks!
sizeLinkedList won't work correctly if the list is empty (it cannot return 0)
you are using size as different variables at different scopes (at main scope, and within deleteNode). This is pretty confusing although not strictly wrong.
in deleteNode, this sequence won't work:
else if (position == size-1) {
getNode(position - 1)->next = NULL;
delete getNode(position);
}
setting the next pointer on the node prior to position to NULL will interfere with the attempt to getNode(position) in the very next line, because it traverses the list based on next. The fix is to reverse these two lines.
Likewise, your last sequence in deleteNode won't work for a similar reason, because you are modifying the next pointers:
else {
getNode(position - 1)->next = getNode(position+1);
delete getNode(position); // this list traversal will skip the node to delete!
}
the solution here is like this:
else {
currentNode = getNode(position);
getNode(position - 1)->next = getNode(position+1);
delete currentNode;
}
I've also re-written the insertNode function incorporating the comment provided by #0x499602D2 .
Here's a modified version of your code that has your current sequence in main fixed:
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* head = NULL;
int size = 0;
Node* tail = NULL;
void printLinkedList() {
Node *search = head;
if (head == NULL) {
cout << "linkedlist is empty" << endl;
}
else {
while (search != NULL){
cout << search->data << endl;
search = search->next;
}
}
}
int sizeLinkedList() {
size = 0;
if (head->next != NULL){
size = 1;
Node* current = head;
while (current->next != NULL) {
current = current->next;
size = size + 1;
}
}
cout << size << endl;
return size;
}
Node *getNode(int position){
Node *current = head;
for (int i = 0; i<position; i++)
{
current = current->next;
}
return current;
}
void appendNode(int n) {
Node *newNode = new Node; //creating new node
newNode->data = n;
newNode->next = NULL;
size++;
if (head == NULL)
{
head = newNode;
return;
}
else {
Node *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
void insertNode(int n, int position) {
Node *newNode = new Node;
newNode->data = n;
newNode->next = NULL;
if (position == 0){
newNode->next = head;
head = newNode;
}
else if (position == sizeLinkedList()) {
appendNode(n);
}
else {
Node *prevNode = getNode(position-1);
Node *nextNode = getNode(position);
prevNode->next = newNode;
newNode->next = nextNode;
}
}
void deleteNode(int position) {
Node *currentNode;
int my_size = sizeLinkedList();
if ((my_size == 0) || (position > my_size)) {
return;
}
if (position == 0) {
currentNode = head->next;
head = currentNode;
}
else if (position == size-1) {
delete getNode(position);
getNode(position - 1)->next = NULL;
}
else {
currentNode = getNode(position);
getNode(position - 1)->next = getNode(position+1);
delete currentNode;
}
}
//making a dynamic array only via pointers in VC++
void makeArray() {
int* m = NULL;
int n;
cout << "how many entries are there?"<<endl;
cin >> n;
m = new int[n];
int temp;
for (int x = 0; x < n; x++){
cout << "enter item:"<< x+1<< endl;
cin >> temp;
*(m + x) = temp;
}
for (int x = 0; x < n; x++){
cout << x+1 + ":" << "There is item: "<<*(m+x) << endl;
}
delete[]m;
}
int main() {
int x;
//makeArray();
appendNode(1);
appendNode(2);
appendNode(32);
appendNode(55);
appendNode(66);
insertNode(2, 0);
printLinkedList();
deleteNode(3);
printLinkedList();
sizeLinkedList();
}

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"

What are self referential C++ types good for?

What are use cases for self-referential types?
By self referential types, I mean:
class T {
T *ptr; // member variable that references the type of the class
};
It is one of the most efficient ways to build linked lists or tree
hierarchies.
#include <iostream>
class linked_ints {
public:
linked_ints() : next(nullptr), x(0) {}
linked_ints* next;
int x;
};
void print(linked_ints* b) {
if(b == nullptr) return;
do {
std::cout << b->x << std::endl;
} while((b = b->next));
}
int main()
{
linked_ints x, y, z;
x.next = &y; y.next = &z;
print(&x);
return 0;
}
One example I can think of are linked lists.
The example borrowed from here:
#include<iostream>
struct Node{
int data;
Node* next;
};
void InsertAfter(Node **head, int value){
if(*head==NULL){
Node* temp=NULL;
temp = new Node;
temp->data = value;
temp->next = NULL;
*head = temp;
}else{
Node *temp = new Node;
temp->data = value;
temp->next = (*head)->next;
(*head)->next = temp;
}
}
void DeleteAfter(Node **head){
if(*head==NULL){
return;
}else{
Node *temp = NULL;
temp = (*head)->next;
(*head)->next = (*head)->next->next;
delete temp;
temp=NULL;
}
}
int DeleteAll(Node **head,int value){
int count=0;
Node *p = NULL;
Node *q = (*head);
if(*head==NULL){
count =0;
}else{
while((q)!=NULL){
if((q)->data==value){
Node *temp = NULL;
temp = q;
if ( p!=NULL){
p->next = q->next;
}else{
(*head) = q->next;
}
q = q->next;
delete temp;
temp = NULL;
++count;
}else{
p = q;
q = q->next;
}
}
}
return count;
}
void DisplayList(Node *head){
if(head!=NULL){
std::cout << head->data << "\n";
while(head->next!=NULL){
std::cout << head->data << "\n";
head =
head->next;
}
std::cout << "\n\n";
}
int main(){
Node *head=NULL;
InsertAfter(&head,10);
InsertAfter(&head,10);
InsertAfter(&head,20);
InsertAfter(&head,10);
DisplayList(head);
DeleteAfter(&head);
DisplayList(head);
int a = DeleteAll(&head,10);
std::cout << "Number Of Nodes deleted
having value 10 = " <<
a <<"\n\n";
DisplayList(head);
return 0;
}