How do i print all the valúes that I give to p, and sort them??
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
struct person {
string name;
int grade;
int counter;
person* next;
person* previous;
};
void Mergefiles() {
ifstream in1 ("/Users/Arturo/Desktop/sample/inA_2.txt");
ifstream in2("/Users/Arturo/Desktop/sample/inA_1.txt");
ofstream out ("/Users/Arturo/Desktop/sample/out_11.txt");
out << in1.rdbuf() << in2.rdbuf();
};
person* readPerson() {
ifstream out;
out.open("/Users/Arturo/Desktop/sample/out_11.txt");
string line;
string nombre;
int cali;
person* p = new person;
while (getline(out,line)) {
istringstream is (line);
is>>nombre>>cali;
p->name=nombre;
//if (p->name == "stop")
// return 0;
p->grade=cali;
p->next = 0;
p->previous = 0;}
return p;//Problem with return ¿?
}
void addPerson(person* newPerson, person*& firstPerson) {
person* currentPerson = firstPerson;
person* lastPerson=0;
while (currentPerson != 0) {
if (newPerson->name < currentPerson->name) {
break;
}
if (currentPerson->next == 0)
lastPerson = currentPerson;
currentPerson = currentPerson->next;
}
if (currentPerson == firstPerson) {
newPerson->previous = 0; //1
newPerson->next = currentPerson; //2
currentPerson->previous = newPerson; //3
firstPerson = newPerson;
return;
}
person* previous; // previous of the new one
if (currentPerson == 0)
previous = lastPerson;
else
previous = currentPerson->previous;
newPerson->next = previous->next; //1
if (previous->next != 0)
previous->next->previous=newPerson; //2
previous->next = newPerson; //3
newPerson->previous = previous; //4
}
void changeGrades(person*first, int newGrades, string personName) {
person*current = first;
while (current != 0) {
if (personName == current->name) {
current->grade = newGrades;
break;
}
current = current->next;}}
void printPerson(person* first){
person* current = first;
while(current != 0){
cout << current->name << " " << current->grade<< endl;
current = current->next;
}
}
int main(int argc, char** argv) {
person* p;
person* firstPerson = 0;
//HERE I DONT KNOW HOW TO IMPLEMENT THE LOOP
p = readPerson();
if (firstPerson != 0) {
addPerson(p, firstPerson);
} else {
firstPerson = p;
}
cout<<"The list:"<<endl;
printPerson(firstPerson);
changeGrades(firstPerson, 3, "bird");
cout<<"The sorted list:"<<endl;
printPerson(firstPerson);
person* current = firstPerson;
person* next;
while (current != 0) {
next = current->next;
delete current;
current = next;
}
return 0;
}