Professional Documents
Culture Documents
CCS0007 - Laboratory Exercise 6
CCS0007 - Laboratory Exercise 6
CCS007L
(COMPUTER PROGRAMMING 2)
EXERCISE
6
LINKED LIST
Section:
Professor:
I. PROGRAM OUTCOME/S (PO) ADDRESSED BY THE LABORATORY EXERCISE
Design, implement and evaluate computer-based systems or applications to meet
desired needs and requirements. [PO: C]
Linked List is a sequence of links which contains items. Each link contains a connection
to another link. Linked list is the second most-used data structure after array. Following are the
important terms to understand the concept of Linked List.
Link − Each link of a linked list can store a data called an element.
Next − Each link of a linked list contains a link to the next link called Next.
LinkedList − A Linked List contains the connection link to the first link called First.
Basic Operations
INSTRUCTIONS:
Copy your source codes to be pasted in this document as well as a screen shot of your running
output.
ACTIVITY 6.1: Player’s records using linked list
Redo your work in Laboratory Activity 3 where you will record and compute for the
scores of 5 players, this time, using linked lists. The information of each player contains:
Nickname, Age and two best played scores.
The program will prompt the user to choose the operation of records from a menu as
shown below:
==============================================
MENU
==============================================
1. Add record
2. View player’s records
3. Compute for the average
4. Show the player(s) who gets the max average.
5. Show the player(s) who gets the min average.
6. Exit
#include <iostream>
#include <iomanip>
using namespace std;
//structure of player
struct player
{
string nickname;
int age;
int score1;
int score2;
float average;
player *next;
};
//function to print the menu
void printMenu()
{
cout<<"========================================"<<endl;
cout<<" MENU "<<endl;
cout<<"========================================"<<endl;
cout<<"1. Add record"<<endl;
CCS007L-Computer Programming 2 Page 6 of 6
cout<<"2. View player's record"<<endl;
cout<<"3. Compute for the average"<<endl;
cout<<"4. Show the player's who gets the max average"<<endl;
cout<<"5. Show the player's who gets the min average"<<endl;
cout<<"6. Exit"<<endl;
}
//function to print the data for the player
void printPlayer(player *head)
{
cout<<"Nickname Age Two-best-scores Average"<<endl;
while(head!=nullptr)
{
cout<<left<<setw(10)<<head->nickname;
cout<<left<<setw(5)<<head->age;
cout<<left<<setw(9)<<head->score1;
cout<<left<<setw(9)<<head->score2;
cout<<head->average<<endl;
head = head->next;
}
}
//function to Add a player to the list
void addPlayer(player **head)
{
//create a new node
player *newnode = new player;
cout<<"Enter Nickname of player: ";
cin>>newnode->nickname;
cout<<"Enter Age: ";
cin>>newnode->age;
cout<<"Enter best score #1: ";
cin>>newnode->score1;
cout<<"Enter best score #2: ";
cin>>newnode->score2;
newnode->next = nullptr;
//check is the list empty
if(*head==nullptr)
{
*head = newnode;
return;
}
//traverse the list
player *temp = *head;
while(temp->next!=nullptr)
CCS007L-Computer Programming 2 Page 7 of 6
{
temp = temp->next;
}
//add the newnode at the end of the list
temp->next = newnode;
}
//function to compute the average score
void computeAverage(player *head)
{
while(head!=nullptr)
{
head->average = (head->score1 + head->score2)/2.0;
head = head->next;
}
}
//function to print the player's who gets the min average
void printMaxAverage(player *head)
{
float maxavg = head->average;
player *temp = head;
//calculate the maximum average
while(temp!=nullptr)
player *next;
};
//function to print the menu
void printMenu()
{
cout<<"========================================"<<endl;
cout<<" MENU "<<endl;
cout<<"========================================"<<endl;
cout<<"1. Add record"<<endl;
cout<<"2. View player's record"<<endl;
cout<<"3. Compute for the average"<<endl;
cout<<"4. Show the player's who gets the max average"<<endl;
cout<<"5. Show the player's who gets the min average"<<endl;
cout<<"6. Exit"<<endl;
}
//function to print the data for the player
void printPlayer(player *head)
{
cout<<"Nickname Age Two-best-scores Average"<<endl;
while(head!=nullptr)
CCS007L-Computer Programming 2 Page 8 of 6
{
cout<<left<<setw(10)<<head->nickname;
cout<<left<<setw(5)<<head->age;
cout<<left<<setw(9)<<head->score1;
cout<<left<<setw(9)<<head->score2;
cout<<head->average<<endl;
head = head->next;
}
}
//function to Add a player to the list
void addPlayer(player **head)
{
//create a new node
player *newnode = new player;
cout<<"Enter Nickname of player: ";
cin>>newnode->nickname;
cout<<"Enter Age: ";
cin>>newnode->age;
cout<<"Enter best score #1: ";
cin>>newnode->score1;
cout<<"Enter best score #2: ";
cin>>newnode->score2;
newnode->next = nullptr;
//check is the list empty
if(*head==nullptr)
{
*head = newnode;
return;
}
//traverse the list
player *temp = *head;
while(temp->next!=nullptr)
{
temp = temp->next;
}
//add the newnode at the end of the list
temp->next = newnode;
}
//function to compute the average score
void computeAverage(player *head)
{
while(head!=nullptr)
{
CCS007L-Computer Programming 2 Page 9 of 6
head->average = (head->score1 + head->score2)/2.0;
head = head->next;
}
}
//function to print the player's who gets the min average
void printMaxAverage(player *head)
{
float maxavg = head->average;
player *temp = head;
//calculate the maximum average
while(temp!=nullptr)
{
if(maxavg<temp->average)
maxavg = temp->average;
temp = temp->next;
}
while(head!=nullptr)
{
if(maxavg == head->average)
cout<<head->nickname<<endl;
head = head->next;
}
}
}
cout<<"The player's who gets the minimum average: "<<endl;
while (head!=nullptr)
CCS007L-Computer Programming 2 Page 10 of
{
if (minavg == head->average)
cout<<head->nickname<<endl;
head = head->next;
}
}
int main ()
{
int op;
player *head = nullptr;
while(1)
{
printMenu();
cout<<"Enter an Option(1-6): ";
cin>>op;
if(op==6) break;
switch(op)
{
case 1:
addPlayer(&head);
break;
case 2:
printPlayer(head);
break;
case 3:
computeAverage(head);
break;
case 4:
printMaxAverage(head);
break;
case 5:
printMinAverage(head);
break;
}
cout<<endl;
return 0;
Briefly answer the questions below. Avoid erasures. For group activity, specify the name of
GROUP MEMBER/s who answered the question. Do not forget to include the source for all
NON-ORIGINAL IDEAS.