File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -58,6 +58,30 @@ class Stack{
5858 }
5959 return *this ;
6060 }
61+ /* *
62+ * example with simple study case
63+ * let a = 2,b = 3 your task swap to variable
64+ * simple method use temporary variabel
65+ * solution
66+ * temp = a
67+ * a = b
68+ * b = temp
69+ */
70+ void swap (Stack& others){
71+ // tukar capacity
72+ std::size_t tempCapa = capacity;
73+ capacity = others.size ;
74+ others.size = tempCapa;
75+ // tukar size
76+ int tempSize = size;
77+ size = others.size ;
78+ others.size = tempSize;
79+ // tukar array
80+ type* temp = new type[capacity];
81+ temp = arr;
82+ arr = others.arr ;
83+ others.arr = temp;
84+ }
6185 public: // abstraksi metod getter
6286 int get_size ()const {
6387 return this ->size ;
@@ -228,6 +252,14 @@ int main(){
228252 Stack<int >stack2 (100 );
229253 stack2 = stack1;
230254 stack2.print ();
231-
255+ Stack<int >stack5 (100 );
256+ stack5.push (10 );
257+ stack5.push (20 );
258+ stack5.push (30 );
259+ std::cout << " stack 5 sebelum swap" << std::endl;
260+ stack5.print ();
261+ std::cout << " sesusah swapp" << std::endl;
262+ stack5.swap (stack1);
263+ stack5.print ();
232264 return 0 ;
233265}
You can’t perform that action at this time.
0 commit comments