Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.07 KB | None | 0 0
  1. #include "pch.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. struct student {
  5. student *nextStudent;
  6. student *prevStudent;
  7. int studentAge;
  8. char *studentName;
  9. int studentGrade;
  10. };
  11. char* setStudentName(char* name) {
  12. int i = 0;
  13. if (!(name = (char*)malloc(sizeof(char)))) {
  14. printf("Memory allocation problem\n");
  15. exit(1);
  16. }
  17. while (1) {
  18. name[i++] = (char)getchar();
  19. if (name[i - 1] == '\n') {
  20. name[i - 1] = '\0';
  21. break;
  22. }
  23. if (!(name = (char*)realloc(name, i + 2))) {
  24. printf("Memory allocation problem\n");
  25. exit(1);
  26. }
  27. }
  28. i = 0;
  29. if (name[0] == '\0') {
  30. printf("Incorrect name,reenter!\n");
  31. rewind(stdin);
  32. setStudentName(name);
  33. }
  34. while (name[i] != '\0') {
  35. if (!(name[i] >= 'a' && name[i] <= 'z' || name[i] >= 'A' && name[i] <= 'Z')) {
  36. printf("Incorrect information in name,reenter!\n");
  37. free(name);
  38. rewind(stdin);
  39. setStudentName(name);
  40. }
  41. i++;
  42. }
  43. return name;
  44. }
  45. student* addElementInHead(student** head) {
  46. student *currentElement;
  47. if (!(currentElement = (student*)malloc(sizeof(student)))) {
  48. printf("Memory allocation problem\n");
  49. exit(1);
  50. }
  51. printf("Enter student name\n");
  52. rewind(stdin);
  53. currentElement->studentName = setStudentName(currentElement->studentName);
  54. printf("Enter student age\n");
  55. while ((scanf_s("%d", &currentElement->studentAge)) != 1 || currentElement->studentAge <= 0) {
  56. printf("Incorrect information,reenter!\n");
  57. rewind(stdin);
  58. }
  59. printf("What the grade of student?\n");
  60. while ((scanf_s("%d", &currentElement->studentGrade)) != 1 || currentElement->studentGrade <= 0 || currentElement->studentGrade >= 7) {
  61. printf("Incorrect information,reenter!\n");
  62. rewind(stdin);
  63. }
  64. currentElement->nextStudent = *head;
  65. (*head)->prevStudent = currentElement;
  66. currentElement->prevStudent = NULL;
  67. *head = currentElement;
  68. return *head;
  69. }
  70. student* addElementInTail(student** tail) {
  71. student *currentElement;
  72. if (!(currentElement = (student*)malloc(sizeof(student)))) {
  73. printf("Memory allocation problem\n");
  74. exit(1);
  75. }
  76. printf("Enter student name\n");
  77. rewind(stdin);
  78. currentElement->studentName = setStudentName(currentElement->studentName);
  79. printf("Enter student age\n");
  80. while ((scanf_s("%d", &currentElement->studentAge)) != 1 || currentElement->studentAge <= 0) {
  81. printf("Incorrect information,reenter!\n");
  82. rewind(stdin);
  83. }
  84. printf("What the grade of student?\n");
  85. while ((scanf_s("%d", &currentElement->studentGrade)) != 1 || currentElement->studentGrade <= 0 || currentElement->studentGrade >= 7) {
  86. printf("Incorrect information,reenter!\n");
  87. rewind(stdin);
  88. }
  89. currentElement->prevStudent = *tail;
  90. (*tail)->nextStudent = currentElement;
  91. currentElement->nextStudent = NULL;
  92. *tail = currentElement;
  93. return *tail;
  94. }
  95. student* setList(student** head, student** tail) {
  96. student *currentElement;
  97. if (!(currentElement = (student*)malloc(sizeof(student)))) {
  98. printf("Memory allocation problem\n");
  99. exit(1);
  100. }
  101. printf("Enter student name\n");
  102. rewind(stdin);
  103. currentElement->studentName = setStudentName(currentElement->studentName);
  104. printf("Enter student age\n");
  105. while ((scanf_s("%d", &currentElement->studentAge)) != 1 || currentElement->studentAge <= 0) {
  106. printf("Incorrect information,reenter!\n");
  107. rewind(stdin);
  108. }
  109. printf("What the grade of student?\n");
  110. while ((scanf_s("%d", &currentElement->studentGrade)) != 1 || currentElement->studentGrade <= 0 || currentElement->studentGrade >= 7) {
  111. printf("Incorrect information,reenter!\n");
  112. rewind(stdin);
  113. }
  114. *head = *tail = currentElement;
  115. (*head)->nextStudent = (*tail)->prevStudent = NULL;
  116. return *head;
  117. }
  118. void showStudent(student* head) {
  119. student *tmpElement = head;
  120. while (tmpElement) {
  121. printf("Student name : %s\n", tmpElement->studentName);
  122. printf("Student age : %d\n", tmpElement->studentAge);
  123. printf("Student grade : %d\n", tmpElement->studentGrade);
  124. tmpElement = tmpElement->nextStudent;
  125. }
  126. }
  127. student* deleteAllElements(student** head,student** tail) {
  128. while (*head != NULL) {
  129. *head = (*head)->nextStudent;
  130. }
  131. return *head;
  132. }
  133. int NumberOfElements(student* head) {
  134. student *tmpElement;
  135. int studentCounter = 0;
  136. tmpElement = head;
  137. while (tmpElement != NULL) {
  138. studentCounter++;
  139. tmpElement = tmpElement->nextStudent;
  140. }
  141. return studentCounter;
  142. }
  143. bool isEmpty(student* head) {
  144. student *tmpElement;
  145. int elementCounter = 0;
  146. tmpElement = head;
  147. while (tmpElement != NULL) {
  148. elementCounter++;
  149. tmpElement = tmpElement->nextStudent;
  150. }
  151. if (elementCounter == 0)
  152. return true;
  153. else return false;
  154. }
  155. student* deleteHead(student** head) {
  156. *head = (*head)->nextStudent;
  157. if (*head != NULL) {
  158. (*head)->prevStudent = NULL;
  159. }
  160. return *head;
  161. }
  162. student* deleteTail(student** tail) {
  163. *tail = (*tail)->prevStudent;
  164. if (*tail != NULL) {
  165. (*tail)->nextStudent = NULL;
  166. }
  167. return *tail;
  168. }
  169. int searchElement(student* head) {
  170. student *tmpElement = head;
  171. int age;
  172. while ((scanf_s("%d", &age)) != 1 || age <= 0) {
  173. printf("Incorrect information,reenter!\n");
  174. rewind(stdin);
  175. }
  176. while (tmpElement != NULL) {
  177. if (tmpElement->studentAge == age) {
  178. break;
  179. }
  180. else {
  181. tmpElement = tmpElement->nextStudent;
  182. }
  183. }
  184. if (tmpElement == NULL) {
  185. printf("No element's like this,reenter!\n");
  186. rewind(stdin);
  187. searchElement(head);
  188. }
  189. return age;
  190. }
  191. student* deleteElement(int newElement, student** head, student** tail) {
  192. student *tmpElement = *head, *currentElement = *head;
  193. while (currentElement->studentAge != newElement) {
  194. currentElement = currentElement->nextStudent;
  195. }
  196. if (currentElement == *head || currentElement == *tail) {
  197. if (currentElement == *head) {
  198. *head = (*head)->nextStudent;
  199. if (*head != NULL) {
  200. (*head)->prevStudent = NULL;
  201. }
  202. }
  203. else {
  204. *tail = (*tail)->prevStudent;
  205. if (*tail != NULL) {
  206. (*tail)->nextStudent = NULL;
  207. }
  208. }
  209. }
  210. else {
  211. while (tmpElement->nextStudent != currentElement) {
  212. tmpElement = tmpElement->nextStudent;
  213. }
  214. tmpElement->nextStudent = currentElement->nextStudent;
  215. if (currentElement->nextStudent != NULL) {
  216. currentElement->nextStudent->prevStudent = tmpElement;
  217. }
  218. }
  219. return *head;
  220. }
  221. void showMinAndMaxElements(student* head) {
  222. student *tmpElement = head;
  223. int min, max;
  224. min = tmpElement->studentAge;
  225. max = tmpElement->studentAge;
  226. while (tmpElement->nextStudent != NULL) {
  227. if (tmpElement->studentAge < min) {
  228. min = tmpElement->studentAge;
  229. }
  230. tmpElement = tmpElement->nextStudent;
  231. }
  232. tmpElement = head;
  233. while (tmpElement != NULL) {
  234. if (tmpElement->studentAge > max) {
  235. max = tmpElement->studentAge;
  236. }
  237. tmpElement = tmpElement->nextStudent;
  238. }
  239. printf("Minimal element in list - %d\nMaximal element in list - %d\n", min, max);
  240. }
  241. student* deleteAllElementsWithThisValue(int newElement, student** head, student** tail) {
  242. student *tmpElement = *head, *currentElement = *head;
  243. while (currentElement != NULL && head != NULL) {
  244. tmpElement = *head;
  245. if (currentElement->studentAge == newElement) {
  246. if (currentElement == *head || currentElement == *tail) {
  247. if (currentElement == *head) {
  248. *head = (*head)->nextStudent;
  249. if (*head != NULL) {
  250. (*head)->prevStudent = NULL;
  251. }
  252. }
  253. else {
  254. *tail = (*tail)->prevStudent;
  255. if (*tail != NULL) {
  256. (*tail)->nextStudent = NULL;
  257. }
  258. }
  259. }
  260. else {
  261. while (tmpElement->nextStudent != currentElement) {
  262. tmpElement = tmpElement->nextStudent;
  263. }
  264. tmpElement->nextStudent = currentElement->nextStudent;
  265. if (currentElement->nextStudent != NULL) {
  266. currentElement->nextStudent->prevStudent = tmpElement;
  267. }
  268. }
  269. }
  270. currentElement = currentElement->nextStudent;
  271. }
  272. return *head;
  273. }
  274. student* replaceValueOfElements(int newElement, int newValue, student* head) {
  275. student *currentElement = head;
  276. while (currentElement != NULL) {
  277. if (currentElement->studentAge == newElement) {
  278. currentElement->studentAge = newValue;
  279. }
  280. currentElement = currentElement->nextStudent;
  281. }
  282. return head;
  283. }
  284. student* removeDuplicateElements(student** head, student** tail) {
  285. bool flag = true;
  286. student *tmpElement = *head, *currentElement = *head, *tmpElement2 = *head;
  287. if (*head != *tail) {
  288. while (currentElement->nextStudent != NULL) {
  289. tmpElement = currentElement->nextStudent;
  290. while (tmpElement != NULL) {
  291. if (tmpElement->studentAge == currentElement->studentAge) {
  292. if (tmpElement == *head || tmpElement == *tail) {
  293. if (tmpElement == *head) {
  294. *head = (*head)->nextStudent;
  295. if (*head != NULL) {
  296. (*head)->prevStudent = NULL;
  297. }
  298. }
  299. else
  300. {
  301. *tail = (*tail)->prevStudent;
  302. if (*tail != NULL) {
  303. (*tail)->nextStudent = NULL;
  304. }
  305. }
  306. flag = false;
  307. }
  308. else {
  309. tmpElement2 = *head;
  310. flag = false;
  311. while (tmpElement2->nextStudent != tmpElement)
  312. tmpElement2 = tmpElement2->nextStudent;
  313. tmpElement2->nextStudent = tmpElement->nextStudent;
  314. if (tmpElement->nextStudent != NULL) {
  315. tmpElement->nextStudent->prevStudent = tmpElement2;
  316. }
  317. }
  318. }
  319. tmpElement = tmpElement->nextStudent;
  320. }
  321. if (flag == false) {
  322. if (currentElement == *head || currentElement == *tail) {
  323. if (currentElement == *head) {
  324. *head = (*head)->nextStudent;
  325. if (*head != NULL) {
  326. (*head)->prevStudent = NULL;
  327. }
  328. }
  329. else {
  330. *tail = (*tail)->prevStudent;
  331. if (*tail != NULL) {
  332. (*tail)->nextStudent = NULL;
  333. }
  334. }
  335. }
  336. else {
  337. tmpElement2 = *head;
  338. while (tmpElement2->nextStudent != currentElement)
  339. tmpElement2 = tmpElement2->nextStudent;
  340. tmpElement2->nextStudent = currentElement->nextStudent;
  341. if (currentElement->nextStudent != NULL) {
  342. currentElement->nextStudent->prevStudent = tmpElement2;
  343. }
  344. }
  345. }
  346. if (currentElement != NULL) {
  347. currentElement = currentElement->nextStudent;
  348. }
  349. if (currentElement == NULL || *head == *tail) {
  350. break;
  351. }
  352. flag = true;
  353. }
  354. }
  355. return *head;
  356. }
  357. int main() {
  358. student *head = NULL, *tail = NULL;
  359. int typeOfOption = 0, element, newValue, newElement, numberOfStudents;
  360. bool isElement = false;
  361. printf("Choose option : \n1)Set list of students\n2)Add element in head\n3)Add element in tail\n4)Show elements\n5)Delete all elements\n6)Number of element's in list\n7)Is empty?\n8)Delete 1st element\n9)Delete last element\n10)Search 1 element\n11)Delete element\n12)Search minimal and maximal element\n13)Delete all elements with this value\n14)Replace value of all choosen elements\n15)Remove duplicate elements\n16)Exit\n");
  362. while (typeOfOption != 16) {
  363. printf("Enter option number\n");
  364. while ((scanf_s("%d", &typeOfOption)) != 1 || typeOfOption <= 0 || typeOfOption >= 17) {
  365. rewind(stdin);
  366. printf("Incorrect,reenter!\n");
  367. }
  368. switch (typeOfOption) {
  369. case 1: {
  370. if (isEmpty(head) == true) {
  371. head = setList(&head, &tail);
  372. }
  373. else {
  374. printf("Your already have list\n");
  375. }
  376. break;
  377. }
  378. case 2: {
  379. if (isEmpty(head) == true) {
  380. printf("List not created\n");
  381. }
  382. else {
  383. head = addElementInHead(&head);
  384. }
  385. break;
  386. if (isEmpty(head) == true) {
  387. printf("List not created\n");
  388. }
  389. else {
  390. tail = addElementInTail(&tail);
  391. }
  392. break;
  393. }
  394. case 3: {
  395. if (isEmpty(head) == true) {
  396. printf("List not created\n");
  397. }
  398. else {
  399. tail = addElementInTail(&tail);
  400. }
  401. break;
  402. }
  403. case 4: {
  404. if (isEmpty(head) == true) {
  405. printf("List not created\n");
  406. }
  407. else {
  408. showStudent(head);
  409. }
  410. break;
  411. }
  412. case 5: {
  413. if (isEmpty(head) == true) {
  414. printf("List not created\n");
  415. }
  416. else {
  417. head = deleteAllElements(&head, &tail);
  418. }
  419. break;
  420. }
  421. case 6: {
  422. printf("Elements in your list - %d\n", numberOfStudents = NumberOfElements(head));
  423. break;
  424. }
  425. case 7: {
  426. if ((isEmpty(head)) == true) {
  427. printf("Your list is empty\n");
  428. }
  429. else {
  430. printf("Your list is not empty\n");
  431. }
  432. break;
  433. }
  434. case 8: {
  435. if (isEmpty(head) == true) {
  436. printf("List not created\n");
  437. }
  438. else {
  439. head = deleteHead(&head);
  440. }
  441. break;
  442. }
  443. case 9: {
  444. if (isEmpty(head) == true) {
  445. printf("List not created\n");
  446. }
  447. else {
  448. tail = deleteTail(&tail);
  449. }
  450. break;
  451. }
  452. case 10: {
  453. if (isEmpty(head) == true) {
  454. printf("List not created\n");
  455. }
  456. else {
  457. printf("Enter value of element you want to search\n");
  458. newElement = searchElement(head);
  459. isElement = true;
  460. }
  461. break;
  462. }
  463. case 11: {
  464. if (isEmpty(head) == true) {
  465. printf("List not created\n");
  466. }
  467. else if (isElement == true) {
  468. head = deleteElement(newElement, &head, &tail);
  469. printf("Element deleted\n");
  470. }
  471. else {
  472. printf("No element's to search,type 10 on keyboard to choose what to find\n");
  473. }
  474. isElement = false;
  475. break;
  476. }
  477. case 12: {
  478. if (isEmpty(head) == true) {
  479. printf("List not created\n");
  480. }
  481. else {
  482. showMinAndMaxElements(head);
  483. }
  484. break;
  485. }
  486. case 13: {
  487. if (isEmpty(head) == true) {
  488. printf("List not created\n");
  489. }
  490. else if(isElement == true){
  491. head = deleteAllElementsWithThisValue(newElement, &head, &tail);
  492. }
  493. else {
  494. printf("No element's to search,type 10 on keyboard to choose what to find\n");
  495. }
  496. isElement = false;
  497. break;
  498. }
  499. case 14: {
  500. if (isEmpty(head) == true) {
  501. printf("List not created\n");
  502. }
  503. else if(isElement == true){
  504. printf("Enter new value for elements\n");
  505. while ((scanf_s("%d", &newValue)) != 1) {
  506. printf("Incorrect information,reenter!\n");
  507. rewind(stdin);
  508. }
  509. head = replaceValueOfElements(newElement, newValue, head);
  510. }
  511. else {
  512. printf("No element's to search,type 10 on keyboard to choose what to find\n");
  513. }
  514. isElement = false;
  515. break;
  516. }
  517. case 15: {
  518. if (isEmpty(head) == true) {
  519. printf("List not created\n");
  520. }
  521. else {
  522. head = removeDuplicateElements(&head, &tail);
  523. }
  524. break;
  525. }
  526. }
  527. }
  528. return 0;
  529. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement