Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

COLLEGE OF COMPUTER STUDIES

CCS007L
(COMPUTER PROGRAMMING 2)

EXERCISE

6
LINKED LIST

Student Name / Group


Name:
Name Role
Members (if Group):

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]

II. COURSE LEARNING OUTCOME/S (CLO)ADDRESSED BY THE LABORATORY EXERCISE


 Apply the fundamental principles of handling CString values, pointers and memory
allocation, and structures using C++in solving computing activities [CLO: 2]

III. INTENDED LEARNING OUTCOME/S (ILO) OF THE LABORATORY EXERCISE


At the end of this exercise, students must be able to:
 Perform linked list operation
 Iterate and process each data element from a given linked list.
 Have a full understanding on variation of Linked List
 Create some applied program using linked list
 Create a record management program using doubly linked list

IV. BACKGROUND INFORMATION


A linked list is a sequence of data structures, which are connected together via links.

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.

Linked List Representation


Linked list can be visualized as a chain of nodes, where every node points to the next
node.

CCS007L-Computer Programming 2 Page 2 of 6


CCS007L-Computer Programming 2 Page 3 of 6
As per the above illustration, following are the important points to be considered.
 Linked List contains a link element called first.
 Each link carries a data field(s) and a link field called next.
 Each link is linked with its next link using its next link.
 Last link carries a link as null to mark the end of the list.

Types of Linked List


Following are the various types of linked list.
 Simple Linked List − Item navigation is forward only.
 Doubly Linked List − Items can be navigated forward and backward.
 Circular Linked List − Last item contains link of the first element as next and the first
element has a link to the last element as previous.

Basic Operations

Following are the basic operations supported by a list.


 Insertion − Adds an element at the beginning of the list.
 Deletion − Deletes an element at the beginning of the list.
 Display − Displays the complete list.
 Search − Searches an element using the given key.
 Delete − Deletes an element using the given key.

CCS007L-Computer Programming 2 Page 4 of 6


V. GRADING SYSTEM / RUBRIC

Trait (Excellent) (Good) (Fair) (Poor)


Able to identify Able to identify Able to identify only Unable to
correctly all input correctly all input one input or output identify any input
and output and and output (22-14pts) and output
Requirement
provide alternative. (25-17pts) (20-11pts)
Specification(30pts)
(28-20pts)

Able to apply Able to apply Able to identify Unable to


required data type required data type required data type or identify required
or data structure or data structure data structure but data type
Data type(20pts)
and produce and produce does apply correctly (9-11pts)
correct results (18- partially correct (12-14pts)
20pts) results (15-17pts)
The program works The program works The program The program
and meets all and meets all produces correct produce s
specifications. Does specifications. results but does not incorrect results
Input
exception al Does some display correctly Does (9-11pts)
Validation(20pts)
checking for errors checking for errors not check for errors
and out-of- range and out of range and out of range data
data (18-20pts) data (15-17pts) (12-14pts)
Unable to run Able to run Able to run program Able to run
program (10pts) program but have correctly without any program
Free from syntax, logic error (8-9pts) logic error and correctly without
logic, and runtime display inappropriate any logic error
errors (10pts) output (6-7pts) and display
appropriate
output (5pts)
The program was The program was The program was The program was
delivered on time delivered after 5 delivered after 10 delivered after
(10pts) minutes from the minutes from the 15 (or more)
Delivery (10pts)
time required. (8- time required. (6- minutes from the
9pts) 7pts) time required.
(5pts)
Use of Comments Specific purpose is Specific purpose is Purpose is noted for No comments
(10pts) noted for each noted for each each function. (6- included. (5pts)
function, control function and 7pts)
structure, input control structure.
requirements, and (8-9pts)
output results.
(10pts)

CCS007L-Computer Programming 2 Page 5 of 6


VI. LABORATORY ACTIVITY

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;
}

cout<<"The player's who gets the maximum average: "<<endl;

while(head!=nullptr)
{
if(maxavg == head->average)
cout<<head->nickname<<endl;
head = head->next;
}
}

void printMinaverage(player *head)


{
float minavg = head->average;
player *temp = head;

while (temp! =nullptr)


{
if (minavg> temp->average)
minavg = temp->average;
temp = temp->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;

CCS007L-Computer Programming 2 Page 11 of


VII. QUESTION AND ANSWER

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.

What are the advantages of using linked lists?


You may simply do insertion and delete actions in this. That is, the node can be simply
inserted and deleted. At runtime, you may also decrease and grow the linked list. That is,
you may allocate and deallocate memory during runtime. It has an extremely rapid access
time
Whatand
are may be accessed atof
the disadvantages any moment
linked lists?without incurring memory expense.
The linked list takes more memory to store the contents than an array does since each
node of the linked list points to a pointer, requiring additional memory.
A linked list's nodes are extremely complex to traverse.

CCS007L-Computer Programming 2 Page 12 of


VIII. REFERENCES
 Zak, Dianne (2016). An Introduction to Programming with C++
 Deitel, Paul & Deitel, Harvey (2012). C++ How To Program, Eighth Edition
 https://www.cprogramming.com/tutorial/lesson15.html

CCS007L-Computer Programming 2 Page 13 of

You might also like