Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. /// purva grupa
  2. #include<iostream>
  3. #include<cstring>
  4. using namespace std;
  5. struct Element
  6. {
  7. int x;
  8. Element* next;
  9. };
  10. class Stack
  11. {
  12. Element* start;
  13. public:
  14. Stack()
  15. {
  16. start=NULL;
  17. }
  18. int top()
  19. {
  20. return (start->x);
  21. }
  22. void push(int a)
  23. {
  24. Element* k=start;
  25. start= new Element;
  26. start->x=a;
  27. start->next=k;
  28. }
  29. int pop()
  30. {
  31. int a=start->x;
  32. Element*p=start;
  33. start=p->next;
  34. delete[]p;
  35. return a;
  36. }
  37. bool isempty()
  38. {
  39. return start==NULL;
  40. }
  41. void print()
  42. {
  43. while(!isempty())
  44. {
  45. cout<<pop()<<" ";
  46. }
  47. }
  48. Stack operator=(Stack a)
  49. {
  50. Stack temp=Stack();
  51. Stack c=Stack();
  52. while(!a.isempty())
  53. {
  54. temp.push(a.pop());
  55. }
  56. while(!temp.isempty())
  57. {
  58. c.push(temp.top());
  59. c.push(temp.top());
  60. temp.pop();
  61. }
  62. ///return *this;
  63. return c;
  64. }
  65. };
  66. Stack Merge(Stack a, Stack b)
  67. {
  68. Stack temp=Stack();
  69. while(!a.isempty())
  70. {
  71. temp.push(a.pop());
  72. }
  73. while(!b.isempty())
  74. {
  75. temp.push(b.pop());
  76. }
  77. return temp;
  78. }
  79. int minStack(Stack a)
  80. {
  81. int flag=0;
  82. Stack tempstack=Stack();
  83. while(!a.isempty())
  84. {
  85. flag++;
  86. tempstack.push(a.pop());
  87. }
  88. int temp[flag];
  89. int j=0;
  90. while(!tempstack.isempty())
  91. {
  92. temp[j]=tempstack.pop();
  93. j++;
  94. }
  95. int minn=temp[0];
  96. for(int i=0; i<flag; i++)
  97. {
  98. if(minn>temp[i]) minn=temp[i];
  99. }
  100. return minn;
  101. }
  102. int main()
  103. {
  104. Stack a=Stack();
  105. a.push(1);
  106. a.push(2);
  107. a.push(3);
  108. a.push(4);
  109. Stack b=Stack();
  110. b.push(5);
  111. b.push(6);
  112. b.push(7);
  113. b.push(8);
  114. Merge(a,b).print();
  115. Stack c=Stack();
  116. c.push(4);
  117. c.push(7);
  118. c.push(9);
  119. c.push(15);
  120. cout<<endl<<"min = "<<minStack(c);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement