Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stddef.h>
  4.  
  5. struct list
  6. {
  7.     struct list *next;
  8.     int key;
  9. };
  10. struct list* new_list(int key)
  11. {
  12.     struct list *L = malloc(sizeof(*L));
  13.     L->key = key;
  14.     L->next = NULL;
  15.     return L;
  16. }
  17.  
  18. struct list *delete(struct list *l, int key)
  19. {
  20.     if (l == NULL)
  21.         return NULL;
  22.     while (l != NULL && l->key == key)
  23.     {
  24.         struct list *tmp = l->next;
  25.         free(l);
  26.         l = tmp;
  27.     }
  28.     if (l == NULL)
  29.         return NULL;
  30.     struct list *extra = l;
  31.     while (l->next != NULL)
  32.     {
  33.         while (l->next != NULL && l->next->key == key)
  34.         {
  35.             struct list *tmp = l->next->next;
  36.             free(l->next);
  37.             l->next = tmp;
  38.         }
  39.         l = l->next;
  40.         if (l == NULL)
  41.             break;
  42.     }
  43.     return extra;
  44. }
  45.  
  46. int main(void)
  47. {
  48.     FILE* in = fopen("input.txt", "r");
  49.     FILE* out = fopen("output.txt", "w");
  50.  
  51.     int x = 0;
  52.     fscanf(in, "%d", &x);
  53.     struct list *h1, *c1;
  54.     if (x == -1)
  55.     {
  56.         h1 = NULL;
  57.         c1 = NULL;
  58.     }
  59.     else
  60.     {
  61.         h1 = new_list(x);
  62.         c1 = h1;
  63.         for (fscanf(in, "%d", &x); x != -1; fscanf(in, "%d", &x))
  64.         {
  65.             h1->next = new_list(x);
  66.             h1 = h1->next;
  67.         }
  68.     }
  69.     for (fscanf(in, "%d", &x); x != -1 && c1 != NULL; fscanf(in, "%d", &x))
  70.         c1 = delete(c1, x);
  71.  
  72.     struct list *tmp;
  73.     for (tmp = c1; tmp != NULL && tmp->key != -1; tmp = tmp->next)
  74.         fprintf(out, "%d ", tmp->key);
  75.  
  76.     fclose(in);
  77.     fclose(out);
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement