I made a code circular doubly linked list. I think it is not good.
Could anybody to check my code??
What I have tried:
#include "CircularDLL.h"
#include "Node.h"
using namespace std;
int main()
{
CircularDLL list1;
list1.insertDataFromFile();
list1.print();
list1.insertBeforeFirst(54123, "Joe", "joe@google.com", 35);
list1.insertAfterFirst(54321, "Jim", "jim@google.com", 25);
list1.insertBeforeFirst(54123, "Joe", "joe@google.com", 35);
list1.insertAfterLast(63421, "Adam", "adam@google.com", 20);
list1.insertBeforeLast(66641, "Nancy", "nancy@google.com", 27);
list1.print();
bool found = list1.search(12321);
if (found)
cout << "the record was found" << endl;
else
cout << "the record was not found" << endl;
list1.remove(54321);
list1.print();
CircularDLL list2(list1);
list2.print();
return 0;
#ifndef NODE_H
#define NODE_H
#include <iostream>
#include<string>
using namespace std;
class Node;
class CircularDLL;
typedef Node* NodePtr;
class Node
{
friend class CircularDLL;
private:
int stId;
string stName;
string stEmail;
int stAge;
NodePtr next;
NodePtr before;
};
#include <iostream>
#include<string>
#include<fstream>
#include "Node.h"
using namespace std;
class CircularDLL
{
private:
NodePtr top;
void destroy(NodePtr&);
public:
CircularDLL();
CircularDLL(const CircularDLL& source);
~CircularDLL();
void insertDataFromFile();
void print();
bool search(int);
void insertAfterFirst(int id, string name, string email, int age);
void insertBeforeFirst(int id, string name, string email, int age);
void insertAfterLast(int id, string name, string email, int age);
void insertBeforeLast(int id, string name, string email, int age);
void remove(int);
void copy(NodePtr top1, NodePtr& top2);
};
#endif // !CIRCULARDLL_
--------------------------------------------
#include "CircularDLL.h"
CircularDLL::CircularDLL()
{
top = NULL;
}
CircularDLL::CircularDLL(const CircularDLL& source)
{
top = NULL;
copy(source.top, top);
}
CircularDLL::~CircularDLL()
{
destroy(top);
}
void CircularDLL::insertDataFromFile()
{
ifstream dataIfs;
dataIfs.open("Transaction.txt");
int id;
string name, email;
int age;
while (dataIfs >> id >> name >> email >> age)
{
insertAfterLast(id, name, email, age);
}
}
void CircularDLL::print()
{
if (top == NULL)
{
return;
}
Node *currNode = top;
do {
cout << "Node (addr " << ((int*)currNode) << "): " << currNode->stName << " <" <<
currNode->stEmail << "> age " << currNode->stAge << ", ID " << currNode->stId << ")" << endl;
currNode = currNode->next;
} while (currNode != top);
cout << "---------------------------------" << endl;
}
bool CircularDLL::search(int id)
{
NodePtr find = top;
bool found = false;
while (find !=top)
{
if (find->stId == id)
found = true;
else
find = find->next;
}
return found;
}
void CircularDLL::insertAfterFirst(int id, string name, string email, int age)
{
NodePtr newNode = new Node;
newNode->stId = id;
newNode->stName = name;
newNode->stEmail = email;
newNode->stAge = age;
if (top == NULL)
{
top = newNode;
return;
}
newNode->before = top;
newNode->next = top->next;
if (top->before == top)
{
top->before = newNode;
}
else
{
top->next->before = newNode;
}
top->next = newNode;
}
void CircularDLL::insertBeforeFirst(int id, string name, string email, int age)
{
NodePtr newNode = new Node;
NodePtr np = top;
newNode->stId = id;
newNode->stName = name;
newNode->stEmail = email;
newNode->stAge = age;
if (top->next == top)
{
newNode->before = top;
newNode->next = top;
top->next = newNode;
top->before = newNode;
top = newNode;
}
else
{
newNode->before = top->before;
newNode->next = top;
top->before = newNode;
top = newNode;
}
}
void CircularDLL::insertAfterLast(int id, string name, string email, int age)
{
NodePtr newNode= new Node;
newNode->stId = id;
newNode->stName = name;
newNode->stEmail = email;
newNode->stAge = age;
if (!top)
{
top = newNode;
top->next = top;
top->before = top;
}
else
{
NodePtr np = top;
while (np->next != top)
{
np = np->next;
}
np->next = newNode;
newNode->before = np;
newNode->next = top;
top->before = newNode;
}
}
void CircularDLL::insertBeforeLast(int id, string name, string email, int age)
{
NodePtr newNode = new Node;
newNode->stId = id;
newNode->stName = name;
newNode->stEmail = email;
newNode->stAge = age;
if (top == NULL)
{
top = newNode;
return;
}
NodePtr lastNode = top->before;
newNode->next = lastNode;
newNode->before = lastNode->before;
lastNode->before->next = newNode;
if (top->before == top)
{
top = newNode;
}
lastNode->before = newNode;
}
void CircularDLL::remove(int id)
{
NodePtr find = top;
if (search(id) == 0)
return;
while (find->next->stId != id && find->next != top)
{
find = find->next;
}
NodePtr del = find->next;
find->next = del->next ;
del->next = NULL;
delete del;
}
void CircularDLL::copy(NodePtr atop, NodePtr& btop)
{
NodePtr acurr, bcurr;
destroy(btop);
if (atop != NULL)
{
btop = new Node;
btop->stId = atop->stId;
btop->stName = atop->stName;
btop->stEmail = atop->stEmail;
btop->stAge = atop->stAge;
acurr = atop;
bcurr = btop;
while (acurr->next != atop)
{
bcurr->next = new Node;
acurr = acurr->next;
bcurr = bcurr->next;
bcurr->stId = acurr->stId;
bcurr->stName = acurr->stName;
bcurr->stEmail = acurr->stEmail;
bcurr->stAge = acurr->stAge;
}
bcurr->next = btop;
btop->before = bcurr;
}
}
void CircularDLL::destroy(NodePtr& top)
{
NodePtr curr, temp;
curr = top;
while (top != curr)
{
temp = top;
top = top->next;
delete temp;
}
top = NULL;
}