Copy of pointer (stack)

Hello, the problem is that i need a copy of *s to save it, so then to cout elements 5 3 1 doing one more while cycle loop written below. Or tell me another way to cout these elements.
1
2
3
4
5
6
while (!s.isEmpty())              
	{
                s.pop();
		cout << s.topp() << endl;
		s.pop();
	}



input: input:
6 6
1 1
2 2
3 3
4 4
5 5
6 6

output: 6 4 2 needed output: 6 4 2 5 3 1


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
#include "stdafx.h"
#include <iostream>
using namespace std;

struct Stack
{
	int max_n;
	int *s;
	int s2;
	int top;

	Stack(int n) : max_n(n), s(new int[max_n]), top(0) {}


	bool isEmpty() const
	{
		return top == 0;
	}

	void push(int x)
	{
		s[top++] = x;
	}

	void pop()
	{
		s[--top];
	}

	int topp()
	{
		return s[top - 1];
	}

	void delStack()
	{
		delete[] s;
	}

};

void PushoPop(int n, Stack s);


int main()
{
	int n;
	cin >> n;
	Stack s(n);

	PushoPop(n, s);

	return 0;
}

void PushoPop(int n, Stack s)
{
	int number;
	for (int i = 0; i < n; i++)
	{
		cin >> number;
		s.push(number);
	}

	while (!s.isEmpty())              
	{
		cout << s.topp() << endl;
		s.pop();
                s.pop();
	}

	s.delStack();
}
Last edited on
So, your question is:

Given any input, you'd like to output every other value in the array?

For example, given the following input 6, 3, 9, 5, 7, 4, you'd like to output: 4, 5, 3, 7, 9, 6?

Or, given 2, 4, 1, 6, 8, 4, 6, 9, 0, 1, 3, 2, 4 you'd like to output:
4, 3, 0, 6, 8, 1, 2, 2, 1, 9, 4, 6, 4?
Last edited on
Yes, given any output, I'd like to output every other value in the array as you have written in both examples.
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
#include <sstream>
#include <string>

//....

void PushoPop(int n, Stack s)
{
        int number;
        for (int i = 0; i < n; i++)
        {
                cin >> number;
                s.push(number);
        }

        stringstream ss;
        while (!s.isEmpty())              
        {
                cout << s.topp() << endl;
                s.pop();
                ss << s.topp() << endl;
                s.pop();
        }

        s.delStack();
        cout << ss.str();
}
Last edited on
It's worth noting that your array still exists on the free-store even after you "pop" the values from the stack (but not after you call delete []). One solution could be to reset top to again point one-past the top of the stack and re-iterate through it -- this time printing the other set of values you need. A reset function would look something like:
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
#include <iostream>
using namespace std;

struct Stack
{
	int max_n;
	int *s;
	int s2;
	int top;

	Stack(int n) : max_n(n), s(new int[max_n]), top(0) {}


	bool isEmpty() const
	{
		return top <= 0;
	}

	void push(int x)
	{
		s[top++] = x;
	}

	int pop()
	{
		if(!isEmpty())
			return s[--top];
	}

	void delStack()
	{
		delete[] s;
	}

	void reset()
	{
		top = max_n;
	}

};

void PushoPop(int n, Stack s);


int main()
{
	int n;
	cin >> n;
	Stack s(n);

	PushoPop(n, s);

	return 0;
}

void PushoPop(int n, Stack s)
{
	int number;
	for (int i = 0; i < n; i++)
	{
		cin >> number;
		s.push(number);
	}

	while (!s.isEmpty())              
	{
		cout << s.pop();
		s.pop();
	}

	s.reset();

	while(!s.isEmpty())
	{
		s.pop();
		cout << s.pop();
	}

	s.delStack();
}



Additionally, in regards to your mention of copying *s, note that you passed Stack by value which creates a new Stack object, but now you have two Stack objects pointing to the same array in the free-store. If you'd like to copy the dynamically allocated memory on pass-by-value, you'd need to create Stack as a class and create something called a copy constructor that allocates new space on the free-store for array elements.

Note with this solution, I got rid of "topp" since it all can really be handled by "pop"
Last edited on
Thanks for solution :)
Topic archived. No new replies allowed.