#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cstdlib>
using namespace std;
void selectionSort(string [], int);
void showArray(string [], int);
struct node{
node * next;
node * tail;
node * head;
int id;
string name;
string surname;
int age;
};
node * thelist = NULL;
node * tail = NULL;
node * head = NULL;
void initList(int id, string surname, string name, int age){
thelist=new node;
thelist->next=NULL;
thelist->id=id;
thelist->surname=surname;
thelist->name=name;
thelist->age=age;
tail=thelist;
head=thelist;
}
void addToHead(int newid, string newsurname, string newname, int newage){
node * mynewnode = new node;
mynewnode->id=newid;
mynewnode->surname=newsurname;
mynewnode->name=newname;
mynewnode->age=newage;
mynewnode->next=thelist;
thelist=mynewnode;
head=thelist;
//Question : we did not check whether the list is empty or not, why ?
}
void removeFromHead(){
if(head==NULL){
return;
}
node * temp = head;
head=head->next;
delete temp;
}
node * getNodeFromValue(int searchValue){
node * temp=thelist;
while(temp!=NULL && temp->id!=searchValue){
temp=temp->next;
}
return temp;
}
void insertAfterNode(node * mynode,int newid, string newsurname, string newname, int newage){
node * mynewnode = new node;
mynewnode->id=newid;
mynewnode->surname=newsurname;
mynewnode->name=newname;
mynewnode->age=newage;
mynewnode->next=mynode->next;
mynode->next=mynewnode;
}
void addToTail(int newid, string newsurname, string newname, int newage) {
if (tail != 0) {
node * mynewnode = new node;
mynewnode->id=newid;
mynewnode->surname=newsurname;
mynewnode->name=newname;
mynewnode->age=newage;
tail->next=mynewnode;
tail=tail->next;
tail->next=NULL;
}
else node * head = tail = new node;
}
void deleteAfterNode(node * mynode){
if(mynode->next==NULL){
return;
}
node * temp=mynode->next;
mynode->next=mynode->next->next;
delete temp;
}
void printList(){
node * temp=head;
while(temp!=NULL){
cout<<temp->id<<" ";
cout<<temp->surname<<" ";
cout<<temp->name<<" ";
cout<<temp->age<<" "<<endl;
temp=temp->next;
}
cout<<endl;
}
void sortNodes(node * ptr) {
string temp ;
node * curr;
for(bool Swap = true; Swap; ) {
Swap = false;
for(curr = ptr; curr->next != NULL; curr = curr->next) {
if(curr->surname > curr->next->surname) {
temp= curr->surname;
curr->surname = curr->next->surname;
curr->next->surname = temp;
Swap = true;
}
}
}
}
void printUserInput()
{
char x,y;
node * temp=head;
cout<<"Please enter two latter"<<endl;
cin>>x;
cin>>y;
if(x>=y)
{
cout<<"The second user input must come after the first one."<<endl;
}
else
{
while(temp->surname[0]==x){
temp=temp->next;
}
while(temp->surname[0]==y){
cout<<temp->id<<" ";
cout<<temp->surname<<" ";
cout<<temp->name<<" ";
cout<<temp->age<<" "<<endl;
temp=temp->next;
}
}
}
int main()
{
initList(0,"Surname","Name",0);
ifstream infile;
infile.open ("projectsampleDB.txt");
string sString;
string id;
while(!infile.eof())
{
getline(infile,sString);
int i;
i=sString.find(" ");
int tempId=atoi(sString.substr(0,i).c_str());
sString.erase(0,i+1);
i=sString.find(" ");
string tempSurname=sString.substr(0,i);
sString.erase(0,i+1);
i=sString.find(" ");
string tempName=sString.substr(0,i);
sString.erase(0,i+1);
int tempAge=atoi(sString.substr().c_str());
addToTail(tempId, tempSurname, tempName, tempAge);
}
removeFromHead();
cout<<"Unsorted List : "<<endl;
printList();
cout<<"Sorted List : "<<endl<<endl;
sortNodes(head);
printList();
printUserInput();
}