链表,类,结构 [英] linked list, class, struct

查看:64
本文介绍了链表,类,结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b $猜猜我已经使用c ++实现了一个链表。

i在这里发布我的代码,如果你看到任何可以改进这段代码并更好地执行它的话,请告诉,

谢谢,



标题linked_list代码

Hi,
i guess i've implemented a linked list using c++.
i post my code here, if you see anything that could improve this code and perform it better, please tell,
thanks,

Header linked_list code

#include <string>
#include <iostream>
using namespace std;
class linked_list
{
private:
	struct linked_node
	{
		linked_node* link;
		int customercode/* unique */, price;
		string fname, lname, address, destination;
	};
	linked_node* head;

public:
	linked_list(){	head = NULL;	}//clear head

	bool isempty() const { return head==NULL; }
	void remove();
	void add();
	int check_code(int code);		
	void print_all();
	string get_string(string caller, string quest);
	int get_integer(string caller, string quest);

	void search_integer();
	void search_string();
	
};





资源linked_list代码





Resource linked_list code

#include "StdAfx.h"
#include <string>
#include "linked_list.h"
using namespace std;

/* ----- remove ----- */
void linked_list::remove()
{
	int code = get_integer("remove","custommer code");

	bool found = false;
	linked_node* lcursor = head;
	linked_node* cursor = lcursor->link;
	while(cursor->link != NULL)//find entered code
	{		
		//check all nodes but the last one
		if(cursor->customercode == code)
		{
			found = true;
			lcursor->link=cursor->link;
			delete cursor;
			return;
		}
		else found = false;
		lcursor = cursor;
		cursor = cursor->link;
	}
	//check last node
	if(cursor->customercode == code)
		{
			found = true;
			lcursor->link=cursor->link;
			delete cursor;
			return;
		}
		else found = false;
	if (found == false)
		return;
}

/* ----- add ----- */
void linked_list::add()
{
	linked_node* t = new linked_node;

	if(isempty())//new list
	{				
		t->fname = get_string("add","first name");
		t->lname = get_string("add","last name");
		t->address = get_string("add","address");
		t->destination = get_string("add","destination");
		t->customercode = get_integer("add","custommer code");
		t->price = get_integer("add","price");
		/* head */head= t;
		t->link = NULL;
	}
	else//valued list
	{
		t->fname = get_string("add","first name");
		t->lname = get_string("add","last name");
		t->address = get_string("add","address");
		t->destination = get_string("add","destination");
		t->customercode = check_code( get_integer("add","custommer code"));
		t->price = get_integer("add","price");
		t->link = NULL;

		linked_node* cursor;
		cursor = head;
		while(cursor->link != NULL)//find new node position
			cursor = cursor->link;
		cursor->link = t;
	}
}

/* ----- print_all ----- */
void linked_list::print_all()
{
	linked_node* cursor;
	cursor = head;
	int i=0;
	while(cursor->link !=NULL)
	{
		i++;		
		cout<<endl<<i<<".\t";
		cout<<cursor->fname<<" "<<cursor->lname<<" "<<cursor->address<<" "<<cursor->customercode<<" "<<cursor->destination<<" "<<cursor->price<<endl;
		cursor = cursor->link;
	}
	/* last node */
	i++;
	cout<<endl<<i<<".\t";
	cout<<cursor->fname<<" "<<cursor->lname<<" "<<cursor->address<<" "<<cursor->customercode<<" "<<cursor->destination<<" "<<cursor->price<<endl;
}

/* ----- get_string ----- */
string linked_list::get_string(string caller, string quest)
{
	cout<<endl<<"Enter "<< quest <<" to "<<caller<<" :"<<endl;
	string str;
	cin>>str;
	return str;
}

/* ----- get_integer ----- */
int linked_list::get_integer(string caller, string quest)
{
	cout<<endl<<"Enter "<< quest <<" to "<<caller<<" :"<<endl;
	int integer;
	cin>>integer;
	return integer;
}

/* ----- check_code ----- */
int linked_list::check_code(int code)
{
	bool found = false;
	linked_node* cursor = head;
	while(cursor->link != NULL)//find entered code
	{		
		//check all nodes but the last one
		if(cursor->customercode == code)
		{
			found = true;
			cout<<endl<<"entered code is used before, please enter another :"<<endl;
			get_integer("add","custommer code");
		}
		else found = false;
		cursor = cursor->link;
	}
	//check last node
	if(cursor->customercode == code)
		{
			found = true;
			cout<<endl<<"enteredcode is used before, please enter another :"<<endl;
			get_integer("add","custommer code");
		}
		else found = false;
	if (found == false)
		return code;
}

/* ----- search_string ----- */
void linked_list::search_string()
{
	string field = get_string("search","field");

	bool found = false;	
	linked_node* cursor = head;
	int i=0;
	while(cursor->link != NULL)//find entered field
	{		
		//check all nodes but the last one
		if((cursor->fname == field) || (cursor->lname == field) || (cursor->address == field) || (cursor->destination == field))
		{
			found = true;
			i++;
			cout<<endl<<i<<". "<<cursor->fname;
			cout<<endl<<"   "<<cursor->lname;
			cout<<endl<<"   "<<cursor->address;
			cout<<endl<<"   "<<cursor->customercode;
			cout<<endl<<"   "<<cursor->destination;
			cout<<endl<<"   "<<cursor->price;
		}		
		cursor = cursor->link;
	}
	//check last node
	if((cursor->fname == field) || (cursor->lname == field) || (cursor->address == field) || (cursor->destination == field))
		{
			found = true;
			i++;
			cout<<endl<<i<<". "<<cursor->fname;
			cout<<endl<<"   "<<cursor->lname;
			cout<<endl<<"   "<<cursor->address;
			cout<<endl<<"   "<<cursor->customercode;
			cout<<endl<<"   "<<cursor->destination;
			cout<<endl<<"   "<<cursor->price;		
		}		
	if (found == false)	cout<<endl<<"entered data was not found!"<<endl;
}

/* ----- search_integer ----- */
void linked_list::search_integer()
{
	int field = get_integer("search","field");

	bool found = false;	
	linked_node* cursor = head;
	int i=0;
	while(cursor->link != NULL)//find entered field
	{		
		//check all nodes but the last one
		if((cursor->customercode == field) || (cursor->price == field))
		{
			found = true;
			i++;
			cout<<endl<<i<<". "<<cursor->fname;
			cout<<endl<<"   "<<cursor->lname;
			cout<<endl<<"   "<<cursor->address;
			cout<<endl<<"   "<<cursor->customercode;
			cout<<endl<<"   "<<cursor->destination;
			cout<<endl<<"   "<<cursor->price;
		}		
		cursor = cursor->link;
	}
	//check last node
	if((cursor->customercode == field) || (cursor->price == field))
		{
			found = true;
			i++;
			cout<<endl<<i<<". "<<cursor->fname;
			cout<<endl<<"   "<<cursor->lname;
			cout<<endl<<"   "<<cursor->address;
			cout<<endl<<"   "<<cursor->customercode;
			cout<<endl<<"   "<<cursor->destination;
			cout<<endl<<"   "<<cursor->price;
		}		
	if (found == false)	cout<<endl<<"entered data was not found!"<<endl;
}

/* ----- main ----- */
void main()
{
	cout<<" LINKED LIST project :\ntaxy service program example using linked list"<<endl;	
	int order=0;
	linked_list l;//defines a list
	while(1)
	{
		cout<<"Linked List operation's :"<<endl;
		cout<<" ----------------------- "<<endl;
		cout<<"1. add\n   creat if first insertion"<<endl;
		cout<<"2. remove"<<endl;
		cout<<"3. search"<<endl;
		cout<<"4. print_all"<<endl;
		cout<<"5. exit"<<endl;
		cin>>order;
		switch(order)
		{
			case 1:					
				l.add();
				break;
			case 2:
				if(!l.isempty())	l.remove();
				else cout<<"list is empty"<<endl;
				break;
			case 3:
				if(l.isempty())	cout<<"list is empty"<<endl;					
				else
				{	cout<<"1. search by name / family / address / destination"<<endl;
					cout<<"2. search by custommer code / price"<<endl;
					int sorder=0;
					cin>>sorder;
					if(sorder==1)	l.search_string();
					else if(sorder==2)	l.search_integer();
					else cout<<endl<<"wrong entry"<<endl;
				}
				break;
			case 4:
				if(!l.isempty())	l.print_all();
				else cout<<"list is empty"<<endl;				
				break;
			case 5:
				return;
			default :
				cout<<" invalid entry, try again! "<<endl;
				break;
		}
	}
}

推荐答案

我必须完全同意谢尔盖先前所说的话发帖(即使你不喜欢)。这是一个链表,但不可重复使用。您已将其设计为特殊用途,并且仅满足此目的。如果我需要列表节点中的其他字段怎么办?好的,我可以修改你的源代码。如果我的程序需要30种不同类型的列表(对于大型产品来说并不罕见)怎么办?你看到你内置的限制?并且永远不要在基本数据结构类或算法中进行任何流I / O;这绝对不行。



查看STL或MFC或任何其他广泛传播的库中定义的数据结构。您可以从研究他们的源代码以及他们如何设计真正可重用的类或模板中学到很多东西。
I must fully agree to what Sergey said in the previous post (even if you don't like that). This is a linked list, but it is not re-usable. You have designed it for a special purpose and it fulfills only this single purpose. What if I need some other fields in my list nodes? Ok, I could modify your source code. What if my program needs 30 different types of lists (not uncommon for larger products)? You see the limitations you built in? And never do any stream I/O in a basic data structure class or algorithm; that's an absolute no go.

Take a look at the data structures defined in STL or MFC or any other widely spread library. You can learn a lot from studying their source code and how they managed to design truly re-usable classes or templates.


它远非性能问题。首先,您应该将链表制作一个纯列表,即数据结构。它的操作中不应该有一个字符串,特别是那些硬编码的立即常量。此外,该类不应该包含任何IO操作。无论它的操作有多好或多坏,都不应该这样指定。你不想创建一个只能用于控制台的列表吗?我不这么认为。



-SA
It's really far from performance concern. First of all, you should make a linked list a pure list, the data structure. It should not have a single string in its operations, especially those hard-coded immediate constants. Also, the class should not have any IO operations in it. No matter how good or bad its operation is, it should not be specified like that. Don't you want to create a list which can only work with a console? I don't think so.

—SA


这篇关于链表,类,结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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