I have attempted this for three days. What i have to do is create a linked list of soccer player. Each soccer player has the following information.
Player’s Name (maximum 15 characters) Player’s Jersey Number (integer) Points Scored by Player (integer)
I have to define a structure called Player to store the information about a soccer player. Apart from the above data member the structure should have another pointer member that can point to another structure of Player type. To create the linked list, follow the algorithm below:
Declare the head pointer and a previous node pointer and set them to NULL.
Dynamically create a Player structure and if this is the 1st node make the head and previous pointer pointing to the newly created structure.
Ask user to enter player info and store it in the structure. Set the pointer member to NULL.
If not the 1st node set the pointer member of the node pointed by the previous node point to the new node.
Ask user whether he/she wants to continue.
- If yes go to step 2 if not done with linked list creation.
After creating the linked list my program should then print a table that lists each player’s number, name, and points scored. The program must use a function name displayPlayer to print the table. The main program should pass the head node pointer to the function. The function should display the player’s information in single line in fields of appropriate width (use setw). The prototype of the function is void displayPlayer(Player *).
My program should not accept negative values for players’ numbers or point scored. In case user enters negative number my program should display appropriate message and ask the user for input again.
#include <iostream>
#include <cctype>
using namespace std;
struct Player
{
char name[16];
int jersey;
int points;
// declare the required members of the structure here
Player *next;
};
void displayPlayer(Player *); // prototype of the display function
int main()
{
Player *headptr;
Player *lastptr;
Player *newnode;
headptr = NULL;
lastptr = NULL;
//declare three pointers of Player type
//one pointer to point to head node; one to point to previous node
//and one to point to new node
do {
Player info;
cout << " Enter name then jersey, points ";
cin >> info.name;
cin >> info.jersey;
cin >> info.points;
newnode->
//dynamically create a new node of Player type and make the new node pointer point to it
//enter data in to the new node (name, jersy no, score)
//set the next field of new node to NULL
if ()
{
//make head node and previous node pointers equal to the new node pointer
} else {
//set the next member of of the previous node eqal to ne node pointer
//make previous node pointer point to the new new node
}
} while(user wants to continue);
// Call displayPlayer function with headnode pointer
}
void displayPlayer(Player *h)
{
while( not end of the list) {
//display node content
} // set h to next node
}
std::vector
. – Some programmer dude Dec 7 '12 at 5:02