C++ Linked List Assignment Objectives: After completing this assignment, student
ID: 3917283 • Letter: C
Question
C++ Linked List Assignment
Objectives: 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 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->next == NULL; 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.
Problem description: A University has a need to maintain student records such as their personal details and their grades. Currently they are maintaining these records on papers. But that involves a lot of manual work for their employees to store and fetch the records. Also sometimes they are facing some problems such as losing student records and taking a lot of time to search for a particular record. As the University is gaining more recognition, the number of students getting registered is increasing year by year. The University has a shortage of funds to hire more employees to maintain the records of student details.
The University Management decides to hire an application developer to design an application that will be able to store and search their students’ records.
Application should be capable of storing student’s personal records and their grades. Personal records include Student’s First Name, Last Name, UIN, Date of Birth, and GPA.
Your task is to create the Double Linked Linked List which implements the following functions:
• Creates a Linked List to store student records.
• Inserts an element at a particular position.
• Displays student records.
• Searches for a particular student record based on the Student UIN.
• Free the list on user request.
Resources Provided: You are provided with a program that implements single linked list. This program performs the following functions: • Creates a Linked List to store student records. • Displays the student records. • Searches a particular student record based on the Student UIN. • Free the list on user request.
singleLinkedList.cpp
Task 2
Objectives: Your task for this part of the lab is to use the standard library to create 4 different linked lists: 1. Integer list 2. Float list 3. String list 4. A cellphone Contact list including name, telephone number, address, and further notes.
Background: In this portion of the lab, we need to use the standard library which includes many helpful built-in functions. These functions perform different operations on lists, like sorting, searching, reversing, deleting, and adding elements to the list. An object of the class is a container for storing a collection of objects (data items). Unlike the lists you create by hand, these lists come with pre-defined next and previous element pointers. By nature, lists are implemented as doubly linked lists. You can use an iterator to iterate through the list. Lists also support functions like push_back, push_front, insert, sort, and reverse. While the pointer pointing to the first item in the list can be accessed via begin(),the end() function returns a pointer to the spot after the last item in the list.
For more information on lists, what they are capable of, and how you can implement one, visit http://en.cppreference.com/w/cpp/container/list
Start: 1) Create the integer, and string lists as empty lists. Add 6 elements each to the lists based on:
• The numerical order of the integer (between 1 and 10)
• Alphabetical order of the string list
2) Create one more list of 1000 elements, all of them with the value 78.15.
3) Next, “push” 0 to the beginning and 15 to the end of the integer list.
4) Insert your name to the middle of the string list (at location 4) using the insert() functions
5) Add 2 to the first integer in the list and subtract 4 from the last integer in your integer list.
6) Iterate thorough your string linked list to print the values in the list.
7) Create and fill the cellphone-contact list with 500 names. Use the Random Number Generator to create First/Family names. For phone numbers, keep the area code 757 and again, use the random number generator to generate phone numbers for the contacts.
8) Lastly, sort the integer list and then reverse it
Explanation / Answer
here is your modified program : ---------->>>>>>
#include <iostream>
#include <cstdlib>
#include <string>
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 *prev;
};
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 = current->next;
current->next = NULL;
delete current;
current = head;
}
head = NULL;
}
void display_data()
{
cout << endl << endl << "Listing all student records: " << endl;
cout << "---------------------------" << endl;
// 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 << "No Data!" << endl;
return;
}
while(start) {
cout << start -> firstName << endl;
cout << start -> lastName << endl;
cout << start -> oduUin << endl;
cout << start -> dateOfBirth << endl;
cout << start -> gpa << endl << endl;
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 << endl;
cout << "You chose option #1." << endl;
cout << "What is the student's first name?: ";
cin >> rec->firstName;
cout << "What is the student's last name?: ";
cin >> rec->lastName;
cout << "What is the student's uin?: ";
cin >> rec->oduUin;
cout << "What is the student's date of birth?: ";
cin >> rec->dateOfBirth;
cout << "What is the student's GPA?: ";
cin >> rec->gpa;
rec->next = NULL;
rec->prev = NULL;
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)
if(head != NULL)
head->prev = current;
head = current;
}
void insertAt(int index,StudentRecord* ¤t){
StudentRecord *cur = head;
if(index < 0){
delete current;
cout<<" Index Out Of Bound: ";
return;
}
if(index == 0){
add_data(current);
return;
}
int i = 0;
while(cur != NULL){
if(i == index){
current->prev = cur->prev;
current->next = cur;
cur->prev = current;
if(current->prev != NULL)
current->prev->next = current;
return;
}
i++;
cur = cur->next;
}
delete current;
cout<<" Index Out Of Bound: ";
}
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
StudentRecord *cur = head;
while (cur != NULL)
{
if (cur->oduUin == key)
{
cout<<"key found"<<endl;
// cout<<head->uin<<endl;
cout<<"first name = "<<cur->firstName<<endl;
cout<<"last name = "<<cur->lastName<<endl;
cout<<"date of birth = "<<cur->dateOfBirth<<endl;
cout<<"gpa = "<<cur->gpa<<endl;
return;
}
cur = cur->next;
}
cout<<"Key not found"<<endl;
}
void processMenu()
{
// creating current node for StudentRecord struct
StudentRecord *current = NULL;
int ser;
char choice = 0;
while(true) {
cout << endl <<"What would you like to do?" << endl;
cout <<"==========================" << endl;
cout << "1. Enter a student record. " << endl;
cout << "2. List all student records. " << endl;
cout << "3. Exit program. " <<endl;
cout << "4. Search. " << endl;
cout << "5. Insert a student records at index" << endl;
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<<"Enter student uin to search for records"<<endl;
cin>>ser;
search(ser);
}else if(choice == '5'){
current = get_data();
int index;
cout << "Enter a index to insert the record : ";
cin >> index;
insertAt(index,current);
cout<<endl;
}
else {
cout << "Allowed Selections are 1, 2, 3, 4, and 5 !!!" << endl;
}
}
}
int main()
{
// Program starts execution from main block
cout << "Student Record Program." << endl << endl;
processMenu();
// calling process function which inturn calls create and display functions
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.