Hey there, I can't seem to figure out why i can't access the member '*next' from the Node class to the Shuffle function. Any help would be great.
/* llist.h
* LList
*/
#include <iostream>
#include <string>
using namespace std;
typedef string ElementType;
class LList;
class Node {
protected:
ElementType data;
Node *next;
public:
Node() {next=NULL;};
Node(ElementType d) {data=d; next=NULL;}
Node* GetNext() {return next;};
ElementType GetData() {return data;};
friend class LList;
friend ostream& operator<< (ostream &, Node &);
friend ostream& operator<< (ostream &out, LList &li);
};
class LList {
protected:
Node *first;
int mySize;
public:
LList() {first = NULL; mySize = 0;}
~LList();
void Insert(ElementType, Node *);
Node* InsertAndReturn(ElementType, Node *);
void InsertFirst(ElementType);
void Delete(Node *);
void DeleteFirst();
void DeleteAll();
int FindFirst(ElementType);
friend ostream& operator<< (ostream &out, LList &li);
LList& operator= (const LList &li);
friend LList* Shuffle(LList & , LList &);
friend class Node;
};
/* llist.cpp
* Implementation of member functions for the classes Node and LList.
*/
#include "llist.h"
using namespace std;
ostream& operator<< (ostream &out, Node &n)
{
out << n.data;
return out;
}
//
// Destructor for the List class. Must remember to delete the linked list.
//
LList::~LList() {
while (first != NULL) {
DeleteFirst();
}
}
//
// Preconditions: The list has been created
// Postconditions: A node will be added after the one pointed by p.
void LList::Insert(ElementType e, Node* p) {
Node *n = new Node(e);
if (p == first) {
n->next = first;
first = n;
}
else {
n->next = p->next;
p->next = n;
}
}
void LList::InsertFirst(ElementType e) {
Insert(e,first);
}
Node* LList::InsertAndReturn(ElementType e, Node* p) {
Node *n = new Node(e);
if (p == first) {
n->next = first;
first = n;
}
else {
n->next = p->next;
p->next = n;
}
return n;
}
void LList::Delete(Node *p) {
Node *n;
if (p == first) {
n = first;
first = first->next;
}
else {
n = p->next;
p->next = n->next;
}
delete(n);
}
void LList::DeleteFirst() { Delete(first);}
int LList::FindFirst(ElementType e) {
Node *p = first;
int i = 0;
while(p!=NULL) {
if (p->data == e) return i;
else {
p = p->next;
i++;
}
}
return -1;
}
void LList::DeleteAll() {
Node *n;
int i = 0;
while (first != NULL) {
n = first;
first = first->next;
cout << "deleting: " << i++ << endl;
delete(n);
}
}
ostream& operator<< (ostream &out, LList &li) {
// Traverse the list, printing each element
Node *p = li.first;
while(p!=NULL) {
out << *p;
p = p->next;
if (p!=NULL) out << ", ";
}
return out;
}
LList& LList::operator= (const LList &li) {
Node* p;
Node *r;
p = li.first;
r = this->first;
while(p!=NULL) {
//cout << "Copying: " << p->data << " " ;
r = this->InsertAndReturn(p->data,r);
p = p->next;
}
return *this;
}
LList* Shuffle(LList &L1, LList &L2){
LList L3;
LList *L3 = new LList;
Node *p1 = L1.first;
Node *p2 = L2.first;
Node *p3 = L3.first;
L3.first = p1;
p3 = L3.first;
p3.next = p2;
p1 = p1->GetNext();
p2 = p2->GetNext();
/*---------------------------------*/
L3.first = p1;
p3 = L3.first;
p3->next = p2;
p3 = p2;
p1 = p1->GetNext();
p2 = p2->GetNext();
for(int i = 0; i < L1.mySize-1 ; i++){
p3->next = p1;
p3 = p1;
p3->next = p2;
p3 = p2;
p1 = p1->GetNext();
p2 = p2->GetNext();
}
return L3;
}
#include <iostream>
#include "llist.h"
int main (int argc, char * const argv[]) {
LList *myTestList = new LList;
cout << "will test insertion..." << endl;
myTestList->InsertFirst("abanico");
myTestList->InsertFirst("berro");
myTestList->InsertFirst("caballo");
myTestList->InsertFirst("dado");
cout << myTestList << endl;
cout << "will test delete..." << endl;
myTestList->DeleteFirst();
cout << "List after deleting the first element..." << endl;
cout << myTestList << endl;
cout << "Finding berro" << endl;
cout << myTestList->FindFirst("berro") << endl;
cout << "Finding dado" << endl;
cout << myTestList->FindFirst("dado") << endl;
//myTestList->DeleteAll();
LList myTestList2;
myTestList2 = *myTestList;
cout << "This is the content of TestList2\n" << myTestList2 << endl;
delete(myTestList);
cout << "This is the content of TestList2 after delete of TestList1:\n" << myTestList2 << endl;
/*----------------------------------------------------------------------------------------------------------*/
cout << "\nAdd an external friend function called 'Shuffle' that " << endl;
cout << "takes as input two existing lists" << endl;
cout << "(the ones using linked lists)and creates (and returns) " << endl;
cout << "a shuffled list of the elements in the two lists." << endl;
LList *newlist1 = new LList;
LList *newlist2 = new LList;
LList *newlist3 = new LList;
newlist1->InsertFirst("10");
newlist1->InsertFirst("20");
//newlist1->InsertFirst("30");
//newlist1->InsertFirst("40");
newlist2->InsertFirst("8");
newlist2->InsertFirst("16");
//newlist2->InsertFirst("24");
//newlist2->InsertFirst("32");
LList list1;
LList list2;
LList list3;
list1 = *newlist1;
cout << "\nThis is the content of List 1:\n" << list1 << endl;
list2 = *newlist2;
cout << "\nThis is the content of List 2:\n" << list2 << endl;
//list3 = *newlist3;
cout << "\nThis is the content of List 3:\n" << endl;
newlist3 = Shuffle(list1, list2);
//cout << list3 << endl;
/*------------------------------------------------------------------------------------------------------------*/
int dummy;
cout << "\nCreating a big list so that we can see impact in memory..\n";
cin >> dummy;
for (int i=0; i<10000; i++) {
myTestList2.InsertFirst("porqueria");
}
cin >> dummy;
delete(&myTestList2);
cin >> dummy;
LList *myTestList3 = new LList;
cout << "Creating a big list so that we can see impact in memory..\n";
cin >> dummy;
for (int i=0; i<10000; i++) {
myTestList3->InsertFirst("porqueria");
}
cin >> dummy;
delete(myTestList3);
cin >> dummy;
return 0;
}