Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct elem elem;
  5. typedef struct elem {
  6.     int value;
  7.     elem *right, *left;
  8. }elem;
  9.  
  10. void add(elem *head, int x) {
  11.     elem *cur = head;
  12.     while (cur->right != NULL) {
  13.         cur = cur->right;
  14.     }
  15.     elem *new_elem = (elem*)malloc(sizeof(elem));
  16.     new_elem->value = x;
  17.     new_elem->right = NULL;
  18.     new_elem->left = cur;
  19.     cur->right = new_elem;
  20. }
  21.  
  22. void delite(elem *head, int x) {
  23.     elem *cur = head;
  24.     while (cur->value != x) {
  25.         cur = cur->right;
  26.     }
  27.     if (cur == NULL) {
  28.         return;
  29.     }
  30.     if (cur->left != NULL) {
  31.         (cur->left)->right = cur->right;
  32.     }
  33.     if (cur->right != NULL) {
  34.         (cur->right)->left = cur->left;
  35.     }
  36.     free(cur);
  37. }
  38.  
  39. void print_list(elem * head) {
  40.     elem * cur = head;
  41.     cur = cur -> right;
  42.     while (cur != NULL) {
  43.         printf("%d ", cur->value);
  44.         cur = cur -> right;
  45.     }
  46.     printf("\n");
  47. }
  48.  
  49. int main() {
  50.     elem *head = (elem*)malloc(sizeof(elem));
  51.     head->value = 0;
  52.     head->right = NULL;
  53.     head->left = NULL;
  54.     char c;
  55.     while (c != 'q') {
  56.         scanf("%c", &c);
  57.         if (c == '+') {
  58.             int m;
  59.             scanf("%d", &m);
  60.             add(head, m);
  61.         }
  62.         if (c == '-') {
  63.             int m;
  64.             scanf("%d", &m);
  65.             delite(head, m);
  66.         }
  67.         if (c == '!') {
  68.             print_list(head);
  69.         }
  70.     }
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement