有人可以查看关于循环双向链表的代码吗? [英] Could somebody to check my code about circular doubly linked list?

查看:80
本文介绍了有人可以查看关于循环双向链表的代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个代码循环双链表。我认为这不好。



有人可以查看我的代码吗?



我试过的:



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"

//--------------------------------------------
//--------------------------------------------
// 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;
}

//--------------------------------------------

推荐答案

没有。

我没有涉及大量代码,以寻找它可能无法正常工作。



所以,这取决于你。

幸运的是,你有一个工具可以帮助你找到正在发生的事情:调试器。如果您不知道如何使用它,那么快速GoogleVisual Studio调试器应该会为您提供所需的信息。



在断点上设置断点函数的第一行,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
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!


这篇关于有人可以查看关于循环双向链表的代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆