将inFile读入链接列表并按名称排序

时间:2012-12-21 20:11:42

标签: c++ sorting linked-list

我是链接列表中的一个菜鸟,所以如果你能帮助我使用这段代码,我将非常感激,到目前为止,如果写下你可以看到我的第一个问题,我怎么能调用我的空白呢? (见下文)我已经写了排序无效但不知道如何正确地写它来访问链表......

  ***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;
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

请勿使用struct,而是使用class

class Organizer
{
   Organizer() { next = NULL; };
   string first_name;
   string last_name;
   int number;
   string email;
   string bday;
   Organizer* next;
   bool operator <(const Organizer &other)
   { return last_name.compare(other.last_name) == -1; };
};

我认为字符串不支持<,所以我使用了compare。

由于您只有一个单链接列表,而不是双链接(仅next变量,而不是previous),因此排序需要**,或者您需要比较->next->next->next,并且有一个空头项目。

sort_by_last(organizer);更改为sort_by_last(&head_ptr);

sort函数看起来像(假设你想进行插入排序,这很慢但很简单):(未经测试)

void sort_by_last(Organizer **head_ptr)
{
   Organizer **node = head_ptr;

   while (*node != NULL)
   {
      Organizer **temp = head_ptr;
      Organizer **next = &(*node)->next;
      while (*temp != NULL && **temp < **node)
         temp = &(*temp)->next;
      if (*temp != NULL && *temp != *node)
      {
         (*node)->next = *temp;
         (*temp)->next = (*node)->next;
         *temp = *node; // same as (*temp)->prev->next = *node
         *node = NULL; // so we don't point back to earlier in the list
      }
      node = next;
   }
}

我假设您必须使用链表,但是std :: set将是一个更简单的解决方案(它在插入时排序)。

#include <set>
std::set<Organizer> theSet;
...
Organizer organizer;
// set up organizer
theSet.insert(organizer);