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