Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I made a code circular doubly linked list. I think it is not good.

Could anybody to check my code??

What I have tried:

C#
#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"

//--------------------------------------------
//--------------------------------------------
// the default constructor
CircularDLL::CircularDLL()
{
	top = NULL;
}
//--------------------------------------------
//--------------------------------------------
// the copy constructor
CircularDLL::CircularDLL(const CircularDLL& source)
{
	top = NULL;

	copy(source.top, top);
}

//--------------------------------------------
//--------------------------------------------
// the destructor
CircularDLL::~CircularDLL()
{
	destroy(top);
}

//--------------------------------------------
//--------------------------------------------
// Read a transaction file and insert the data into it
// after reading a set of data you can call any of the 
// insert functions to insert the node into the linked list 
/* use the following data to test your program
	76543	Mary	mary@google.com		19
	98765	Kathy	kathy@googlel.com		30
	16438	Flora	flora@google.com	25
	43260	Peter	peter@google.com		29
	87590	kim		kim@google.com		31
*/
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);
	}


}



//--------------------------------------------
//--------------------------------------------
// print the linked list
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;
}
//--------------------------------------------
//--------------------------------------------
// search for a particular student id in the list
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;
}

//--------------------------------------------
//--------------------------------------------
// creates a node and insert the node on the top of the
// linked list but after the first node. For example if the
// list constains 1 <--> 20 <-->13 <--> 4 <--> 5 <--> 6 <--> (links to the first node which is 1)
// after inserting 10, we should get:
// list constains 1 <--> 10 <--> 20 <-->13 <--> 4 <--> 5 <--> 6 <--> (links to the first node which is 1)

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;

	/*newNode->prev = top;
		if (top->next == top) // only one node in the list
		{
		newNode->next = top; // This will be the second node, and to make it circular its next will point to top.
		top->prev = newNode;
		top->next = newNode;
		}
		else // More than one node.
		{
		newNode->next = top->next;
		top->next->prev = newNode; // The second node's previous needs to point to this node, as it goes 'inbetween' the old second and top
		}
		top->next = newNode;*/
}
//--------------------------------------------
//--------------------------------------------
// creates a node and insert the node on the top of the
// linked list before the first node. For example if the
// list constains 1 <--> 20 <-->13 <--> 4 <--> 5 <--> 6 <--> (links to the first node which is 1)
// after inserting 10, we should get:
// list constains 10 <--> 1 <--> 20 <-->13 <--> 4 <--> 5 <--> 6 <--> (links to the first node which is 10)

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) // One node
	{
		newNode->before = top;
		newNode->next = top;
		top->next = newNode;
		top->before = newNode;
		top = newNode;
	}
	else // More than one
	{
		newNode->before = top->before;
		newNode->next = top;
		top->before = newNode;
		top = newNode;
	}

	/*newNode->prev = top->prev;
	newNode->next = top->next;

	top->prev = newNode;
	top->next = newNode;
	top = newNode;*/
	
}

	/*
	np->next = newNode;
			newNode->before = np;
			newNode->next = top;
			top->before = newNode;
			
	
	
	newNode->prev = top->prev;
	newNode->next = top->next;

	top->prev = newNode;
	top->next = newNode;
	top = newNode;*/


//--------------------------------------------
//--------------------------------------------
// creates a node and insert the node on the bottom of the
// linked list after the last node. For example if the
// list constains 1 <--> 20 <-->13 <--> 4 <--> 5 <--> 6 <--> (links to the first node which is 1)
// after inserting 10, we should get:
// list constains 1 <--> 20 <-->13 <--> 4 <--> 5 <--> 6 <--> 10 <-->(links to the first node which is 1)

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;
	}
}
//--------------------------------------------
//--------------------------------------------
// creates a node and insert the node on the bottom of the
// linked list before the last node. For example if the
// list constains 1 <--> 20 <-->13 <--> 4 <--> 5 <--> 6 <--> (links to the first node which is 1)
// after inserting 10, we should get:
// list constains 1 <--> 20 <-->13 <--> 4 <--> 5 <--> 10 <--> 6 <--> (links to the first node which is 1)

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) // A list of one
	{
		top = newNode;
	}
	lastNode->before = newNode;

}
//--------------------------------------------
//--------------------------------------------
// removes a node from the list based on the given student id 
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;


}

//--------------------------------------------
//--------------------------------------------
// copies one list into another
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;
	}
}

//--------------------------------------------
// deallocate the nodes in a linked list
void CircularDLL::destroy(NodePtr& top)
{
	NodePtr  curr, temp;

	curr = top;

	while (top != curr)
	{
		temp = top;
		top = top->next;
		delete temp;

	}

	top = NULL;
}

//--------------------------------------------
Posted
Updated 25-Nov-18 1:00am
v2
Comments
KarstenK 25-Nov-18 13:25pm    
write some test code to verify your solution.

1 solution

No.
I'm not wading through a large pile of code in search of "it doesn't work, probably".

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how you use it, then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900