C++ problem I need to sort the following data from txt file by linked list displ
ID: 3865934 • Letter: C
Question
C++ problem
I need to sort the following data from txt file by linked list
display them first and than remove those population less than 4 miilion
and display the list after remove
Thanks for the help!
Data:
Idaho Boise 1,297,274
Washington Olympia 5,908,684
Vermont Montpelier 609,890
Hawaii Honolulu 1,216,642
Arizona Phonenix 5,140,683
Montana Helena 905,316
Virginia Richmond 7,100,702
North Dakota Bismarck 643,756
Colorado Denver 4,311,882
Mississippi Jackson 2,852,927
Delaware Dover 785,068
Georgia Atlanta 8,206,975
South Carolina Columbia 4,025,061
Illinois Springfield 12,439,042
Nebraska Lincoln 1,715,369
Arkansas Little Rock 2,679,733
New Hampshire Concord 1,238,415
Ohio Columbus 11,374,540
Kansas Topeka 2,693,824
Louisiana Baton Rouge 4,480,271
Michigan Lansing 9,955,829
Florida Talahasse 16,028,890
Connecticut Hartford 3,409,535
Iowa Des Moines 2,931,923
West Virginia Charleston 1,813,077
Missouri Jefferson 5,606,260
Wyoming Cheyenne 495,304
California Sacramento 33,930,798
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<sstream>
#include<string>
using namespace std;
struct entry{
string name;
string capital;
long population;
struct entry *next;
};
class LinkedList {
private:
entry *start;
public:
LinkedList(){
start = NULL;
}
void Add(string name, string capital, long population){
entry *temp;
entry *ptr;
temp = new entry;
temp->name = name;
temp->capital = capital;
temp->population = population;
temp->next = NULL;
if (start == NULL)
start = temp;
else {
ptr = start;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = temp;
}
}
void sortByPopulation(){
entry *ptr, *ptr1;
ptr = start;
entry temp;
while (ptr != NULL){
ptr1 = ptr->next;
while(ptr1 != NULL){
if (ptr->population > ptr1->population){
temp.name = ptr->name;
temp.capital = ptr->capital;
temp.population = ptr->population;
ptr->name = ptr1->name;
ptr->capital = ptr1->capital;
ptr->population = ptr1->population;
ptr1->name = temp.name;
ptr1->capital = temp.capital;
ptr1->population = temp.population;
}
ptr1 = ptr1->next;
}
ptr = ptr->next;
}
}
void disp(){
entry *ptr;
ptr = start;
while(ptr != NULL){
cout << ptr->name << " " << ptr->capital << " " << ptr->population << endl;
ptr = ptr->next;
}
}
void remove(long a){
entry *ptr;
ptr = start;
while (start->population < a)
start = start->next;
ptr = start;
while(ptr->next != NULL){
if (ptr->next->population < a){
ptr->next = ptr->next->next;
}
if (ptr->next != NULL)
ptr = ptr->next;
}
}
};
int main(){
ifstream fin;
//vector<entry> data;
string name;
string capital;
string population;
long data;
entry item;
string str;
LinkedList lt;
fin.open("statedata3.txt"); //Assuming data is in Input.txt and every line conatins name, capital and population separated with spaces
while (fin >> name){
item.name = name;
if (name == "North"){
fin >> name;
item.name = item.name + " " + name;
}
if (name == "South"){
fin >> name;
item.name = item.name + " " + name;
}
if (name == "New"){
fin >> name;
item.name = item.name + " " + name;
}
if (name == "West"){
fin >> name;
item.name = item.name + " " + name;
}
if (name == "Rhode"){
fin >> name;
item.name = item.name + " " + name;
}
fin >> capital;
item.capital = capital;
if (capital == "Little"){
fin >> capital;
item.capital = item.capital + " " + capital;
}
if (capital == "Des"){
fin >> capital;
item.capital = item.capital + " " + capital;
}
if (capital == "Baton"){
fin >> capital;
item.capital = item.capital + " " + capital;
}
if (capital == "Salt"){
fin >> capital;
item.capital = item.capital + " " + capital;
item.capital = capital;
fin >> capital;
item.capital = item.capital + " " + capital;
}
if (capital == "Carson"){
fin >> capital;
item.capital = item.capital + " " + capital;
}
if (capital == "Santa"){
fin >> capital;
item.capital = item.capital + " " + capital;
}
if (capital == "St."){
fin >> capital;
item.capital = item.capital + " " + capital;
}
if (capital == "Oklahoma"){
fin >> capital;
item.capital = item.capital + " " + capital;
}
fin >> population;
str = "";
for (int i = 0; i<population.size(); i++){
if (population[i] != ',')
str = str + population[i];
}
istringstream iss1(str);
iss1 >> data;
lt.Add(name, capital, data);
}
cout << "All State Information ";
lt.disp();
cout << "-------------------------------------------------------------------" << endl;
lt.sortByPopulation();
cout << "Sorted by Population ";
lt.disp();
cout << "-------------------------------------------------------------------" << endl;
cout << "Removing States with population less that 4 million.... ";
cout << "-------------------------------------------------------------------" << endl;
lt.remove(4000000);
lt.disp();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.