Implementing methods to move elements from one stack to another

Hi. I have 3 stacks (bucket1, bucket2, barrel). I want to implement 3 methods that do the following:

fill() - fills bucket1 or bucket2 with elements taken from barrel;

empty() - transfers all elements from bucket1 or bucket2 into barrel;

transfer()- transfers elements from bucket1 to bucket2 and vice-versa until one of them is either full or empty;

Barrel starts full with elements at first, then you take elements from barrel using bucket1/2.fill() and so on until you reach your goal. My goal here is to measure 4 liters. I already have the algorithm for that. I just need to know how to implement these 3 methods for my Stack class. PLEASE NOTE I am not using <stack>, but my own template class called Stack with various methods. Here it is below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
template<typename T, int N> 
class Stack {
    private:
	T stackArray[N]; 
	int topLevel; 
    public:
        void push(T x);
        int isEmpty();
        T pop();      
	  T peek();
	  Stack();
	 ~Stack();
};

template<typename T, int N>
void Stack<T, N>::push(T x) {
	if (topLevel >= N - 1) 
	 {
	cout<<"The stack is full!\n";
                	return;
        }
	stackArray[++topLevel] = x;
 }

template<typename T, int N>
int Stack<T, N>::isEmpty() {
	return (topLevel < 0); 
}

template<typename T, int N>
T Stack<T, N>::pop() {
	if (isEmpty()) {
	        cout<<"The stack is empty! \n";
               	T x;
                return x;
            }					
        return stackArray[topLevel--];
}

template<typename T, int N>
T Stack<T, N>::peek() {
	       if (isEmpty()) {
		cout<<"The stack is empty! \n";
	        T x;
	        return x;
        	}	
          return stackArray[topLevel]; 
}
		
template<typename T, int N>
Stack<T, N>::Stack() { // constructor
	        topLevel = -1; 
}

template<typename T, int N>
Stack<T, N>::~Stack() { // destructor
}
Last edited on
Topic archived. No new replies allowed.