fork download
  1. #include <stdio.h>
  2. #include <malloc.h>
  3.  
  4. struct node {
  5. int val;
  6. struct node* next;
  7. };
  8.  
  9. int stack_push(struct node** st, int val);
  10. void stack_pop(struct node** st);
  11.  
  12. //разделение стека на два стека
  13. void stack_split(struct node** s1,
  14. struct node** s2,
  15. struct node** st){
  16. struct node* p = *st;
  17.  
  18. for(*s1 = *s2 = NULL; p != NULL; ){
  19. if(p->val % 2){
  20. *s1 = p;
  21. p = p->next;
  22. s1 = &(*s1)->next;
  23. } else {
  24. *s2 = p;
  25. p = p->next;
  26. s2 = &(*s2)->next;
  27. }
  28. }
  29. *s1 = *s2 = *st = NULL;
  30. }
  31.  
  32.  
  33. int main(void){
  34. int i;
  35. struct node* s1, *s2, *st = NULL;
  36.  
  37. for(i = 0; i < 30; ++i)
  38. stack_push(&st, i);
  39.  
  40. stack_split(&s1, &s2, &st);
  41.  
  42. //адреса
  43. printf("addr ptr - s1: 0x%08X\n", s1);
  44. printf("addr ptr - s2: 0x%08X\n\n", s2);
  45.  
  46. //вывести не чётные числа
  47. while(s1 != NULL){
  48. printf("%d ", s1->val);
  49. s1 = s1->next;
  50. }
  51. putchar('\n');
  52.  
  53. //вывести чётные числа
  54. while(s2 != NULL){
  55. printf("%d ", s2->val);
  56. s2 = s2->next;
  57. }
  58. putchar('\n');
  59. return 0;
  60. }
  61.  
  62. //втолкнуть
  63. int stack_push(struct node** st, int val){
  64. struct node* p = (struct node*)malloc(sizeof(struct node));
  65. if(p != NULL){
  66. p->val = val;
  67. p->next = *st;
  68. *st = p;
  69. }
  70. return (p != NULL);
  71. }
  72.  
  73. //вытолкнуть
  74. void stack_pop(struct node** st){
  75. struct node* t = *st;
  76. if(t != NULL){
  77. *st = (*st)->next;
  78. free(t);
  79. }
  80. }
Success #stdin #stdout 0s 2184KB
stdin
Standard input is empty
stdout
addr ptr - s1: 0x095F41D8
addr ptr - s2: 0x095F41C8

29 27 25 23 21 19 17 15 13 11 9 7 5 3 1 
28 26 24 22 20 18 16 14 12 10 8 6 4 2 0