Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
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
C++
#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();
	
};


Resource linked_list code

C++
#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;
		}
	}
}
Posted
Updated 13-Jun-13 8:55am
v2

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.
 
Share this answer
 
Comments
m.r.m.40 13-Jun-13 16:15pm    
thank you,
Sergey Alexandrovich Kryukov 13-Jun-13 16:17pm    
OK, are you going to accept the advice by mv3 and mine formally (green button)? I really recommend you to follow them.
—SA
m.r.m.40 14-Jun-13 3:32am    
i did follow them.
nv3 14-Jun-13 3:44am    
Good and thank you. Feel free to repost your code, once you have completed your workover. We have all started that way and writing a good general purpose container class is not directly an easy job. So don't be afraid if you don't get it right on the first attempt.
Sergey Alexandrovich Kryukov 13-Jun-13 16:16pm    
My 5 for the explanation of the things which looks more convincing.
—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
 
Share this answer
 
Comments
m.r.m.40 13-Jun-13 16:14pm    
thanks,
Sergey Alexandrovich Kryukov 14-Jun-13 12:26pm    
You are very welcome.
—SA
Monjurul Habib 16-Jun-13 15:55pm    
5+
Sergey Alexandrovich Kryukov 16-Jun-13 22:35pm    
Thank you, Monjurul.
—SA

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