Question: How would I split this C++ program into a separate header file and implementation file? #include using namespace std; class LinkedList { struct node {
How would I split this C++ program into a separate header file and implementation file?
#include
using namespace std;
class LinkedList { struct node { int data; node *next; };
node* head;
int counter;
void memFree() { node *i; while(head != NULL) { i = head; head = head -> next; delete i; } head = NULL; }
bool isPres(int data) { node *i = head;
while (i != NULL ) { if(data == i -> data ){ return true;} i = i -> next; }
return false; }
public: LinkedList():head(NULL), counter(0) { }
LinkedList(const LinkedList &cp) { node *i = cp.head, *j; counter = cp.counter; head = NULL; while(i != NULL) { j = new node; j -> data = i -> data; j -> next = head; head = j; i = i -> next; }
}
LinkedList& operator=(const LinkedList &cp) { if(&cp == this){ return *this;} memFree(); node *i = cp.head, *j; head = NULL; while(i != NULL) { j = new node; j -> data = i -> data; j -> next = head; head = j; i = i -> next; } counter = cp.counter; return *this; }
~LinkedList() { node *i; while(head != NULL) { i = head; head = head -> next; delete i; } head = NULL; counter = 0; }
void push(int data) { node *i = new node; i -> next = head; i -> data = data; head = i; counter++; }
void printList() { node *i = head; while(i != NULL) { cout << i -> data << " "; i = i -> next; } cout << endl; }
int getsize() { return counter; }
LinkedList unionList(LinkedList list2) { node *x = head, *j = list2.head; LinkedList result;
while(x != NULL) { result.push(x -> data); x = x -> next; }
while(j != NULL) { if(!result.isPres(j -> data)) { result.push(j -> data); } j = j -> next; }
return result;
}
LinkedList intersectList(LinkedList list2) { node *x = head; LinkedList result;
while(x != NULL) { if(list2.isPres(x -> data)) { result.push(x -> data); } x = x -> next; }
return result; }
LinkedList differenceList(LinkedList list2) { node *x = head, *j = list2.head; LinkedList result; LinkedList list1;
while(x != NULL) { list1.push(x -> data); if(!list2.isPres(x -> data)) { result.push(x -> data); } x = x -> next; }
while(j != NULL) { if(!list1.isPres(j -> data)) { result.push(j -> data); } j = j -> next; }
return result; }
};
int main() { LinkedList list1; list1.push(10); list1.push(12); list1.push(19); list1.push(22); list1.push(30); list1.push(55); list1.push(78); list1.push(99); cout << "List1: "; list1.printList(); cout << "size of List1: " << list1.getsize() << endl;
LinkedList list2; list2.push(19); list2.push(30); list2.push(44); list2.push(55); list2.push(88); list2.push(89); list2.push(121); cout<<"List2: "; list2.printList(); cout << "size of List2: " << list2.getsize() << endl; LinkedList listUnion = list1.unionList(list2); cout<<"Union List: "; listUnion.printList(); LinkedList listIntersect = list1.intersectList(list2); cout<<"Intercect List: "; listIntersect.printList(); LinkedList listDifference = list1.differenceList(list2); cout<<"Difference List: "; listDifference.printList();
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
