DilyaraL

16.24

May 10th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5.  
  6. struct node
  7. {
  8. int info;
  9. node * next;
  10. };
  11. void setNull(node * & p)
  12. {
  13. p = NULL;
  14. }
  15. void createElem(node * & p, int y)
  16. {
  17. p = new node;
  18. p->info = y;
  19. p->next = NULL;
  20. }
  21. void addFirst(node * & head1, node * p)//добавление в начало
  22. {
  23. p->next = head1;
  24. head1 = p;
  25. }
  26.  
  27. void addLast(node*&tail,node *q)
  28. {
  29. tail ->next = q;
  30. tail = q;
  31. }
  32.  
  33. void createListFromFile(node * & head1, ifstream & f)
  34. {
  35. node * q;
  36. int x;
  37. setNull(head1);
  38. while (f >> x)
  39. {
  40. createElem(q, x);
  41. addFirst(head1, q);
  42. }
  43. }
  44. void setHead(node * head1, node * & p)
  45. {
  46. p = head1;
  47. }
  48. bool nodeIsNull(node * p)
  49. {
  50. return p == NULL;
  51. }
  52. void showNode(node * p)
  53. {
  54. cout << p->info << ' ';
  55. }
  56. void moove(node * & p)
  57. {
  58. p = p->next;
  59. }
  60. void showList(node * head)
  61. {
  62. node * q;
  63. setHead(head, q);
  64. if(!nodeIsNull(head))
  65. while (!nodeIsNull(q))
  66. {
  67. showNode(q);
  68. moove(q);
  69. }
  70. else cout << "List is empy";
  71. cout << endl;
  72. }
  73.  
  74. bool compInfo(node * p1, node * p2)
  75. {
  76. return p1->info == p2->info;
  77. }
  78.  
  79. bool taskA(node *head,int E)
  80. {
  81. if (nodeIsNull(head))
  82. return false;
  83. else
  84. {
  85. if (head->info == E)
  86. return true;
  87. taskA(head->next, E);
  88. }
  89. }
  90.  
  91. void taskB(node *head, int E, int&k)
  92. {
  93. if (!nodeIsNull(head))
  94. {
  95. if (head->info == E)
  96. k++;
  97. taskB(head->next, E,k);
  98. }
  99. }
  100.  
  101. int taskC(node *head, int max)
  102. {
  103. if (nodeIsNull(head))
  104. return max;
  105. else
  106. {
  107. if (head->info > max)
  108. max = head->info;
  109. taskC(head->next, max);
  110.  
  111. }
  112. }
  113.  
  114. void taskD(node *head)
  115. {
  116. if (!nodeIsNull(head))
  117. {
  118. taskD(head->next);
  119. showNode(head);
  120.  
  121. }
  122. }
  123.  
  124. void taskE(node *head,int e1, int e2)
  125. {
  126. if (!nodeIsNull(head))
  127. {
  128. if (head->info == e1)
  129. head->info = e2;
  130. taskE(head->next,e1,e2);
  131. }
  132. }
  133.  
  134. void taskF(node *&head, int e1, node*temp)
  135. {
  136. if (!nodeIsNull(head))
  137. {
  138. if (head->info != e1)
  139. {
  140. temp = head;
  141. taskF(head->next, e1,temp);
  142. }
  143. else
  144. {
  145. if (temp == head)
  146. head = head->next;//первый элемент
  147. else
  148. temp->next = head->next;
  149. }
  150. }
  151. }
  152.  
  153. void taskG(node *&head, int e1, node*temp)//
  154. {
  155. if (!nodeIsNull(head))
  156. {
  157. if (head->info == e1)
  158. {
  159. if (temp == head)
  160. head = head->next;//первый элемент
  161. else
  162. temp->next = head->next;
  163. }
  164. temp = head;
  165. taskG(head->next, e1, temp);
  166. }
  167.  
  168. }
  169.  
  170. void taskH(node* &head1, node*&head2, node*&tail)
  171. {
  172. node*p;
  173. if(!nodeIsNull(head1))
  174. {
  175. createElem(p, head1->info);
  176. addLast(tail, p);
  177. taskH(head1->next, head2, tail);
  178. }
  179. }
  180.  
  181. void taskI(node* &head1, int e1)
  182. {
  183. if (!nodeIsNull(head1))
  184. {
  185. if (head1->info == e1)
  186. {
  187. node*p;
  188. createElem(p, e1);
  189. p->next= head1->next;
  190. head1->next = p;
  191. taskI(p->next, e1);
  192. }
  193. else
  194. taskI(head1->next, e1);
  195. }
  196. }
  197.  
  198. void sr(node*&head, double &s, int &k)//не пустой список
  199. {
  200. if (!nodeIsNull(head))
  201. {
  202. s += head->info;
  203. k++;
  204. sr(head->next, s, k);
  205. }
  206. else
  207. cout<< s / k;
  208. }
  209.  
  210. int main()
  211. {
  212. ifstream f("Text.txt");
  213. node * head1;
  214. createListFromFile(head1, f);
  215. showList(head1);
  216.  
  217. /*if (!nodeIsNull(head1))
  218. {
  219. int e;
  220. cin >> e;
  221. taskA(head1, e) ? cout << "YES" : cout << "NO";
  222. cout << endl;
  223. }
  224. else cout << "List is empy" << endl;*/
  225.  
  226. if (!nodeIsNull(head1))
  227. {
  228. int e,k=0;
  229. cin >> e;
  230. taskB(head1, e,k);
  231. cout <<k<< endl;
  232. }
  233. else cout << "List is empy" << endl;
  234.  
  235. //cout << taskC(head1, head1->info) << endl;
  236.  
  237. /*if (!nodeIsNull(head1))
  238. {
  239. taskD(head1);
  240. cout << endl;
  241. }
  242. else cout << "List is empy" << endl; */
  243.  
  244. /*if (!nodeIsNull(head1))
  245. {
  246. int e1, e2;
  247. cin >> e1 >> e2;
  248. taskE(head1, e1, e2);
  249. showList(head1);
  250. }
  251. else cout << "List is empy" << endl;*/
  252.  
  253. /*if (!nodeIsNull(head1))
  254. {
  255. int e1;
  256. cin >> e1;
  257. taskF(head1, e1,head1);
  258. showList(head1);
  259. }
  260. else cout << "List is empy" << endl;*/
  261.  
  262. /*if (!nodeIsNull(head1))
  263. {
  264. node* head2 = new node;
  265. setNull(head2->next);
  266. head2->info = head1->info;
  267. moove(head1);
  268. node* tail = head2;
  269. taskH(head1, head2, tail);
  270. showList(head2);
  271. }
  272. else cout << "List is empy" << endl;*/
  273.  
  274. /*if (!nodeIsNull(head1))
  275. {
  276. int e1;
  277. cin >> e1;
  278. taskI(head1, e1);
  279. showList(head1);
  280. }
  281. else cout << "List is empy" << endl;*/
  282.  
  283. /*double s = 0;int k = 0;
  284. sr(head1, s, k);*/
  285.  
  286. system("pause");
  287. return 0;
  288. }
Advertisement
Add Comment
Please, Sign In to add comment