Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. /* Node of a doubly linked list */
  6. // A complete working C program to demonstrate all insertion methods
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. typedef struct {
  11. char name[80];
  12. char author[80];
  13. int year;
  14. } Item;
  15.  
  16. // A linked list node
  17. struct Node {
  18. Item data;
  19. struct Node *next;
  20. struct Node *prev;
  21. };
  22.  
  23. //*Функция для создания элемента списка (тип элемента MusicalComposition)**/
  24. struct Node *getNewNode(char *name, char *author, int year) {
  25. struct Node *newNode = (struct Node *) malloc(sizeof(struct Node));
  26. newNode->next = NULL;
  27. newNode->prev = NULL;
  28. strcpy(newNode->data.name, name);
  29. strcpy(newNode->data.author, author);
  30. newNode->data.year = year;
  31. return newNode;
  32. }
  33.  
  34. /* Given a reference (pointer to pointer) to the head of a list
  35. and an int, inserts a new node on the front of the list. */
  36. //void push(struct Node** head_ref,struct Node* node_to_insert)
  37. //{
  38. // /* 1. allocate node */
  39. // struct Node* new_node = node_to_insert;
  40. //
  41. // /* 3. Make next of new node as head and previous as NULL */
  42. // new_node->next = (*head_ref);
  43. // new_node->prev = NULL;
  44. //
  45. // /* 4. change prev of head node to new node */
  46. // if((*head_ref) != NULL)
  47. // (*head_ref)->prev = new_node ;
  48. //
  49. // /* 5. move the head to point to the new node */
  50. // (*head_ref) = new_node;
  51. // printf("%s",(*head_ref)->data.name);
  52. //}
  53.  
  54. //* Given a node as prev_node, insert a new node after the given node */
  55. void insertAfter(struct Node *prev_node, struct Node *node_to_insert) {
  56. /*1. check if the given prev_node is NULL */
  57. if (prev_node == NULL) {
  58. printf("the given previous node cannot be NULL");
  59. return;
  60. }
  61. /* 2. allocate new node */
  62. struct Node *new_node = node_to_insert;
  63.  
  64. /* 3. put in the data */
  65. // new_node->data = new_data;
  66.  
  67. /* 4. Make next of new node as next of prev_node */
  68. new_node->next = prev_node->next;
  69.  
  70. /* 5. Make the next of prev_node as new_node */
  71. prev_node->next = new_node;
  72.  
  73. /* 6. Make prev_node as previous of new_node */
  74. new_node->prev = prev_node;
  75.  
  76. /* 7. Change previous of new_node's next node */
  77. if (new_node->next != NULL)
  78. new_node->next->prev = new_node;
  79. }
  80.  
  81. //* #3 добавляет element в конец списка musical_composition_list//
  82. void append(struct Node **head_ref, struct Node *node_to_insert) {
  83. /* 1. allocate node */
  84. // struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
  85.  
  86. struct Node *last = *head_ref; /* used in step 5*/
  87.  
  88. /* 2. put in the data */
  89. struct Node *new_node = node_to_insert;
  90.  
  91. /* 3. This new node is going to be the last node, so
  92. make next of it as NULL*/
  93. new_node->next = NULL;
  94.  
  95. /* 4. If the Linked List is empty, then make the new
  96. node as head */
  97. if (*head_ref == NULL) {
  98. printf("%s", "THIS!");
  99. new_node->prev = NULL;
  100. *head_ref = new_node;
  101. return;
  102. }
  103.  
  104. /* 5. Else traverse till the last node */
  105. while (last->next != NULL)
  106. last = last->next;
  107.  
  108. /* 6. Change the next of last node */
  109. last->next = new_node;
  110.  
  111. /* 7. Make last node as previous of new node */
  112. new_node->prev = last;
  113.  
  114. return;
  115. }
  116.  
  117. // #6 Выводит названия композиций /
  118. void printList(struct Node *head) {
  119. struct Node *currentNode = head;
  120. if (currentNode == NULL) return;
  121. while (currentNode != NULL) {
  122. printf(" %s ", currentNode->data.name);
  123. currentNode = currentNode->next;
  124. }
  125. printf("\n");
  126. }
  127.  
  128. //
  129. struct Node *getNodeByIndex(struct Node *head, int index) {
  130. struct Node *tmp = head;
  131. int i = 0;
  132. while (tmp && i < index) {
  133. tmp = tmp->next;
  134. i++;
  135. }
  136.  
  137. return tmp;
  138. }
  139.  
  140. int getIndexByName(struct Node *head, char *name) {
  141. int index = 0;
  142. struct Node *tmp = head;
  143. struct Node *next = NULL;
  144. while (tmp) {
  145. if (strcmp(name, tmp->data.name) == 0) return index;
  146. next = tmp->next;
  147. tmp = next;
  148. index++;
  149. }
  150. return -1;
  151. }
  152.  
  153. /* Function to delete a node in a Doubly Linked List.
  154. head_ref --> pointer to head node pointer.
  155. del --> pointer to node to be deleted. */
  156. void deleteNode(struct Node **head_ref, struct Node *del) {
  157. /* base case */
  158. if (*head_ref == NULL || del == NULL)
  159. return;
  160.  
  161. /* If node to be deleted is head node */
  162. if (*head_ref == del)
  163. *head_ref = del->next;
  164.  
  165. /* Change next only if node to be deleted is NOT the last node */
  166. if (del->next != NULL)
  167. del->next->prev = del->prev;
  168.  
  169. /* Change prev only if node to be deleted is NOT the first node */
  170. if (del->prev != NULL)
  171. del->prev->next = del->next;
  172.  
  173. /* Finally, free the memory occupied by del*/
  174. free(del);
  175.  
  176. }
  177.  
  178.  
  179. void deleteNodeByIndex(struct Node **head_ref, int index) {
  180. deleteNode(head_ref, getNodeByIndex(*head_ref, index));
  181. }
  182.  
  183. //* #4 удаляет элемент element списка, у которого значение name равно значению name_for_remove*/
  184. void deleteNodeByName(struct Node **head_ref, char *name) {
  185. deleteNodeByIndex(head_ref, getIndexByName(*head_ref, name));
  186. }
  187.  
  188. // Создает двусвязный списоk, returns ptr to HEAD
  189. struct Node *createDLL() {
  190. struct Node *head = malloc(sizeof(struct Node));
  191. head->next = NULL;
  192. head->prev = NULL;
  193. return head;
  194. }
  195.  
  196. //* #2 создает список музыкальных композиций MusicalCompositionList*/
  197. struct Node *getListOfNodes(char **array_names, char **array_authors, const int *array_years, int n) {
  198. struct Node *head = NULL;
  199. int i;
  200. for (i = 0; i < n; i++) {
  201. struct Node *insertedNode = malloc(sizeof(struct Node));
  202. int len = strlen(array_names[i]);
  203. strcpy(insertedNode->data.author, array_authors[i]);
  204. strcpy(insertedNode->data.name, array_names[i]);
  205. insertedNode->data.year = array_years[i];
  206. append(&head, insertedNode);
  207. }
  208. return head;
  209. }
  210.  
  211. //* #5 возвращает количество элементов списка */
  212. int count(struct Node *head) {
  213. int count = 0;
  214. struct Node *tmp = head;
  215. struct Node *next = NULL;
  216. while (tmp) {
  217. count++;
  218. next = tmp->next;
  219. tmp = next;
  220. }
  221. return count;
  222. }
  223. /*sorting */
  224. struct Node *split(struct Node *head);
  225.  
  226. // Function to merge two linked lists
  227. struct Node *merge(struct Node *first, struct Node *second)
  228. {
  229. // If first linked list is empty
  230. if (!first)
  231. return second;
  232.  
  233. // If second linked list is empty
  234. if (!second)
  235. return first;
  236.  
  237. // Pick the smaller value
  238. if (first->data.year > second->data.year)
  239. {
  240. first->next = merge(first->next,second);
  241. first->next->prev = first;
  242. first->prev = NULL;
  243. return first;
  244. }
  245. else
  246. {
  247. second->next = merge(first,second->next);
  248. second->next->prev = second;
  249. second->prev = NULL;
  250. return second;
  251. }
  252. }
  253.  
  254. // Function to do merge sort
  255. struct Node *mergeSort(struct Node *head)
  256. {
  257. if (!head || !head->next)
  258. return head;
  259. struct Node *second = split(head);
  260.  
  261. // Recur for left and right halves
  262. head = mergeSort(head);
  263. second = mergeSort(second);
  264.  
  265. // Merge the two sorted halves
  266. return merge(head,second);
  267. }
  268.  
  269. // Utility function to swap two integers
  270. void swap(int *A, int *B)
  271. {
  272. int temp = *A;
  273. *A = *B;
  274. *B = temp;
  275. }
  276.  
  277. // Split a doubly linked list (DLL) into 2 DLLs of
  278. // half sizes
  279. struct Node *split(struct Node *head)
  280. {
  281. struct Node *fast = head,*slow = head;
  282. while (fast->next && fast->next->next)
  283. {
  284. fast = fast->next->next;
  285. slow = slow->next;
  286. }
  287. struct Node *temp = slow->next;
  288. slow->next = NULL;
  289. return temp;
  290. }
  291.  
  292. /*
  293. char **add_string(char **array, const char *word)
  294. {
  295.  
  296. char *word[80];
  297. char *term = ":"; // termination char
  298. char *prnt = "print";
  299.  
  300. while (strcmp(term, word) != 0)
  301. {
  302.  
  303. scanf("%s", word);
  304. if (strcmp(term, word) == 0)
  305. {
  306. printf("Terminate\n");
  307. }
  308.  
  309. else if (strcmp(prnt, word) == 0)
  310. {
  311. printf("Enumerate\n");
  312.  
  313. int i;
  314.  
  315. for (i=0; i<count; i++)
  316. {
  317. printf("Slot %d: %s\n",i, array[i]);
  318. }
  319.  
  320. }
  321. else
  322. {
  323. printf("String added to array\n");
  324. count++;
  325. array = (char**)realloc(array, (count+1)*sizeof(*array));
  326. array[count-1] = (char*)malloc(sizeof(word));
  327. strcpy(array[count-1], word);
  328. }
  329.  
  330. }
  331. }
  332. */
  333.  
  334.  
  335.  
  336.  
  337. char** add_string(char** array, int* size, const char* string)
  338. { printf("%s", size);
  339. printf("%s", string);
  340. char* newArray = realloc(array, (*size + 1) *sizeof(char*) );
  341. newArray[*size] = malloc(strlen(string)+1);
  342. strcpy(newArray[*size], string);
  343. *size += 1;
  344. }
  345.  
  346.  
  347.  
  348. int main() {
  349.  
  350. struct Node *head = NULL;
  351. int array_size =0;
  352. int len = 1;
  353. int i = 0;
  354. int j = 0, k = 0;
  355. int count_till_three = 0;
  356. int c;
  357. char* buffer = NULL;
  358.  
  359. int curr_year = -1;
  360. char* curr_name;
  361. char* curr_author;
  362.  
  363. while((c = getchar()) != EOF){
  364. buffer = realloc(buffer,len*sizeof(char));
  365. buffer[i] =(char)c;
  366. i++;
  367. len++;
  368. if(c == ':') {
  369. if(count_till_three == 0){
  370. curr_name = malloc(sizeof(char)*strlen(buffer));
  371. strcpy(curr_name,buffer);
  372.  
  373. }
  374. if(count_till_three == 1){
  375. curr_author = (char*)malloc(sizeof(char)*strlen(buffer));
  376.  
  377. strcpy(curr_author,buffer);
  378.  
  379. }
  380. count_till_three++;
  381. free(buffer);
  382. buffer = NULL;
  383. len =1;
  384. i = 0;
  385. }
  386. if(c == '\n'){
  387. curr_year = atoi(buffer);
  388. free(buffer);
  389. buffer = NULL;
  390. len =1;
  391. i = 0;
  392. count_till_three = 0;
  393.  
  394. }
  395.  
  396. if(curr_year != -1) {
  397.  
  398. struct Node *insertedNode = malloc(sizeof(struct Node));
  399. printf("%s",curr_author);
  400. strcpy(insertedNode->data.author, curr_author);
  401. strcpy(insertedNode->data.name, curr_name);
  402. insertedNode->data.year = curr_year;
  403. append(&head, insertedNode);
  404.  
  405.  
  406. curr_year = -1;
  407.  
  408. free(curr_author);
  409. free(curr_name);
  410. curr_author = NULL;
  411. curr_name = NULL;
  412. count_till_three = 0;
  413. break;
  414. }
  415. }
  416. printf("%s", "HERsE!");
  417. printList(head);
  418. // /// Future House:FrankJavCee:2015//
  419. printf("%s", "HsdadsdasdasdaERsE!");
  420.  
  421. return 0;
  422. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement