fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4.  
  5.  
  6. typedef struct _stack
  7. {
  8.  
  9. struct _stack* prev;
  10. struct _stack* next;
  11. int value;
  12.  
  13. }
  14. stack;
  15.  
  16. void push( int data );
  17. void pop(void);
  18. stack *temp = NULL;
  19. stack *first = NULL;
  20. stack *real_first = NULL;
  21.  
  22.  
  23. int main(void)
  24. {
  25.  
  26.  
  27. push(22);
  28. push(32);
  29. push(42);
  30. push(52);
  31. push(4);
  32. push(15);
  33. push(3453);
  34. pop();
  35. pop();
  36.  
  37. temp = real_first;
  38.  
  39. while (true)
  40. {
  41. if (temp == NULL)
  42. break;
  43.  
  44. printf("%i ", temp ->value);
  45. temp = temp->next;
  46.  
  47. }
  48.  
  49. temp = real_first;
  50.  
  51. while (temp != NULL)
  52. {
  53. real_first = real_first->next;
  54. free(temp);
  55. temp = real_first;
  56.  
  57. }
  58.  
  59.  
  60. printf("\n");
  61. return 0;
  62. }
  63.  
  64.  
  65.  
  66.  
  67.  
  68. void push( int data)
  69. {
  70. stack *newptr = malloc(sizeof(stack));
  71. if (newptr == NULL)
  72. {
  73. return;
  74. }
  75.  
  76. newptr->value = data;
  77.  
  78. newptr->prev = NULL;
  79.  
  80. newptr->next = NULL;
  81. if (first == NULL)
  82. {
  83. first = newptr;
  84.  
  85. real_first = first;
  86.  
  87. }
  88.  
  89. else
  90. {
  91.  
  92. newptr->prev = first;
  93.  
  94. first->next = newptr;
  95.  
  96. first = newptr;
  97.  
  98. }
  99.  
  100. return;
  101. }
  102.  
  103.  
  104. void pop(void)
  105. {
  106.  
  107.  
  108. if ( real_first == NULL || first == NULL)
  109. return;
  110.  
  111. else if (real_first->next == NULL )
  112. {
  113.  
  114.  
  115. free( real_first);
  116. real_first = NULL;
  117.  
  118. return;
  119. }
  120. else
  121. {
  122.  
  123.  
  124. real_first = real_first->next;
  125. free(real_first->prev);
  126. real_first->prev = NULL;
  127. }
  128.  
  129. }
Success #stdin #stdout 0s 2300KB
stdin
Standard input is empty
stdout
42 52 4 15 3453