linked list, class, struct

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  #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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#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;
		}
	}
}
Topic archived. No new replies allowed.