我是链表上的菜鸟,所以如果你能帮助我处理这段代码,我将不胜感激,到目前为止,如果写下你可以在下面看到的我的第一个问题是我如何调用我的 void 对文本文档进行排序(见下文) 我已经写了 sort void 但不知道如何正确地写它来访问链表......
***1stName lastname number email bday***
Nikos Nikolas 281085252 niko@hotmail.com 21/10/1995
mary ann 2810258963 mary@hotmail.com 22/10/1995
james bond 2810254789 james@hotmail.com 23/11/2000
luna loop 2810258741 luna@hotmail.com 24/04/1990
#include <iostream>
#include <fstream>
#include <string>
#define TRUE 1
using namespace std;
struct organizer
{
string first_name;
string last_name;
int number;
string email;
string bday;
organizer* next;
};
int main()
{
void sort_by_last(organizer* head_ptr);
char command;
ifstream inFile;
inFile.open("mycontacts.txt");
if (!inFile)
{
cerr << "Unable to open file." << endl;
return 0;
}
organizer* temp = new organizer;
organizer* head_ptr = temp;
while(inFile)
{
inFile >> temp->first_name;
inFile >> temp->last_name;
inFile >> temp->number;
inFile >> temp->email;
inFile >> temp->bday;
temp->next = new organizer;
temp = temp->next;
}
temp->next = NULL;
while (TRUE)
{
cout << "Write 'print' To print mycontacts." << '\n';
cout << "Write 'find' '..'to search myconacts."<< '\n';
cout << "Write 'delete' 'name'to delete a contact."<< '\n';
cout << "Write 'insert' 'details' to add a contact."<< '\n';
cout << "Write 'quit' to exit the programm."<< '\n';
command = getchar();
switch(command)
{
case 'p':
sort_by_last(organizer);
break;
case 'f':
cout << "search and print\n"; break;
case 'd':
cout << "delete\n";break;
case 'i':
cout << "insert\n";break;
case 'q':
cout << "Quitin programm.\n";
return 0;
}
}
return 0;
}
void sort_by_last(organizer* head_ptr)
{
organizer* node = head_ptr;
organizer* temp = NULL;
while (node->next != NULL)
{
if(node->last_name > node->next->last_name)
{
temp = node;
node = node->next;
temp->next = node;
node = temp;
}
}
}