Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #include"clist.h"
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<stddef.h>
  5. #include<string.h>
  6.  
  7.  
  8. struct point {
  9. int x, y;
  10. struct intrusive_node node;
  11. };
  12.  
  13. void add_point(struct intrusive_list *list, int x, int y) {
  14. struct point *new_node = malloc(sizeof(struct point));
  15. new_node->x = x;
  16. new_node->y = y;
  17. add_node(list, &new_node->node);
  18. }
  19.  
  20. void remove_point(struct intrusive_list *list, int x, int y) {
  21. struct intrusive_node *ptr = list->head;
  22. while (ptr->next) {
  23. struct point *rm_point = container_of(ptr->next, struct point, node);
  24. if (rm_point->x == x && rm_point->y == y) {
  25. remove_node(ptr->next);
  26. } else {
  27. ptr = ptr->next;
  28. }
  29. }
  30. }
  31.  
  32. void show_all_points(struct intrusive_list *list) {
  33. struct intrusive_node *ptr = list->head;
  34. while (ptr->next) {
  35. struct point *show_point = container_of(ptr->next, struct point, node);
  36. printf("(%d %d) ", show_point->x, show_point->y);
  37. ptr = ptr->next;
  38. }
  39. }
  40.  
  41. void remove_all_points(struct intrusive_list *list) {
  42. struct intrusive_node *ptr = list->head;
  43. while (ptr->next) {
  44. struct point *rm_point = container_of(ptr, struct point, node);
  45. remove_node(rm_point->node.next);
  46.  
  47. }
  48. }
  49.  
  50. int main() {
  51. struct intrusive_list list;
  52. init_list(&list);
  53. char str[256];
  54. int x, y;
  55. while (1 == 1) {
  56. scanf("%255s", str);
  57. if (strcmp(str, "exit") == 0) {
  58. break;
  59. }
  60. if (strcmp(str, "rma") == 0) {
  61. remove_all_points(&list);
  62. continue;
  63. }
  64. if (strcmp(str, "len") == 0) {
  65. printf("%d\n", get_length(&list));
  66. continue;
  67. }
  68. if (strcmp(str, "print") == 0) {
  69. show_all_points(&list);
  70. continue;
  71. }
  72. if (strcmp(str, "add") == 0) {
  73. scanf("%i%i", &x, &y);
  74. add_point(&list, x, y);
  75. continue;
  76. }
  77. if (strcmp(str, "rm") == 0) {
  78. scanf("%d%d", &x, &y);
  79. remove_point(&list, x, y);
  80. continue;
  81. }
  82. printf("Unknown command\n");
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement