SHARE
TWEET

Untitled

a guest Mar 19th, 2019 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4.  
  5. struct list
  6. {
  7.    char el;
  8.    list *next, *pred;
  9. };
  10.  
  11. struct  dline
  12. {
  13.    list *beg;
  14. };
  15.  
  16. void create(dline **d);
  17. int isEmpty(dline *d);
  18. void push(dline **st, char x);
  19. int pop(dline **d, char *x);
  20.  
  21. int main()
  22. {
  23.    char x;
  24.    dline *d;
  25.    create(&d);
  26.    push(&d, 1);
  27.    push(&d, 2);
  28.    push(&d, 3);
  29.    pop(&d, &x);
  30.    _getch();
  31.    return 0;
  32. }
  33.  
  34. void create(dline **d)
  35. {
  36.    *d = new dline;
  37.    (*d)->beg = new list;
  38.    (*d)->beg->next = (*d)->beg;
  39.    (*d)->beg->pred = (*d)->beg;
  40. }
  41.  
  42. int isEmpty(dline *d)
  43. {
  44.    return d->beg->next == d->beg;
  45. }
  46.  
  47. void push(dline **d, char x)
  48. {
  49.    list **r = &(*d)->beg;
  50.    if (isEmpty(*d))
  51.    {
  52.       (*r)->el = x;
  53.       (*r)->pred = new list;
  54.       (*r)->pred->next = *r;
  55.       (*r)->pred->pred = *r;
  56.       (*r)->next = (*r)->pred;
  57.       (*d)->beg = (*r)->pred;
  58.    }
  59.    else
  60.    {
  61.       (*r)->el = x;
  62.       (*r)->next->pred = new list;
  63.       (*r)->next->pred->next = (*r)->next;
  64.       (*r)->next->pred->pred = *r;
  65.       (*r)->next = (*r)->next->pred;
  66.       (*d)->beg = (*r)->next;
  67.    }
  68. }
  69.  
  70. int pop(dline **d, char *x)
  71. {
  72.    list **r = &(*d)->beg;
  73.    if (!isEmpty(*d))
  74.    {
  75.       *x = (*r)->next->el;
  76.       if ((*r)->next->next != (*r)->next)
  77.       {
  78.          list *d1, *d2;
  79.          d1 = (*r)->next->next;
  80.          d2 = (*r)->next->pred;
  81.          delete (*r)->next;
  82.          (*r)->next = d1;
  83.          (*r)->next->pred = d2;
  84.       }
  85.       else
  86.       {
  87.          delete (*r)->next;
  88.          (*r)->next = *r;
  89.          (*r)->pred = *r;
  90.       }
  91.       return 1;
  92.    }
  93.    else
  94.       return 0;
  95. }
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