Last active
December 26, 2016 21:40
-
-
Save Sara3/78deb42f93163011818c30448c363ab8 to your computer and use it in GitHub Desktop.
I am trying to use (copy constructor, destractor, and overloaded assignment operator) What am I missing up?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class linkedList{ | |
public: | |
struct Node{ | |
int data; | |
Node* next; | |
}; | |
linkedList(){ | |
head = NULL; | |
} | |
//copy constractor (problem might be here) | |
linkedList(const linkedList& a){ | |
head = NULL; | |
*this = a; | |
} | |
//copy assignment Operator (problem might be here) | |
linkedList& Operator=(const linkedList& a){ | |
if(a!=this){ | |
Node* temp = head; | |
while(temp->next!=NULL){ | |
head= head->next; | |
delete temp; | |
temp = head; | |
} | |
} | |
temp = a.head; | |
while(temp!=NULL){ | |
a.insert(temp->data); | |
} | |
return *this; | |
} | |
void insert(int val){ | |
Node* n = new Node(); | |
n->data = val; | |
n->next = head; | |
head = n; | |
} | |
void display()const{ | |
Node* n; | |
n= head; | |
while(n!=NULL){ | |
cout<<n->data; | |
n=n->next; | |
} | |
} | |
~linkedList(){ //traveres and delete pointers (problem might be here) | |
Node* cur; | |
Node* temp; | |
cur=head; | |
while(cur!=NULL){ | |
temp = cur; | |
cur = cur->next; | |
delete temp; | |
} | |
delete cur; | |
} | |
private: | |
Node* head; | |
}; | |
int main(){ | |
linkedList a; | |
a.insert(3); | |
a.display(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment