Friday , 22 November 2024

CS301 Assignments # 1 Solution Spring 2012

We need to store a number of Cricket Players and their Scores in a list using Linked List Data Structure. The data of the players will comprise of their Names and Scores, which means each Node of the Linked List will contain Player Name, Player Score and Next Pointer as follows,

You need to write a C++ Program which contains the following two classes and main function

  1. Player Class (means Node Class): This class should define three private variables for Player Name, Player Score and Next pointer, this class should also define Constructor, Getter and Setter functions for these variable as inline functions (means define these functions within class body).
  2. PlayerList Class (means Linked List Class): This Class should define private variables for Header, CurrentPlayer ( means Head pointer, CurrentNode Node) and an int variable which count the number of Players in the Linked List.

The Class should also declare the following methods/Functions,

Constructor(): Default Constructor of the Class.

Add_NewPlayer(): This method should add new player only at the end of list.

Display(): This method should Print the Players information (Names and Score) in the list.

ListLength(): This method should return the total number of Players added in the list.

            getTotalScore(): This method should return (print) Total score (e.g score of player1+player2.. etc).

Main Function():  In the main function define an object of the PlayerList class and call the Add_NewPlayer method with this object multiple times (At least two times), then call the Display(), getTotalScore and ListLength functions with this object, your output should look like this.

/****************** Assignment No. 1 : Solution **********************\

#include<iostream.h>
#include<conio.h>

/////////Node Class//////////

class Player
{
private:
char *PlayerName; ///////Node Data/////////
int PlayerScore; /////////////////////////
Player *NextPlayer; // Node Next Pointer/////

public:

///// Node Class Constructor//////
Player()
{
PlayerName = “”;
PlayerScore = 0;
NextPlayer = NULL;
}

//////Node Class Setter Functions///////

void SetPlayerName(char *P_name)
{
PlayerName = P_name;
}
void SetPlayerScore(int P_score)
{
PlayerScore = P_score;
}
void SetNextPlayer(Player *P)
{

NextPlayer = P;
}

//////Node Class Getter Functions///////


char* getPlayerName()
{
return PlayerName;
}
int getPlayerScore()
{
return PlayerScore;
}
Player* getNextPlayer()
{
return NextPlayer;
}
};

///// Link List Class////////

class PlayerList
{
private:
Player *CurrentPlayer;
Player *HeadPlayer;
int Player_Count;
int Total_Score;

public:
PlayerList();
void Add_NewPlayer(char*, int);
int ListLength();
void Display();
int getTotalScore();

};

PlayerList::PlayerList()
{
Player *Pl = new Player();
HeadPlayer = Pl;
CurrentPlayer = NULL;
Player_Count = 0;
Total_Score = 0;
}

void PlayerList::Add_NewPlayer(char *P_name, int score)
{
Player *NewPlayer = new Player();

NewPlayer->SetPlayerName(P_name);
NewPlayer->SetPlayerScore(score);
NewPlayer->SetNextPlayer(NULL);

/////When List is empty//////
if(CurrentPlayer == NULL)
{
HeadPlayer = NewPlayer;
CurrentPlayer = NewPlayer;
}
////When List has some Players/Nodes
else

CurrentPlayer->SetNextPlayer(NewPlayer);
CurrentPlayer = NewPlayer;

Player_Count++;
Total_Score = Total_Score + NewPlayer->getPlayerScore();
}

void PlayerList::Display()
{
CurrentPlayer = HeadPlayer;

cout<<“Player Names ” <<‘\t'<<“Score ” <<endl <<endl;
while (CurrentPlayer != NULL)
{

cout<<CurrentPlayer->getPlayerName()<<‘\t'<<CurrentPlayer->getPlayerScore()<<endl;

CurrentPlayer = CurrentPlayer->getNextPlayer();

}
}

int PlayerList::ListLength()
{
return Player_Count;
}

int PlayerList::getTotalScore()
{
return Total_Score;
}

////// Main Program/Function/////////

main ()
{

PlayerList P_list;

P_list.Add_NewPlayer(“Muhammad Hafeez”, 110);
P_list.Add_NewPlayer(“Nasir Jamshad”, 40);
P_list.Add_NewPlayer(“Younas Khan”, 52);
P_list.Add_NewPlayer(“Misbaul Haq”, 45);
P_list.Add_NewPlayer(“Umar Akmal”, 30);
P_list.Add_NewPlayer(“Shahid Afridi”, 15);
P_list.Add_NewPlayer(“Umar Gul”, 10);
P_list.Add_NewPlayer(“Saeed Ajmal”, 20);

cout<<“——Players Records——-” <<endl <<endl;

P_list.Display();

cout <<endl <<endl;

cout<<“Total Score: ” <<‘\t’ <<P_list.getTotalScore() <<endl <<endl;
cout<<“Total Players in the List = “<<P_list.ListLength();

getche();
return 0;
}

/****************** DOWNLOAD FROM HERE **********************\

 


CS301_Assignments_Solution_1_Spring_2012

Check Also

CS301 unSolved Final Term papers Mega Colletionction by Arslan Ali

      FINALTERM  EXAMINATION Spring 2010 CS301- Data Structures Time: 90 min Marks: 58 …

Leave a Reply

Your email address will not be published. Required fields are marked *

*