SHARE
TWEET

Untitled

a guest Dec 9th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. typedef struct Node
  7. {
  8.     int data;
  9.     struct Node *next_ptr;
  10. } Node;
  11.  
  12.  
  13. typedef struct List {
  14.     size_t size;
  15.     Node *head;
  16.     Node *tail;
  17. } List;
  18.  
  19.  
  20. List *init()
  21. {
  22.     List *newList = (List*)malloc(sizeof(List));
  23.     newList->size = 0;
  24.     newList->head = NULL;
  25.     newList->tail = NULL;
  26.     return newList;
  27. }
  28.  
  29. void push_front(List *list, int _data)
  30. {
  31.     Node *temp = (Node*)malloc(sizeof(Node));
  32.     temp->data = _data;
  33.     temp->next_ptr = list->head;
  34.     if (list->head == NULL)
  35.         list->tail = temp;
  36.     list->head = temp;
  37.     list->size++;
  38. }
  39.  
  40. void print_data(List *list)
  41. {
  42.     for (Node *temp = list->head; temp != NULL; temp = temp->next_ptr)
  43.         printf("%d ", temp->data);
  44. }
  45.  
  46.  
  47. int pop(List *list, Node *del)
  48. {
  49.     Node *temp = del->next_ptr;
  50.     if (temp == NULL)
  51.     {
  52.  
  53.     }
  54.     del->next_ptr = temp->next_ptr;
  55.     int res = temp->data;
  56.     free(temp);
  57.     list->size--;
  58.     return res;
  59. }
  60.  
  61. int main()
  62. {
  63.     freopen("input.txt", "r", stdin);
  64.     freopen("output.txt", "w", stdout);
  65.     List *list = init();
  66.     int data = 0;
  67.     int num = 0;
  68.     scanf("%d", &num);
  69.     for (; scanf("%d", &data) == 1;)
  70.     {
  71.         push_front(list, data);
  72.     }
  73.  
  74.     for (Node *i = list->head, *prev = i; i->next_ptr != NULL; )
  75.     {
  76.         if (i->next_ptr->data == num && i != list->head)
  77.         {
  78.             Node *temp = i->next_ptr;
  79.             pop(list, prev);
  80.             i = temp;
  81.             continue;
  82.         }
  83.         if (i->next_ptr->data == num && i == list->head)
  84.         {
  85.             Node *temp = list->head;
  86.             list->head = temp->next_ptr;
  87.             free(temp);
  88.             i = list->head;
  89.             prev = i;
  90.             continue;
  91.         }
  92.         if (i != list->head)
  93.             prev = prev->next_ptr;
  94.         i = i->next_ptr;
  95.     }
  96.     print_data(list);
  97.  
  98.     return 0;
  99. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top