Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include"clist.h"
- #include<stdio.h>
- #include<stdlib.h>
- #include<stddef.h>
- #include<string.h>
- struct point {
- int x, y;
- struct intrusive_node node;
- };
- void add_point(struct intrusive_list *list, int x, int y) {
- struct point *new_node = malloc(sizeof(struct point));
- new_node->x = x;
- new_node->y = y;
- add_node(list, &new_node->node);
- }
- void remove_point(struct intrusive_list *list, int x, int y) {
- struct intrusive_node *ptr = list->head;
- while (ptr->next) {
- struct point *rm_point = container_of(ptr->next, struct point, node);
- if (rm_point->x == x && rm_point->y == y) {
- remove_node(ptr->next);
- } else {
- ptr = ptr->next;
- }
- }
- }
- void show_all_points(struct intrusive_list *list) {
- struct intrusive_node *ptr = list->head;
- while (ptr->next) {
- struct point *show_point = container_of(ptr->next, struct point, node);
- printf("(%d %d) ", show_point->x, show_point->y);
- ptr = ptr->next;
- }
- }
- void remove_all_points(struct intrusive_list *list) {
- struct intrusive_node *ptr = list->head;
- while (ptr->next) {
- struct point *rm_point = container_of(ptr, struct point, node);
- remove_node(rm_point->node.next);
- }
- }
- int main() {
- struct intrusive_list list;
- init_list(&list);
- char str[256];
- int x, y;
- while (1 == 1) {
- scanf("%255s", str);
- if (strcmp(str, "exit") == 0) {
- break;
- }
- if (strcmp(str, "rma") == 0) {
- remove_all_points(&list);
- continue;
- }
- if (strcmp(str, "len") == 0) {
- printf("%d\n", get_length(&list));
- continue;
- }
- if (strcmp(str, "print") == 0) {
- show_all_points(&list);
- continue;
- }
- if (strcmp(str, "add") == 0) {
- scanf("%i%i", &x, &y);
- add_point(&list, x, y);
- continue;
- }
- if (strcmp(str, "rm") == 0) {
- scanf("%d%d", &x, &y);
- remove_point(&list, x, y);
- continue;
- }
- printf("Unknown command\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement