Objectives: Your task for this part of the lab is to use the standard library <l
ID: 3917345 • Letter: O
Question
Objectives: Your task for this part of the lab is to use the standard library <list> 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
Explanation / Answer
#include <iostream>
#include<stdlib.h>
#include <list>
#include <iterator>
using namespace std;
struct telephone{
string name;
string number;
string address;
string notes;
};
void random_name(char *s, const int len) {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < len; ++i) {
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
}
}
void random_number(char *s, const int len) {
static const char alphanum[] ="0123456789";
for (int i = 0; i < len; ++i) {
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
}
}
int main()
{
string str;
list <int> Integer_List;
list <string> String_List;
list <float> Float_List;
list <struct telephone> Telephone_List;
for(int i=0;i,6;i++){
Integer_List.push_back(i+1);
cout<<"Enter string : ";
cin>>str;
String_List.push_back(str);
}
for(int i=0;i<1000;i++){
Float_List.push_back(78.15);
}
Integer_List.push_front(0);
Integer_List.push_back(15);
cout<<"Enter your name : ";
cin>>str;
list <string> :: iterator it;
it=String_List.begin();
for(int i=1;i<4;i++)it++;
String_List.insert(it,str);
list <int> :: iterator it1;
it1=Integer_List.begin();
*it1+2;
for(int i=1;i<8;i++)it1++;
*it1-4;
for(it = String_List.begin(); it != String_List.end(); ++it)
cout << ' ' << *it;
cout << ' ';
char name[10];
for(int i=0;i<500;i++){
struct telephone t;
random_name(name,10);
t.name=name;
random_number(name,10);
t.number=name;
random_name(name,10);
t.notes=name;
random_name(name,10);
t.address=name;
Telephone_List.push_back(t);
}
Integer_List.sort();
Integer_List.reverse();
return 0;
}
for any query please comment.
please upvote if find it helpful.
Thank you.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.