Advertisement
Guest User

Untitled

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