0

我是链表上的菜鸟,所以如果你能帮助我处理这段代码,我将不胜感激,到目前为止,如果写下你可以在下面看到的我的第一个问题是我如何调用我的 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;
                }
            }
        }
4

1 回答 1

0

不使用structclass改为使用。

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

我不认为 string 支持<,所以我使用 compare 代替。

由于您只有一个单链表,而不是双链表(只有next变量,也不previous是),因此 sort 需要采用 a **,或者您需要比较->nextand->next->next并且有一个空白的 head 项目。

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

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

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);
于 2012-12-27T11:32:28.203 回答