Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. //#include<iostream>
  2. //#include<fstream>
  3. //#include<string>
  4. //#include<vector>
  5. //#include<utility>
  6. //#include<algorithm>
  7. //#include<climits>
  8. //#include<set>
  9. //#include<map>
  10. //
  11. //using namespace std;
  12. #include<iostream>
  13. #include<string>
  14. using namespace std;
  15. struct Node
  16. {
  17. int data;
  18. Node* Next;
  19. };
  20. typedef Node* Pnode;
  21.  
  22. struct queue
  23. {
  24. int size;
  25. Pnode head;
  26. Pnode tail;
  27. };
  28.  
  29. void Init(queue &q)
  30. {
  31. q.head = NULL;
  32. q.tail = NULL;
  33. q.size = 0;
  34. }
  35.  
  36. void push(queue &q, int x)
  37. {
  38. Pnode elem = new Node;
  39. elem->data = x;
  40. if (q.head == NULL)
  41. {
  42. q.head = elem;
  43. q.tail = elem;
  44. q.tail->Next = NULL;
  45. q.head->Next = NULL;
  46. q.size++;
  47. cout << "ok" << endl;
  48.  
  49. return;
  50. }
  51. else
  52. {
  53. q.tail->Next = elem;
  54. elem->Next = NULL;
  55. q.tail = elem;
  56. q.size++;
  57. }
  58. cout << "ok" << endl;
  59. }
  60.  
  61. void pop(queue &q)
  62. {
  63. if (q.head == NULL)
  64. {
  65. cout << "error" << endl;
  66. return;
  67. }
  68.  
  69. Pnode elem = q.head;
  70.  
  71. if (q.head == q.tail)
  72. {
  73. q.tail = NULL;
  74. q.head = NULL;
  75. }
  76. else
  77. {
  78. q.head = q.head->Next;
  79.  
  80. }
  81. cout << elem->data << endl;
  82. q.size--;
  83. delete elem;
  84.  
  85. }
  86.  
  87. void front(queue &q)
  88. {
  89. if (q.head == NULL)
  90. {
  91. cout << "error" << endl;
  92. return;
  93. }
  94. cout << q.head->data << endl;
  95. }
  96.  
  97. void Size(queue &q)
  98. {
  99. cout << q.size << endl;
  100. }
  101.  
  102. void clear(queue &q)
  103. {
  104. while (q.head != NULL)
  105. {
  106. Pnode elem = q.head;
  107. q.head = q.head->Next;
  108. delete elem;
  109.  
  110. }
  111. q.size = 0;
  112. q.head = NULL;
  113. q.tail = NULL;
  114. cout << "ok";
  115.  
  116. }
  117.  
  118. bool IsIdiot(string s)
  119. {
  120. if (s != "push" && s != "pop" && s != "front" && s != "front" && s != "size" && s != "clear" && s != "exit")
  121. return true;
  122. return false;
  123. }
  124.  
  125. bool IsNumber(string s)
  126. {
  127. if (s.size() == 1)
  128. {
  129. if (isdigit(s[0]))
  130. return true;
  131. return false;
  132. }
  133. if (!(s[0] == '-' || isdigit(s[0])))
  134. return false;
  135.  
  136. for (int i = 1; i < s.length(); i++)
  137. if (!isdigit(s[i]))
  138. return false;
  139.  
  140. return true;
  141. }
  142.  
  143. int main()
  144. {
  145. setlocale(LC_ALL, "rus");
  146. queue q;
  147. Init(q);
  148. cout << "Выберите и введите название функии." << endl;
  149. cout << "Список функций:" << endl;
  150. cout << "push n- Добавить в очередь число n." << endl <<
  151. "pop-Удалить из очереди первый элемент." << endl <<
  152. "front -Программа должна вывести значение первого элемента, не удаляя его из очереди." << endl <<
  153. "size- Программа должна вывести количество элементов в очереди." << endl <<
  154. "clear- Программа должна очистить очередь." << endl <<
  155. "exit -Программа должна вывести bye и завершить работу." << endl;
  156. while (true)
  157. {
  158. string s;
  159. cin >> s;
  160.  
  161. if (IsIdiot(s))
  162. {
  163. cout << "Данные введены неверно"<<endl;
  164. continue;
  165. }
  166.  
  167. if (s == "push")
  168. {
  169. string g;
  170. cin >> g;
  171. if (!IsNumber(g))
  172. {
  173. cout << "Данные введены неверно" << endl;
  174. continue;
  175. }
  176.  
  177. push(q, stoi(g));
  178. }
  179.  
  180. else
  181. if (s == "pop")
  182. pop(q);
  183. else
  184. if (s == "front")
  185. front(q);
  186. else
  187. if (s == "size")
  188. Size(q);
  189. else
  190. if (s == "clear")
  191. clear(q);
  192. else
  193. if (s == "exit")
  194. {
  195. cout << "bye";
  196. return 0;
  197. }
  198.  
  199. }
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement