Custom Stack and Friendship problems

I'm working on an assignment and would therefore prefer hints instead of full solutions.

First, here's the code:
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
#include <iostream>
#include <stdlib.h>
using namespace std;

class IntStack{
    static const int INITIAL_SIZE = 100;
    int i;
    int st_arr[INITIAL_SIZE];
public:
    void reset();
    void push(int n);
    void push(int a[], size_t array_size);
    int pop();
    void pop(int a[], size_t n);
    int top();
    int size();
    bool is_full();
    bool is_empty();
    friend IntStack print_stack(IntStack* pstack);
};

IntStack print_stack(IntStack* pstack){
    int stop = 0;
    for (int idx = 100; idx > 0; idx--){
        if (pstack->st_arr[idx] != 0){
            stop = idx;
            idx = 0;
        }
    }
    //cout << "STOP POINT IS STACK ELEMENT " << stop << "\n";
    for (int i = 0; i <= stop; i++){
        cout << pstack->st_arr[i] << endl;
        //cout << "i: " << i << endl;
    }
}
int main()
{
    /*int arr[5] = {1,2,3,4,5};
    IntStack stack;
    stack.reset();
    stack.push(arr, 5);
    print_stack(&stack);
    //cout << "\n";
    //stack.pop(arr, 5);
    //print_stack(&stack);
    return 0;*/
    IntStack stack;
    stack.reset();

    std::cout << "pushing 11\n";
    stack.push(11);
    std::cout << "is_empty: " << stack.is_empty() << "\n";
    std::cout << "top: " << stack.top() << "\n";
    std::cout << "pop: " << stack.pop() << "\n";
    std::cout << "is_empty: " << stack.is_empty() << "\n";

    std::cout << "pop: " << stack.pop() << "\n"; // this should result in "STACK EMPTY" output, or similar message
    std::cout << "pop: " << stack.pop() << "\n"; // this should result in "STACK EMPTY" output, or similar message
    std::cout << "is_empty: " << stack.is_empty() << "\n";

    std::cout << "pushing 1\n";
    stack.push(1);
    std::cout << "pushing 2\n";
    stack.push(2);
    std::cout << "pushing 3\n";
    stack.push(3);
    std::cout << "pushing 4\n";
    stack.push(4);

    std::cout << "pop: " << stack.pop() << "\n";
    std::cout << "pop: " << stack.pop() << "\n";
    std::cout << "top: " << stack.top() << "\n";
    std::cout << "pop: " << stack.pop() << "\n";

    std::cout << "stack size: " << stack.size() << "\n";

    stack.reset();

    int arr[] = {0, 1, 2, 3, 4};

    stack.push(arr, 5);
    print_stack(&stack);
    stack.pop(arr, 5);

    std::cout << "stack size: " << stack.size() << "\n";

    for(int i = 0; i < 5; ++i)
    {
        std::cout << arr[i] << "\n";
    }

    return 0;


}
int IntStack::top(){
    i--;
    int temp = i;
    i++;
    return st_arr[temp];
}

int IntStack::size(){
    return i;
}

void IntStack::reset(){
    //loop through the stack and set each element to NULL
    for (i = 0; i < INITIAL_SIZE; i++){
        st_arr[i] = NULL;
    }
    i = 0;
}

void IntStack::push(int n){
    //if stack is full, int cannot be pushed, signal error
    if (i == 100){
        cerr << "STACK EMPTY";
    }
    //set current element of stack to pushed int then increment index
    st_arr[i] = n;
    i++;
}

void IntStack::push( int arr[], size_t array_size ){
    for (int idx = 0; idx < array_size; idx++){
        push(arr[idx]);
    }
}

int IntStack::pop(){
    //if stack is empty, int cannot be set to null, signal error
    if (i == 0){
        cerr << "STACK EMPTY\n";
    }
    else
    //decrement to previous stack index then set it to NULL
    --i;
    int temp = st_arr[i];
    st_arr[i] = NULL;
    return temp;
}

void IntStack::pop( int a[], size_t n ){
    i--;
    for (int idx = 0; idx < n; idx++){
            st_arr[i] = a[idx];
            i--;
    }
    for (i = 0; st_arr[i] != NULL; i++);
}

bool IntStack::is_full(){
    if(i == 100)
        return true;
    else
        return false;
}

bool IntStack::is_empty(){
    if(i != 0)
        return false;
    else
        return true;
}


Here is the output I'm getting:
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
pushing 11
is_empty: 0
top: 11
pop: 11
is_empty: 1
STACK EMPTY
pop: 0
STACK EMPTY
pop: 0
is_empty: 1
pushing 1
pushing 2
pushing 3
pushing 4
pop: 4
pop: 3
top: 2
pop: 2
stack size: 1
0 // first element
1
2
3
4
stack size: 4
0
1
2
3
4


And here's the output I want:
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
pushing 11
is_empty: 0
top: 11
pop: 11
is_empty: 1
STACK EMPTY
pop: 0
STACK EMPTY
pop: 0
is_empty: 1
pushing 1
pushing 2
pushing 3
pushing 4
pop: 4
pop: 3
top: 2
pop: 2
stack size: 1
0
1
2
3
4
stack size: 0
4
3
2
1
0


I know I have to change my print function. It has to stay a friend as per assignment rules. Or can I change the reset function?

I'm not sure where to begin. Is there a way to access member variables through the print function even though friendship is a one-way street?

And as for the order of the array at the end of the output... I have no idea.
The following code does reverse the integers with my current overloaded definitions of push and pop as they are:
1
2
3
4
5
6
7
8
int arr[5] = {1,2,3,4,5};
    IntStack stack;
    stack.reset();
    stack.push(arr, 5);
    print_stack(&stack);
    stack.pop(arr, 5);
    print_stack(&stack);
    return 0;


Any help would be greatly appreciated.
Last edited on
You are getting 100 zeros because you are creating the array size to be 100 and the for statement detects this naturally. You only asked for a hint so I will suggest investing time into using deque or vectors for this since if you figure out how to use these and they are pretty simple to learn and use, they only expand to the size you need as you use them and their output is the desired effect you are looking for in that aspect.

[EDIT]

Nevermind, you are trying to create a custom stack, so you are trying to re-invent the wheel here probably to learn. Well, maybe instead of using vector and deque, you could study them and see how they work in detail to better understand how to right a stack class yourself.
Last edited on
Alright, I've changed my print function so it doesn't print out all of the zeroes.

I've spent the past 3 hours working on this, and I'm still completely lost. I'll take more than just hints now. This assignment is a month late already...

Even if at the very least someone could tell me what line/s the problem/s is/are on that would be helpful.
Last edited on
Hint: Your void IntStack::pop( int a[], size_t n ) implementation seems to be performing a push.

Jim
If it's not pushing integers, how does it reverse them?

From the assignment:
The effect of the following code should be to reverse the integers in arr:


int arr[ 5 ] = { 1, 2, 3, 4, 5};
IntStack st;
st.push( arr, 5 );
st.pop( arr, 5 );


also, why does this revers the integers in arr and not in st? I may have screwed up my entire program. If you look at the last loop in main, in my professor's output it shows the integers in that arr array have been reversed, but in my code, they weren't.
Last edited on
The effect of the following code should be to reverse the integers in arr

Reverse the integers in arr, not the stack.

What's supposed to happen is that you push arr onto the stack, and then pop the stack back into arr, which will effectively reverse what's in arr.
Got it with this:

1
2
3
4
5
6
7
8
void IntStack::pop( int a[], size_t n ){
    i--;
    for (int idx = 0; idx < n; idx++){
            a[idx] = st_arr[i];
            st_arr[i] = NULL;
        if(i>0)i--;
    }
}


Thank you very much.
Last edited on
Well, the issue here is that you have a predefined size which in this case makes it so that it isn't really a stack like a vector or a deque would be. If you look at the stacking options and how stacks are set up, they can help you create a stack so that the size isn't specific, you only use what you need, nothing more and nothing less.

A string is also set up that way. A great way would be to look into the vector and deque headers and see how they are coded. This is how I would start into creating my own stack. Once you have an understanding of how they are created, you can create your own with your own functions.

Deques are like vectors, only they allow things to be added and taken out at the beginning and end of a stack. Both allow you access to the middle so you can see and alter what is there.
Topic archived. No new replies allowed.