Advertisement
Proff_Ust

FreqDict

Nov 17th, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. struct TList
  5. {
  6.     string word;
  7.     int counter;
  8.     TList* next;
  9. };
  10.  
  11. bool isEmpty(TList* lst)
  12. {
  13.     return lst==NULL;
  14. }
  15.  
  16. int addElemToDict(TList*& lst, string newValue)
  17. {
  18.     if(!isEmpty(lst))
  19.     {
  20.         TList* temp;
  21.         TList* prev = NULL;
  22.         temp = lst;
  23.         while((temp!=NULL)&&(temp->word<newValue)&&(temp->word!=newValue))
  24.         {
  25.             prev = temp;
  26.             temp = temp->next;
  27.         }
  28.         if(temp==NULL)
  29.         {
  30.             TList * newNode = new TList;
  31.             newNode->word = newValue;
  32.             newNode->counter = 1;
  33.             prev->next = newNode;
  34.             newNode->next = NULL;
  35.             return 0;
  36.         }
  37.         if(temp->word==newValue)
  38.         {
  39.             temp->counter++;
  40.             return 0;
  41.         }
  42.         if(temp->word>newValue)
  43.         {
  44.             TList* newNode = new TList;
  45.             newNode->word = newValue;
  46.             newNode->counter = 1;
  47.             if(prev!=NULL)
  48.             {
  49.                 prev->next = newNode;
  50.                 newNode->next = temp;
  51.                 return 0;
  52.             }
  53.             else
  54.             {
  55.                 newNode->next = lst;
  56.                 lst = newNode;
  57.                 return 0;
  58.             }
  59.         }
  60.     }
  61.     else
  62.     {
  63.         TList * newNode = new TList;
  64.         newNode->word = newValue;
  65.         newNode->counter = 1;
  66.         newNode->next = NULL;
  67.         lst = newNode;
  68.     }
  69.     return 0;
  70. }
  71.  
  72. int printList(TList* lst)
  73. {
  74.     while(lst!=NULL)
  75.     {
  76.         cout<<lst->word<<" "<<lst->counter<<endl;
  77.         lst = lst->next;
  78.     }
  79.     return 0;
  80. }
  81.  
  82. int clearList(TList* lst)
  83. {
  84.     TList* tmp = NULL;
  85.     while(lst!=NULL)
  86.     {
  87.         tmp = lst;
  88.         lst = lst->next;
  89.         delete tmp;
  90.     }
  91.     return 0;
  92. }
  93.  
  94. int createDict(TList*& lst, ifstream& inpf)
  95. {
  96.     string word;
  97.     while(!inpf.eof())
  98.     {
  99.         inpf>>word;
  100.         addElemToDict(lst, word);
  101.     }
  102.     return 0;
  103. }
  104.  
  105. int main()
  106. {
  107.     setlocale(0,"Russian");
  108.     TList* List = NULL;
  109.     ifstream inputFile("input.txt");
  110.     if(inputFile)
  111.     {
  112.         createDict(List,inputFile);
  113.         printList(List);
  114.         clearList(List);
  115.         return 0;
  116.     }
  117.     else
  118.     {
  119.         cout<<"Не удалось открыть файл для чтения"<<endl;
  120.         return -1;
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement