Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stddef.h>
- struct list
- {
- struct list *next;
- int key;
- };
- struct list* new_list(int key)
- {
- struct list *L = malloc(sizeof(*L));
- L->key = key;
- L->next = NULL;
- return L;
- }
- struct list *delete(struct list *l, int key)
- {
- if (l == NULL)
- return NULL;
- while (l != NULL && l->key == key)
- {
- struct list *tmp = l->next;
- free(l);
- l = tmp;
- }
- if (l == NULL)
- return NULL;
- struct list *extra = l;
- while (l->next != NULL)
- {
- while (l->next != NULL && l->next->key == key)
- {
- struct list *tmp = l->next->next;
- free(l->next);
- l->next = tmp;
- }
- l = l->next;
- if (l == NULL)
- break;
- }
- return extra;
- }
- int main(void)
- {
- FILE* in = fopen("input.txt", "r");
- FILE* out = fopen("output.txt", "w");
- int x = 0;
- fscanf(in, "%d", &x);
- struct list *h1, *c1;
- if (x == -1)
- {
- h1 = NULL;
- c1 = NULL;
- }
- else
- {
- h1 = new_list(x);
- c1 = h1;
- for (fscanf(in, "%d", &x); x != -1; fscanf(in, "%d", &x))
- {
- h1->next = new_list(x);
- h1 = h1->next;
- }
- }
- for (fscanf(in, "%d", &x); x != -1 && c1 != NULL; fscanf(in, "%d", &x))
- c1 = delete(c1, x);
- struct list *tmp;
- for (tmp = c1; tmp != NULL && tmp->key != -1; tmp = tmp->next)
- fprintf(out, "%d ", tmp->key);
- fclose(in);
- fclose(out);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement