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

问题描述


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

推荐答案



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

11-02 18:27