Question
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.
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
code given:
singleLinkedList.cpp
#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 << 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 = 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<<"key found"< uin< > 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"< >ser; search(ser); } else { cout << "Allowed Selections are 1, 2, and 3!" << 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; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started