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
}
You'd just have another class to manage Stack(s). Can design it in different ways; hold them all, hold the main one (barrel), etc. Not sure if your second Stack param, N, is generic enough to apply to all methods of the class.

Example:
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <iostream>
#include <vector>

using namespace std;

template<typename T> 
class Stack 
{
public:
    Stack(int n) : capacity_(n)
    {
        stack_array_.reserve(n);
    }
    
    void Push(T x)
    {
        if (stack_array_.size() >= capacity_) 
        {
            cout << "The stack is full!\n";
            return;
        }
      stack_array_.push_back(x);
    }
    bool IsEmpty()
    {
        return stack_array_.empty();
    }
    T Pop()
    {
        if (IsEmpty()) 
        {
            cout << "The stack is empty!" << endl;
            return T();
        }
        T ele = stack_array_[stack_array_.size()-1];
        stack_array_.pop_back();
        return ele;
    }
    
    const T& Peek()
    {
        if (IsEmpty()) 
        {
            cout << "The stack is empty!" << endl;
            return T();
        }
        return stack_array_[stack_array_.size()-1];
    }
    const vector<T>& StackArray() const { return stack_array_; }
    int Size() const { return stack_array_.size(); }
    int Capacity() const { return capacity_; }
    
private:
    vector<T> stack_array_; 
    int capacity_;
};


template<typename T>
class BucketManager
{
public:
    BucketManager(Stack<T>& barrel) :
        barrel_(barrel)
    {
    }
    
    // Fills a from barrel
    void Fill(Stack<T>& a)
    {
        if (a.Size() < a.Capacity())
        {
            int diff = a.Capacity() - a.Size();
            for (int x=1; x<=diff; ++x)
            {
                a.Push(barrel_.Pop());
            }
        }
    }
    
    // Empties a to barrel
    void Empty(Stack<T>& a)
    {
        int size = a.Size();
        if (size > 0)
        {
            for (int x=1; x<=size; ++x)
            {
                auto ele = a.Pop();
                barrel_.Push(ele);
            }
        }
    }
    
    // Transfers from a to b
    void Transfer(Stack<T>& a, Stack<T>& b)
    {
        int diff = b.Capacity() - b.Size();
        if (diff >= a.Size())
        {
            int size = a.Size();
            for (int x=1; x<=size; ++x)
            {
                b.Push(a.Pop());
            }
        }
        else
        {
            for (int x=1; x<=diff; ++x)
            {
                b.Push(a.Pop());
            }
        }
        
    }
    
    // Shows status of barrel and the two stacks
    void Status(const Stack<T>& a, const Stack<T>& b) const
    {
        cout << "Barrel size: " << barrel_.Size() << endl;
        cout << "A (" << a.Size() << " of " << a.Capacity() << ") : ";
        for (const auto& ele : a.StackArray())
            cout << ele << " ";
        cout << endl;
        cout << "B (" << b.Size() << " of " << b.Capacity() << ") : ";
        for (auto const& ele : b.StackArray())
            cout << ele << " ";
        cout << endl << endl;
    }

private:
    Stack<T> barrel_;
};

int main() 
{
    Stack<char> a(5);
    Stack<char> b(8);
    Stack<char> barrel(26);
    for (int x=0; x<26; ++x)
        barrel.Push((char)x+97);
    
    BucketManager<char> bm(barrel);
    bm.Status(a, b);
    
    cout << "Fill B" << endl;
    bm.Fill(b);
    bm.Status(a, b);
    
    cout << "Transfer B->A" << endl;
    bm.Transfer(b, a);
    bm.Status(a, b);
    
    cout << "Empty B" << endl;
    bm.Empty(b);
    bm.Status(a, b);
    
    cout << "Transfer A->B" << endl;
    bm.Transfer(a, b);
    bm.Status(a, b);
    
    cout << "Fill A" << endl;
    bm.Fill(a);
    bm.Status(a, b);
    
    cout << "Transfer A->B" << endl;
    bm.Transfer(a, b);
    bm.Status(a, b);
    
    cout << "Empty B" << endl;
    bm.Empty(b);
    bm.Status(a, b);
    
    cout << "Transfer A->B" << endl;
    bm.Transfer(a, b);
    bm.Status(a, b);
    
    cout << "Fill A" << endl;
    bm.Fill(a);
    bm.Status(a, b);
    
    cout << "Transfer A->B" << endl;
    bm.Transfer(a, b);
    bm.Status(a, b);
    
    cout << "Fill A" << endl;
    bm.Fill(a);
    bm.Status(a, b);
    
    cout << "Transfer A->B" << endl;
    bm.Transfer(a, b);
    bm.Status(a, b);
    
    return 0;
}


Can run it at https://repl.it/repls/WordyAcidicSnake

Barrel size: 26
A (0 of 5) : 
B (0 of 8) : 

Fill B
Barrel size: 18
A (0 of 5) : 
B (8 of 8) : z y x w v u t s 

Transfer B->A
Barrel size: 18
A (5 of 5) : s t u v w 
B (3 of 8) : z y x 

Empty B
Barrel size: 21
A (5 of 5) : s t u v w 
B (0 of 8) : 

Transfer A->B
Barrel size: 21
A (0 of 5) : 
B (5 of 8) : w v u t s 

Fill A
Barrel size: 16
A (5 of 5) : z y x r q 
B (5 of 8) : w v u t s 

Transfer A->B
Barrel size: 16
A (2 of 5) : z y 
B (8 of 8) : w v u t s q r x 

Empty B
Barrel size: 24
A (2 of 5) : z y 
B (0 of 8) : 

Transfer A->B
Barrel size: 24
A (0 of 5) : 
B (2 of 8) : y z 

Fill A
Barrel size: 19
A (5 of 5) : w v u t s 
B (2 of 8) : y z 

Transfer A->B
Barrel size: 19
A (0 of 5) : 
B (7 of 8) : y z s t u v w 

Fill A
Barrel size: 14
A (5 of 5) : q r x p o 
B (7 of 8) : y z s t u v w 

Transfer A->B
Barrel size: 14
A (4 of 5) : q r x p 
B (8 of 8) : y z s t u v w o
Last edited on
Topic archived. No new replies allowed.