Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define MAXVAL 100
  7.  
  8. int to_int(char array) {
  9. return ((int)array - '0');
  10. }
  11.  
  12. typedef struct Node {
  13. unsigned char name[MAXVAL];
  14. unsigned char address[MAXVAL];
  15. int number[MAXVAL];
  16. struct Node *next;
  17. }Node;
  18.  
  19. typedef struct {
  20. Node *head;
  21. } list;
  22.  
  23. void add_new(Node **head) {
  24. Node *tmp = (Node*)malloc(sizeof(Node));
  25. unsigned char name[MAXVAL];
  26. unsigned char address[MAXVAL];
  27. char number[MAXVAL];
  28.  
  29. printf("name:");
  30. gets_s(name, MAXVAL);
  31. strcpy(tmp->name, name);
  32.  
  33. printf("address:");
  34. gets_s(address, MAXVAL);
  35. strcpy(tmp->address, address);
  36.  
  37. printf("number:");
  38. gets_s(number, MAXVAL);
  39. int i = 0;
  40. while (1) {
  41. if (number[i] == '\0') break;
  42. tmp->number[i] = to_int(number[i]);
  43. i++;
  44. }
  45. tmp->next = (*head);
  46. (*head) = tmp;
  47. }
  48.  
  49. void list_foreach(list *list, void(*function)(Node** head)) //применаем функцию function к каждому узлу списка
  50. {
  51. Node *tmp = list->head;
  52. while (tmp!= NULL) {
  53. function(&tmp);
  54. tmp = tmp->next;
  55. }
  56. }
  57.  
  58. int list_find(Node* list, int(*cmp_func)(char*, char*),unsigned char* value) { // возвращает тот узел, для которого результат функции сравнения равен нулю
  59. int i = 0,counter=0;
  60. while (list!= NULL) {
  61. if ((*cmp_func)(value, list->name) == 0) {
  62. printf("name:");
  63. puts(list->name);
  64. printf("address:");
  65. puts(list->address);
  66. printf("number:");
  67. while (list->number[i] >= 0) {
  68. printf("%d", list->number[i++]);
  69. }
  70. printf("\n");
  71. return counter;
  72. }
  73. else {
  74. list = list->next;
  75. counter++;
  76. }
  77. }
  78. return -1;
  79. }
  80.  
  81. int cmp(const unsigned char* a,const unsigned char* b) {
  82. return strcmp(a, b);
  83. }
  84.  
  85. void print_all(Node** head) {
  86. int i = 0;
  87. printf("\n");
  88. printf("name:");
  89. puts((*head)->name);
  90. printf("address:");
  91. puts((*head)->address);
  92. printf("number:");
  93. while ((*head)->number[i] >= 0) {
  94. printf("%d", (*head)->number[i++]);
  95. }
  96. printf("\n");
  97. }
  98.  
  99. Node* getNth(Node* head, int n) {
  100. int counter = 1;
  101. while (counter < n && head) {
  102. head = head->next;
  103. counter++;
  104. }
  105. return head;
  106. }
  107.  
  108. void deleteNth(Node **head, int n) {
  109. if (n == 0) {
  110. Node *tmp = *head;
  111. (*head) = (*head)->next;
  112. free(tmp);
  113. }
  114. else {
  115. Node *prev = getNth(*head, n - 1);
  116. Node *elm = prev->next;
  117. prev->next = elm->next;
  118. free(elm);
  119. }
  120. printf("deleted");
  121. }
  122.  
  123. int main(int argc, char** argv) {
  124. char buffer[MAXVAL];
  125. Node* head = malloc(sizeof(Node));
  126. head = NULL;
  127. list* tmp = malloc(sizeof(list));
  128. if (argc > 1 && strcmp(argv[1], "-i") != 0) {
  129. printf("neizvestnyi kluch");
  130. return 0;
  131. }
  132. if (argc == 2 && !strcmp(argv[1], "-i")) { //если введён ключ -i выполнять программу до ввода exit
  133. while (1) {
  134. gets_s(buffer, MAXVAL);
  135. if (strcmp(buffer, "exit") == 0)
  136. return 0;
  137. if (strcmp(buffer, "add") == 0)
  138. add_new(&head);
  139. if (strcmp(buffer, "printall") == 0) {
  140. tmp->head = head;
  141. list_foreach(tmp, &print_all);
  142. }
  143. if (strcmp(buffer, "find") == 0) {
  144. char example[MAXVAL];
  145. gets_s(example, MAXVAL);
  146. list_find(head, &cmp, example);
  147. }
  148. if (strcmp(buffer, "delete") == 0) {
  149. tmp->head = head;
  150. char example[MAXVAL];
  151. gets_s(example, MAXVAL);
  152. int position = list_find(head, &cmp, example);
  153. deleteNth(&head, position);
  154. }
  155. }
  156. return 0;
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement