SHARE
TWEET

Untitled

a guest Sep 18th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. elem * add(elem *head, int x) {
  11.     elem *cur = head;
  12.     if (cur == NULL) {
  13.         elem *new_elem = (elem*)malloc(sizeof(elem));
  14.         new_elem->value = x;
  15.         new_elem->right = NULL;
  16.         new_elem->left = NULL;
  17.         return new_elem;
  18.     }
  19.     while (cur->right != NULL) {
  20.         cur = cur->right;
  21.     }
  22.     elem *new_elem = (elem*)malloc(sizeof(elem));
  23.     new_elem->value = x;
  24.     new_elem->right = NULL;
  25.     new_elem->left = cur;
  26.     cur->right = new_elem;
  27.     return head;
  28. }
  29.  
  30. elem* delite(elem *head, int x) {
  31.     elem *cur = head;
  32.     while (cur->value != x) {
  33.         cur = cur->right;
  34.     }
  35.     if (cur == NULL) {
  36.         return head;
  37.     }
  38.     elem* new_head = head;
  39.     if (cur->left != NULL) {
  40.         (cur->left)->right = cur->right;
  41.     }
  42.     else {
  43.         new_head = cur->right;
  44.     }
  45.     if (cur->right != NULL) {
  46.         (cur->right)->left = cur->left;
  47.     }
  48.     free(cur);
  49.     return new_head;
  50. }
  51.  
  52. void print_list(elem * head) {
  53.     elem * cur = head;
  54.     while (cur != NULL) {
  55.         printf("%d ", cur->value);
  56.         cur = cur -> right;
  57.     }
  58.     printf("\n");
  59. }
  60.  
  61. int main() {
  62.     elem *head = NULL;
  63.     char c;
  64.     while (c != 'q') {
  65.         scanf("%c", &c);
  66.         if (c == '+') {
  67.             int m;
  68.             scanf("%d", &m);
  69.             head = add(head, m);
  70.         }
  71.         if (c == '-') {
  72.             int m;
  73.             scanf("%d", &m);
  74.             head = delite(head, m);
  75.         }
  76.         if (c == '!') {
  77.             print_list(head);
  78.         }
  79.     }
  80.     return 0;
  81. }
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