Advertisement
Sanlover

Untitled

Oct 16th, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5.  
  6. struct cell
  7. {
  8. char symbol;
  9. size_t amount;
  10.  
  11. cell* next;
  12.  
  13. cell(char symbol) : symbol(symbol), next(nullptr)
  14. {
  15. amount = 1;
  16. }
  17. cell(char symbol, size_t amount) : symbol(symbol), amount(amount),next(nullptr)
  18. {
  19. }
  20. };
  21.  
  22. class queue
  23. {
  24. private:
  25.  
  26. cell* first;
  27. cell* last;
  28. size_t size;
  29. public:
  30. queue() : first(nullptr), last(nullptr), size(0) {};
  31. bool isEmpty() { return size == 0; }
  32.  
  33. void push(char symbol);
  34. cell pop();
  35. void sortByAmount();
  36. cell getById(size_t id);
  37.  
  38. friend void print(const queue my);
  39. };
  40. void print(queue my)
  41. {
  42. if(my.isEmpty())
  43. {
  44. cout << "Queue is empty" << endl;
  45. return;
  46. }
  47.  
  48.  
  49. size_t counter = 1;
  50. cell* it = my.first;
  51. while(it!= nullptr)
  52. {
  53. cout << counter++ << ") Symbol = " << it->symbol << " | Amount = " << it->amount << endl;
  54. it = it->next;
  55. }
  56. };
  57.  
  58. void fillFromFile(queue& my, const char* fileName)
  59. {
  60. ifstream input(fileName);
  61. if (!input.is_open())
  62. {
  63. cout << "File is not open" << endl;
  64. return;
  65. }
  66.  
  67. char temp;
  68. while (!input.eof())
  69. {
  70. input >> temp;
  71. my.push(temp);
  72. }
  73.  
  74. input.close();
  75. }
  76.  
  77.  
  78. int main()
  79. {
  80. queue a;
  81. cout << "Before adding" << endl;
  82. print(a);
  83. fillFromFile(a, "file.txt");
  84. cout << "After adding" << endl;
  85. print(a);
  86.  
  87. cout << endl;
  88. cell save = a.getById(4);
  89. cout << "I got = " << save.symbol << endl << " amount = " << save.amount << endl;
  90. cout << "After adding" << endl;
  91. print(a);
  92. return 0;
  93. }
  94.  
  95. void
  96. queue::push(char symbol)
  97. {
  98.  
  99. if(isEmpty())
  100. {
  101. cell* newCell = new cell(symbol);
  102. first = last = newCell;
  103. size++;
  104. return;
  105. }
  106.  
  107. cell* it = first;
  108. while (it != nullptr)
  109. {
  110. if (it->symbol == symbol)
  111. {
  112. it->amount++;
  113. return;
  114. }
  115. it = it->next;
  116. }
  117. cell* newCell = new cell(symbol);
  118. last->next = newCell;
  119. last = newCell;
  120. size++;
  121.  
  122. }
  123.  
  124. cell
  125. queue::pop()
  126. {
  127. if(isEmpty())
  128. {
  129. cout << "Error: Queue is empty " << endl;
  130. return '-';
  131. }
  132.  
  133. if(first != last)
  134. {
  135. cell toReturn(first->symbol,first->amount);
  136. cell* toDelete = first;
  137.  
  138. first = first->next;
  139. delete toDelete;
  140. size--;
  141. return toReturn;
  142. }
  143. else
  144. {
  145. cell toReturn(first->symbol, first->amount);
  146. delete first;
  147. first = last = nullptr;
  148. size--;
  149. return toReturn;
  150. }
  151. }
  152.  
  153.  
  154.  
  155. cell
  156. queue::getById(size_t id)
  157. {
  158. if(isEmpty() | id > size)
  159. {
  160. cout << "Out of range" << endl;
  161. return cell('-');
  162. }
  163.  
  164. cell* it = first;
  165. for(size_t i = 0 ;i < id; i++)
  166. {
  167. it = it-> next;
  168. }
  169.  
  170. return cell(it->symbol, it->amount);
  171. }
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement