Question: #include #include #include using namespace std; // creating a structure which will hold all the student records. struct StudentRecord { string firstName; string lastName; int
#include
#include
#include
using namespace std;
// creating a structure which will hold all the student records.
struct StudentRecord {
string firstName;
string lastName;
int oduUin;
string dateOfBirth;
double gpa;
StudentRecord *next;
};
StudentRecord *head = NULL;
void free_list()
{
// We will create a current node and assign head to it, then we will iterate the node through the linked list and delete all the records stored in the linked list
StudentRecord *current = head;
while(current!=NULL) // as I am considering tail->next = NULL
{
head->next = current->next;
current->next = NULL;
free(current);
current = head->next;
}
head = NULL;
}
void display_data()
{
cout
cout
// We will create a node named start and will iterate it through the whole linked list and display the data
StudentRecord *start = head;
if (!start) {
cout
return;
}
while(start) {
cout firstName
cout lastName
cout oduUin
cout dateOfBirth
cout gpa
start = start -> next;
}
}
StudentRecord *get_data()
{
//creating a temporary node in which we will store all the student records and return the temporary node in the end of the function
StudentRecord *rec = new StudentRecord;
cout
cout
cout
cin >> rec->firstName;
cout
cin >> rec->lastName;
cout
cin >> rec->oduUin;
cout
cin >> rec->dateOfBirth;
cout
cin >> rec->gpa;
rec->next = head;
return rec;
}
void add_data(StudentRecord *current)
{
// We will store the address of the present head node in the next field of the current node and later we will make the current node as head node
current->next=head; // store the address of the pointer head(second field)
head = current;
}
void search(double key)
{
// We will iterate the head through the linked list until it finds the required variable or until the end of linked list
while (head != NULL)
{
if (head->oduUin == key)
{
cout
// coutuin
coutfirstName
coutlastName
coutdateOfBirth
coutgpa
return;
}
head = head->next;
}
cout
}
void processMenu()
{
// creating current node for StudentRecord struct
StudentRecord *current = NULL;
int ser;
char choice = 0;
while(true) {
cout
cout
cout
cout
cout
cout
cin >> choice;
while(cin.get() != ' ');
if(choice == '1'){
current = get_data();
add_data(current);
}
else if(choice == '2'){
display_data();
}
else if (choice == '3'){
free_list();
return;
}
else if (choice == '4'){
cout
cin>>ser;
search(ser);
}
else {
cout
Obiectives: After completing this assignment, students will able to: create a linked list write the code to implement the list . add elements to a linked list . remove elements from a linked list . search for elements in a linked list sort a linked list . reverse a linked list use the standard library }
}
}
int main()
{
// Program starts execution from main block
cout
processMenu();
// calling process function which inturn calls create and display functions
system("pause");
return 0;
}
- and call many functions from this library compare between the created linked list and the standard library
- Background: Linked list is a way to store multiple objects of the same type. It is similar to array or vector in that it stores data in order but it accesses data via pointers unlike arrays. In a linked list, there is the pointer "head" that always points to first item in the list and the pointer "next" which points to the next item in the list. The pointer "current" is used to iterate over the linked list (or search the list). In order to iterate over the list, first, current is set to the value of head. When we are done with head, we write current- current-next: to move on to the next item in the list. We do this until current->nextNULL: which means we have reached the last item in the list. Keep in mind that this iteration doesn't apply to circular linked lists. When using pointers, we use "instead of "To access the elements of an object. General Instructions: Review the problem description below and implement this program in C++ Your task is to create a Linked List making use of different functions, one method to create a linked list, one method to display the elements in the linked list, one method to implement the search functionality, one method to free the linked list
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
