fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define SIZEMAX 10
  5.  
  6. typedef struct node
  7. {
  8. int data;
  9. struct node* next;
  10. }node;
  11.  
  12. typedef struct stack
  13. {
  14. node *head;
  15. int stksize;
  16. }stack;
  17.  
  18. void initialize(stack *stk)
  19. {
  20. stk->head=NULL;
  21. stk->stksize=0;
  22. }
  23.  
  24. void push(stack *stk,int x)
  25. {
  26. if (stk->stksize==SIZEMAX)
  27. {
  28. printf("stack full");
  29. return;
  30. }
  31.  
  32. node *temp=(node*)malloc(sizeof(node));
  33. temp->data=x;
  34. temp->next=stk->head;
  35. stk->head=temp;
  36. stk->stksize++;
  37. }
  38.  
  39. void print(stack *stk)
  40. {
  41. node *temp=stk->head;
  42. while(temp!=NULL)
  43. {
  44. printf("|%d|\n",temp->data);
  45. temp=temp->next;
  46. }
  47. }
  48.  
  49. void pop(stack *stk)
  50. {
  51. if (stk->stksize==0)
  52. {
  53. printf("nothing to pop");
  54. return;
  55. }
  56.  
  57. node *temp=stk->head->next;
  58. free(stk->head);
  59. stk->head = temp;
  60. stk->stksize--;
  61. }
  62.  
  63. void partition(stack *stk)
  64. {
  65. stack negative,positive;
  66.  
  67. initialize(&negative);
  68. initialize(&positive);
  69. while (stk->stksize!=0)
  70. {
  71. if (stk->head->data<0)
  72. {
  73. push(&negative,stk->head->data);
  74. pop(stk);
  75. }
  76. if (stk->head->data>=0)
  77. {
  78. push(&positive,stk->head->data);
  79. pop(stk);
  80. }
  81. }
  82.  
  83. printf("\n");
  84. print(&negative);
  85.  
  86. printf("\n");
  87. print(&positive);
  88. }
  89.  
  90. int main()
  91. {
  92. int i,x;
  93. stack mystk;
  94. initialize(&mystk);
  95.  
  96. for(i=0;i<5;i++)
  97. {
  98. scanf("%d",&x);
  99. push(&mystk,x);
  100. }
  101.  
  102. print(&mystk);
  103.  
  104. partition(&mystk);
  105. printf("\n");
  106. print(&mystk);
  107.  
  108. return(0);
  109. }
Success #stdin #stdout 0s 2428KB
stdin
1
-2
3
-4
-1
2
-3
4
stdout
|-1|
|-4|
|3|
|-2|
|1|

|-2|
|-4|
|-1|

|1|
|3|