STL Stacks

This is an homework assignment, but I've spent about 7 hours on it with no solution. I'm only allowed to use 2 stacks and 2 variables, one to hold the length of the stack and the other to hold an element of the stack.
I need to make stack2 equivalent to stack1, but all different methods I tried they all loop back to what I started with, because the elements are readded back in the same order. Any ideas?

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
#include <iostream>
#include <stack>

using namespace std;

int main()
{
	stack<int> s1;
	stack<int> s2;
	int input;
	int size = 0;

	cout << "Enter a whole numbers or enter 0 to quit: ";
	cin >> input;
	while (input != 0)
	{
		s1.push(input);
		cin >> input;
		++size;
	}

	cout << "Size is: " << size << endl;

	while (size != 0)
	{
		while (!s1.empty())
		{
			cout << "Inputting: " << s1.top() << " into second stack.\n" << endl;
			s2.push(s1.top());
			input = s1.top();
			s1.pop();
		}
		s2.pop();

		while (!s2.empty())
		{
			cout << "Inputting: " << s2.top() << " into first stack.\n" << endl;
			s1.push(s2.top());
			s2.pop();
		}
		s2.push(input);

		--size;
	}

	while (!s1.empty())
	{
		s2.push(s1.top());
		s1.pop();
	}

	while (!s2.empty())
	{
		cout << "Pop item from S2: " << s2.top() << endl;
		s2.pop();
	}

	cout << endl;
	system("PAUSE");
	return 0;
}
Do both stacks need to be equal in the end, or are you simply required to make s2 equal to what s1 was at the start of the operation?

-Albatross
I only need to output s2, but s2 should be what s1 started as. Thanks!
Okay, that makes the problem infinitely easier.

Think carefully. Stacks are a last-in-first-out structure, meaning that the last element that's pushed in is the first to get popped. You have a buffer that allows you to take one element out of the stack and store it indefinitely until you'd like to reinsert it. You also have a counter that could potentially tell you when your element needs to be reinserted. This counter might decrease by one with every "cycle".

Does this help at all?

-Albatross
Last edited on
Want to watch your teacher lose it?: http://www.cplusplus.com/reference/algorithm/copy/
>:D This is a joke by the way... mostly. I also wanted you to know that programming in C\C++ is not NEARLY as tedious as school makes it seem.
Am I on the right track? This isn't working though...
@Computergeek01, if this wasn't an assignment I could simply do s2 = s1, it works perfectly fine haha.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
while (size != 0)
	{
		while(s2.size() < size)
		{
			cout << "Inputting: " << s1.top() << " into second stack.\n" << endl;
			s2.push(s1.top());
			input = s1.top();
			s1.pop();
		}
		s2.pop();
		while(s1.size() < size-1)
		{
			cout << "Inputting: " << s2.top() << " into first stack.\n" << endl;
			s1.push(s2.top());
			s2.pop();
		}
		s2.push(input);
		size--;
	}
Last edited on
@Computergeek:
Ha ha. When was the last time you saw a stack expose iterators?

@Netto:
You should be using input = s1.top() only once per cycle. Other than that, yes, I think you are on the right track!

-Albatross
I figured it out haha, thanks for the idea Albatross :]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
while (size != 0)
	{
		while(!s1.empty())
		{
			cout << "Inputting: " << s1.top() << " into second stack.\n" << endl;
			s2.push(s1.top());
			input = s1.top();
			s1.pop();
		}
		s2.pop();
		while(size-1 > s1.size())
		{
			cout << "Inputting: " << s2.top() << " into first stack.\n" << endl;
			s1.push(s2.top());
			s2.pop();
		}
		s2.push(input);
		size--;
	}
Last edited on
Topic archived. No new replies allowed.