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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
usingnamespace 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
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.
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.