user1779998
user1779998

Reputation: 31

Structures that involve linked list and pointers

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:

  1. Declare the head pointer and a previous node pointer and set them to NULL.

  2. Dynamically create a Player structure and if this is the 1st node make the head and previous pointer pointing to the newly created structure.

  3. Ask user to enter player info and store it in the structure. Set the pointer member to NULL.

  4. If not the 1st node set the pointer member of the node pointed by the previous node point to the new node.

  5. Ask user whether he/she wants to continue.

  6. 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
} 

Upvotes: 1

Views: 1515

Answers (2)

Beta
Beta

Reputation: 99094

This will take a few iterations.

The Player struct you have is pretty good for a start:

struct Player
{
  char name[16];
  int jersey;
  int points;

  Player *next;
};

This covers the basics. Right now this struct has only data members, no methods; it's a data container that can't do anything-- other things operate on it. Later on you may find it convenient to give it methods. What next?

EDIT:

Declaring the pointers in main. Again, you already have it:

int main()
{
  Player *headptr;
  Player *lastptr;
  Player *newnode;
  ...
}

This may turn out not to be the most convenient place to declare newnode. The practice of declaring all variables at the top of a routine comes from the days when stack memory was a resource that had to be carefully husbanded; nowadays it's more important to have code that's easy to read, so variables shouldn't be declared before they're needed. But we can worry about that later. What next?

EDIT:

Dynamically creating a new Player:

new Player;

That creates a new Player-- but immediately loses it. When you inflate a balloon, it behooves you to hold onto the string:

newnode = new Player;

The new operator returns a pointer to the new object, it's address. Now that we've retained it in newnode, we can do things with it:

newnode->jersey = 34;
cout << newnode->jersey << endl;

Now what?

EDIT:

Now things get dangerous. Character arrays are a pain in the neck, always have been. And getting input by `cin >> ...` is dangerous, and should be avoided in Real Life. But for now we can get away with:

newnode = new Player;
cout << "Enter name, then jersey number, then number of points:" << endl;
cin >> newnode->name >> newnode->jersey >> newnode->points;
cout << newnode->name << " " << newnode->jersey << " scored " << newnode->points << endl;

Note that this code could go into the Player struct as a method; maybe we'll do that later if we have time. Next?

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409146

Okay, let me do the pen-and-paper layout, but on the computer instead of on real pen and paper...

When the list is empty, your head and tail pointers abe both pointing to NULL:

head ---> NULL

tail ---> NULL

Now lets insert a node A into the empty list:

head --\     +------+
        }--> |  A   |
tail --/     | next | ---> NULL
             +------+

Both the head and the tail points to the one and only node, while the nodes next pointer points to NULL (meaning it's the last node in the list).

If we now insert a new node B at the end of the list, it will look like this:

          +------+
head ---> |  A   |
          | next | ---------\     +------+
          +------+           }--> |  B   |
                     tail --/     | next | ---> NULL
                                  +------+

Now head still points to A, but the tail points to B, as well as the next link from A. The next link in B is pointing to NULL indicating this is the last node in the list.

If we add a node C to the head to the list, it will now look like this:

          +------+
head ---> |  C   |      +------+
          | next | ---> |  A   |
          +------+      | next | ---------\     +------+
                        +------+           }--> |  B   |
                                   tail --/     | next | ---> NULL
                                                +------+

Adding to the head requires you to only change the head pointer to point to the new node, and the next pointer of C to point to the old head.

Hope this helps you understand how linked lists works.

Upvotes: 3

Related Questions