Stack.h and Stack.cpp

Hi i am really struggling with this assignment and am not really sure how to begin. so we have to create a file Stack.cpp that will contain all the methods declared in Stack.h.

Here is stack.h
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
#include    <iostream>
#include    <iomanip>
using namespace std;


class Stack
{
public:
	Stack(int);           // constructor
	Stack(const Stack &); // copy constructor
	~Stack();              // destructor

	void push(int);       // push an int into a Stack
	int  pop();            // pop an int from a Stack

	bool empty() const;    // is the Stack empty?
	bool full() const;     // is the Stack full?

	int capacity() const;  // capacity of the stack
	int size() const;      // current size of the stack

	friend ostream &operator <<(ostream &, const Stack &);

private:
	int *stack;             // pointer to local stack of ints

	int top;                // top of stack (next avail. location)
	int maxsize;            // max size of the stack
};


Here is the main.cpp
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
#include    "Stack.h"

int main()
{
	Stack s(2);           // create stack with space for 3 ints
	s.push(10);
	s.push(20);
	s.push(30);           // pushing one int too many

	cout << "stack s size     = " << s.size() << '\n';
	cout << "stack s capacity = " << s.capacity() << '\n';
	cout << "stack s:\n" << s << '\n';

	{                       // start a new block
		Stack t(s);       // t contains a copy of s
		cout << "\nstack t:\n" << t << '\n';
		cout << "pop one element: " << t.pop() << '\n';

		cout << "stack t size     = " << t.size() << '\n';
		cout << "stack t capacity = " << t.capacity() << '\n';
		cout << "\nstack t:\n" << t << '\n';
	}                       // end the new block

	cout << "\nstack s:\n" << s << '\n';
	cout << "pop one element: " << s.pop() << '\n';
	cout << "pop one element: " << s.pop() << '\n';
	// poping one element too many
	cout << "pop one element too many: " << s.pop() << '\n';
	cout << "stack s size     = " << s.size() << '\n';
	cout << "stack s capacity = " << s.capacity() << '\n';
	// print an empty stack s
	cout << "\nstack s:\n" << s << '\n';

	return 0;
}


Please help me understand how to do this (please dont just give me an answer, explain it too if possible)

Thank you for your time
Do you know what a stack is, or do you need an explanation for that too?

Just trying to work out where to start.
yeah i believe i have decent grasp on what a stack is
... and what is it?

If you didn't have to conform to that class, could you code one?
it is a type of container adaptor in which the elements are put in and taken from the same end.

Not sure. Probably a really simple one like the examples in class. Our teacher normally provides the classes so we havent coded one yet
So you can code it now.

The member int *stack is used to store your data.
When push is called, you check if your stack is big enough to hold the data and then append it.
When pop is called, you remove it from the data and return it.
One implementation is to use an array. Let's say we want to make a stack of ints. We need an array and a seperate variable which marks the top of the stack.
1
2
3
const int stacksize = 4;
int array[stacksize];
int top = 0;

So we start of with the stack being empty. How do we know? Because top == 0.

Let's push 3 numbers, 8, 3, 5.

Push 8.
1
2
array = [8] [?] [?] [?]
top = 1
8 is placed in array[top], then top is incremented.

When we push 3, we get:
1
2
array = [8] [3] [?] [?]
top = 2


And 5:
1
2
array = [8] [3] [5] [?]
top = 3


Is the stack full? Well, no because top < stacksize.

Let's peek at the top element. Remember, top points to one past the end, so the top element is array[top - 1], which is 5.

Let's pop the stack, that just moves the top of the stack:
1
2
array = [8] [3] [5] [?]
top = 2
Even though 5 is still there, we ignore it, the top element now holds 3.

I think that covers it. Did you follow that?
i get what you are saying, i dont see how that correlates to my problem though
here ill show you a guess that i have based on what we had discussed in class. Here is what i have for stack.cpp right now

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
#include    <iostream>
#include    <iomanip>
using namespace std;

#include    "Stack.h"

Stack::Stack(const Stack &s)
{
	maxsize = s.maxsize;
	// allocate stack for left side object
	stack = new int[maxsize];
	// now copy right side object to left side object
	for (top = 0; top < maxsize; ++top)
	{
		stack[top] = s.stack[top];
	}
}


void Stack::push(int i)   // push an int into a Stack
{
	if (!full())
	{
		cout << "push( " << i << " )\t at location "
			<< top << '\n';
		stack[top] = i;
		++top;              // advance to the next empty location
	}
}

int Stack::pop()           // pop an int from a Stack
{
	if (empty())
	{
		return -1;          // stack is empty; return -1
	}
	else
	{
		--top;
		cout << "pop( ) " << stack[top]
			<< " at location " << top << '\n';
		// return item at top of the stack
		return stack[top];
	}
}

ostream &operator <<(ostream &out, const Stack &s)
{
	for (int i = s.size() - 1; i >= 0; --i)
	{
		out << setw(3) << i << setw(5) << s.stack[i] << '\n';
	}

	return out;
}
I can't see Stack::Stack(int);


Stack::Stack(const Stack &s) isn't intialising top correctly.

I can't work out whether the push/pop oprations are correct because I can't see what you're doing with top.

The stream thing looks good.
how would i initialize top correctly

im also unable to figure out stack::stack(int);
Last edited on
In Stack::Stack(const Stack &s), you initialise top by copying s.top.

Stack::Stack(int) must set maxsize (from the value passed in as the parameter), stack (an array of size maxsize), and top (initially zero).

How are you getting on with full(), empty(), size(), and capacity()?
not sure. its just whats in the notes
would this be a good way to do stack::stack(int)

1
2
3
4
5
6
7
inline Stack::Stack( int max )     
{
    cout << "in Stack::Stack( " << max << " )\n";
    maxsize = max;
    stack   = new int[ maxsize ];
    top     = 0;           
}                           
Nevermind just realize i cant use inline in cpp. so still not sure what to do for stack::stack (int)
That code is fine. Just remove the inline keyword and move the code from the header file to the source file.

So now it's clear that maxsize is the maximum size of the stack (it's capacity), top is the number of elements in the stack (it's size) and stack is the array containing the data.

So, to complete the excersize, you need to implement full(), empty(), size() and capacity(), and set top in the copy constructor and you're done.
Topic archived. No new replies allowed.