Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define TYPE int
  4. #define STACKSIZE 1000
  5.  
  6.  
  7. typedef struct
  8. {
  9. int top;
  10. TYPE items[STACKSIZE];
  11. } Stack;
  12.  
  13.  
  14. void initialize(Stack *s)
  15. {
  16. s->top=0;
  17. }
  18.  
  19. int isfull (Stack *s)
  20. {
  21. return s->top>=STACKSIZE?1:0;
  22. }
  23.  
  24. int isempty(Stack *s)
  25. {
  26. return s->top==0?1:0;
  27. }
  28.  
  29. void push(Stack *s,TYPE value)
  30. {
  31. s->items[s->top++]=value;
  32. }
  33.  
  34. TYPE pop(Stack *s)
  35. {
  36. return s->items[--s->top];
  37. }
  38.  
  39. TYPE peek(Stack *s)
  40. {
  41. return s->items[s->top-1];
  42. }
  43.  
  44. int Stack_count(Stack *s)
  45. {
  46. Stack x;
  47. int countt=0;
  48. initialize(&x);
  49. TYPE temp;
  50. while(!isempty(s))
  51. {
  52.  
  53. temp=pop(s);
  54. push(&x,temp) ;
  55. countt++;
  56. }
  57. while(!isempty(&x))
  58. {
  59. push(s,pop(&x));
  60. }
  61.  
  62. return countt;
  63. }
  64.  
  65. Stack reverse_stack(Stack *s)
  66. {
  67. Stack rev;
  68. initialize(&rev);
  69. while(!isempty(s))
  70. {
  71. push(&rev,pop(s));
  72.  
  73. }
  74. return rev;
  75. }
  76.  
  77. void evenoddsort(Stack *s)
  78. {
  79. Stack odd,even;
  80. initialize(&odd);
  81. initialize(&even);
  82. TYPE x;
  83.  
  84. while(!isempty(s))
  85. {
  86. x=pop(s);
  87. if(x%2) push(&even,x);
  88. else push (&odd,x);
  89.  
  90. }
  91.  
  92. while(!isempty(&odd))
  93. {
  94. push(s,pop(&odd));
  95. }
  96.  
  97. while(!isempty(&even))
  98. {
  99. push(s,pop(&even));
  100. }
  101.  
  102. }
  103.  
  104.  
  105.  
  106.  
  107. Stack stack_sort(Stack *s)
  108. {
  109. Stack x;
  110. Stack y;
  111. initialize(&x);
  112. initialize(&y);
  113. TYPE temp,xvalue;
  114.  
  115.  
  116. while(!isempty(s))
  117. {
  118. TYPE svalue =pop(s);
  119.  
  120. if(isempty(&x)) //init,
  121. {
  122. push(&x,svalue);
  123. continue; //push it and check next item in stack
  124. }
  125.  
  126. xvalue=peek(&x);
  127.  
  128. while (svalue>xvalue && !isempty(&x))
  129. {
  130. pop(&x);
  131. push(&y,xvalue);
  132. xvalue=peek(&x);
  133.  
  134. }
  135. push(&x,svalue);
  136. while(!isempty(&y))
  137. {
  138. push(&x,pop(&y));
  139. }
  140.  
  141. }
  142. return x;
  143. }
  144.  
  145.  
  146.  
  147. Stack stack_sort_insert(Stack *s,TYPE value)
  148. {
  149. push(s,value);
  150. return stack_sort(s);
  151. }
  152.  
  153.  
  154. Stack two_ordered(Stack *s, Stack *s2)
  155. {
  156. while(!isempty(s))
  157. {
  158. push(s2,pop(s));
  159. }
  160. return stack_sort(s2);
  161.  
  162.  
  163.  
  164. }
  165.  
  166.  
  167.  
  168. void stack_delete(Stack *s,int k)
  169. {
  170. int i;
  171. Stack x; initialize(&x);
  172.  
  173. for(i=1;i<=k-1;i++)
  174. {
  175.  
  176. push(&x,pop(s));
  177.  
  178. }
  179. pop(s);
  180.  
  181. while(!isempty(&x))
  182. {
  183.  
  184. push(s,pop(&x));
  185. }
  186.  
  187.  
  188. }
  189.  
  190. int check_sum(Stack s)//check sum of first half equals sum of secound half
  191. {
  192. Stack x; initialize(&x);
  193.  
  194.  
  195.  
  196. int n=Stack_count(&s)/2;
  197. int i;
  198. int sum1=0;
  199. int sum2=0;
  200. for (i=0;i<=n;i++)
  201. {
  202. sum1+=pop(&s);
  203. }
  204.  
  205. while(!isempty(&s))
  206. {
  207. sum2+=pop(&s);
  208. }
  209.  
  210. return sum1==sum2?1:0;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement