Need a loop to display nodes of a linked list

Hello, the objective of the code is to have 11 players in a linked list. I have to have the the console output a list of the players who have a certain position at the end and can't quite figure out the loop to do it. I've tried a few different methods and I guess this is just something I don't realize right now

Code I have problems with
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
  	void ClubList::find_in_list(char*temp)
{
	int x = 1;
	strcmp(temp,nodes->playing_position);
	
	do
		{
		//cout <<"\n Position-> "<<ClubList::nodes->playing_position;
		x=strcmp(temp,nodes->playing_position);
		if (x == 0)
		{
			cout<<"\n Name of Player::"<< ClubList::nodes->Name;
			cout<<"\n Age of Player::"<<ClubList::nodes->age;
			cout<<"\n Registration_Noof Player::"<< ClubList::nodes->Registration_No;
			cout<<"\n teamname of Player::"<<ClubList::nodes->team<<endl;
			cout<<"\n Playing_position of Player::"<< ClubList::nodes->playing_position;

			ClubList::nodes = ClubList::nodes->linkF;
			

				if(x!=0)
			{
				cout<<"\nNot Found";
			}
		}
		
		/*else 
		{
			ClubList::nodes = ClubList::nodes->linkF;

		}*/

		else if(x!=0)
	{
		cout<<"\nNot Found";
	}

		}
	
	
while((ClubList::nodes != NULL) && (x==0));


Rest of 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
using namespace std;

class Node;
class ClubList
{
private : 
	void print ();

public:
	Node*nodes;
	ClubList (){ nodes=0;}
	void show_listOfMembers(int dir);
	void find_in_list(char*temp);
};


class Node
{
	public:
	char* Name;  // can also use pointer functions
	char* team;
	char* playing_position;
	int Registration_No;
	int age;
	Node* linkB; //pointer to prior nodes
	Node* linkF;//pointer to next nodes
	Node()
	{
	
		
		linkB=0;	//constructor
		linkF=0;
	}
			// declartions 
		char*get_Name();
		char*get_team();
		char*get_playing_position();
		int get_Registration_No();
		int get_age();

		void set_Name(char * );
		char* set_team();
		void set_playing_position(char*);
		int set_Registration_No(int);
		void set_age(int);
};

	ClubList*l1; // club list is ponted to l1, L1 shows the list of information

	// definition
	char*Node:: get_Name() // get name from node class and return the name
	{
		return (Name);
	}

	void Node::set_Name (char* N)
	{
		Name= N;
	}

	void  main()
	{
		cout << "Programmer: Brian Geoghegan C12741701 \nDoubly linked list method for a football team\n" <<endl;
    Node *n1, *n2, *n3, *n4, *n5, *n6, *n7, *n8, *n9,*n10,*n11;
	
	int direction;
	char choice;

	l1=new(ClubList);


	n1=new(Node);
	n2=new(Node);
	n3=new(Node);
	n4=new(Node);
	n5=new(Node);
	n6=new(Node);
	n7=new(Node);
	n8=new(Node);
	n9=new(Node);
	n10=new(Node);
	n11=new(Node);

 	n1->linkB=NULL;
	n1->Name=" Mahon";
	n1->set_Name(" Derick");
	n1->get_Name();
	n1->age = 22 ;
	n1->Registration_No = 2145 ;
	n1->team = " Thrones";
	n1->playing_position = "striker";
	n1->linkF = n2;

	n2->linkB=n1;
	n2->Name=" Delev ";
	n2->set_Name(" Todor ");
	n2->get_Name();
	n2->age = 21;
	n2->Registration_No = 4331 ;
	n2->team = " Thrones";
	n2->playing_position = "defender";
	n2->linkF = n3;

	n3->linkB=n2;
	n3->Name="Johnson"; //name
	n3->set_Name("Alan"); // first name
	n3->get_Name();
	n3->age = 19;
	n3->Registration_No = 1192 ;
	n3->team = " Thrones";
	n3->playing_position = "midfield";
	n3->linkF = n4;

	n4->linkB=n3;
	n4->Name="McGuire"; //name
	n4->set_Name("Micheal"); // first name
	n4->get_Name();
	n4->age = 19;
	n4->Registration_No = 1192 ;
	n4->team = " Thrones";
	n4->playing_position = "striker";
	n4->linkF = n5;

	n5->linkB=n4;
	n5->Name="Cumber"; //name
	n5->set_Name("Deli"); // first name
	n5->get_Name();
	n5->age = 25;
	n5->Registration_No = 1192 ;
	n5->team = " Thrones";
	n5->playing_position = "midfield";
	n5->linkF = n6;

	n6->linkB=n5;
	n6->Name="Little "; //name
	n6->set_Name("Omar"); // first name
	n6->get_Name();
	n6->age = 24;
	n6->Registration_No = 1192 ;
	n6->team = " Thrones";
	n6->playing_position = "defender";
	n6->linkF = n7;

	n7->linkB=n6;
	n7->Name=" God"; //name
	n7->set_Name("Dorman"); // first name
	n7->get_Name();
	n7->age = 23;
	n7->Registration_No = 1192 ;
	n7->team = " Thrones";
	n7->playing_position = "defender";
	n7->linkF = n8;

	n8->linkB=n7;
	n8->Name=" Henley"; //name
	n8->set_Name("Dylan"); // first name
	n8->get_Name();
	n8->age = 18;
	n8->Registration_No = 1192 ;
	n8->team = " Thrones";
	n8->playing_position = "defender";
	n8->linkF = n9;

	n9->linkB=n8;
	n9->Name=" stanley"; //name
	n9->set_Name("Patrick"); // first name
	n9->get_Name();
	n9->age = 27;
	n9->Registration_No = 1192 ;
	n9->team = " Thrones";
	n9->playing_position = "defender";
	n9->linkF = n10;

	n10->linkB=n9;
	n10->Name=" MacJohn"; //name
	n10->set_Name("Lenny"); // first name
	n10->get_Name();
	n10->age = 26;
	n10->Registration_No = 1192 ;
	n10->team = " Thrones";
	n10->playing_position = "defender";
	n10->linkF = n11;


	n11->linkB=n10;
	n11->Name=" Kenuy"; //name
	n11->set_Name("Charles"); // first name
	n11->get_Name();
	n11->age = 20;
	n11->Registration_No = 1192 ;
	n11->team = " Thrones";
	n11->playing_position = "defender";
	n11->linkF = NULL;





	printf("\n\nShow linked List Forward (F) or Backward (B): ");
	scanf("%c",&choice);

	if (choice=='F')
	    {
	    direction=1;
		l1->nodes=n1;
		l1-> show_listOfMembers(direction);
	    }
	    else
		{
			if (choice=='B')
			{
				direction=0;
				l1->nodes=n3;
				l1-> show_listOfMembers(direction);
			}

		    else
			 {
				printf("INVALID ENTRY !");
				exit(1);

			}
		}

	char position [20];
	cout<<"Find player with position?";
	cin>>position;
	l1->nodes=n11;
	l1->find_in_list(position);


		delete n1;
		delete n2;
		delete n3;
		delete n4;
		delete n5;
		delete n6;
		delete n7;
		delete n8;
		delete n9;
		delete n10;
		delete n11;
		delete l1;
	}
	void ClubList::print()
	{
		cout<<nodes->Name;
		cout<<nodes->age;
		
		cout<<nodes->Registration_No;
		cout<<nodes->team;
		cout<<nodes->playing_position;
		cout <<"" ;
	}

		void ClubList::show_listOfMembers(int dir)
		{
			if (dir==1)
		{
	   
		while(ClubList::nodes != NULL)
	   {
			cout<<"\n NAME :: "<<ClubList::nodes->Name;
			cout<<"\n AGE :: "<<ClubList::nodes->age;
			cout<<"\n REG. NO :: "<<ClubList::nodes->Registration_No;
			cout<<"\n TEAM NAME :: "<<ClubList::nodes->team;
			cout<<"\n PLAYING POSITION :: "<<ClubList::nodes->playing_position;
			
			cout<<"\n";
			
			ClubList::nodes = ClubList::nodes->linkF;
	   }
	   printf("\n");
	   }

	if (dir==0){
		while(ClubList::nodes != NULL)
	   {
			cout<<ClubList::nodes->Name;
			cout<<"\n"<<ClubList::nodes->age;
			cout<<"\n"<<ClubList::nodes->Registration_No;
			cout<<"\n"<<ClubList::nodes->team;
			cout<<"\n"<<ClubList::nodes->playing_position;

			ClubList::nodes = ClubList::nodes->linkB;
	   }
	   printf("\n");
	   }

	}


Thank you in advance :),

Also, the code currently displays the node at the bottom of the list only. If I type 'defender' in for example, only the bottom Node displays and the program ends. I need it to display all defenders etc. Thank you again
Last edited on
A function with "find" in its name probably should not be doing anything with standard input/output - its job is to "find" something and return that information to the caller.
Hmm good point, I should be displaying the nodes I guess. I'll try something else thank you :)
Does this have to be a linked list, or have you been asked to do a collection of exactly 11 players? In other words, can the number of players be more than 11? Less than 11? If it's exactly 11 then a linked list is not appropriate. An array of vector would make a lot more sense.

Your code is confusing because you've created a linked list, but then you're dealing with exactly 11 well-defined items. With a traditional linked list, you create a method to add a node to the list and the method adjusts the pointers. The destructor deletes the items, etc.

So let us know if you really need a linked list, or a collection of 11 items.
Topic archived. No new replies allowed.