Double Stack HW

I have a HW on the dynamic allocation topic that involves a double stack. I dont really understand the assignment so here is the Q's, the .h file, the .cpp file and i appreciate any leads I can get on this!! Thank you!

My Assignment:
A stack is a LIFO (last in, first out) data structure. Stacks are extremely useful in programming. A stack can be implemented using an array to hold values, and a counter to indicate where the "top" item on the stack is. See the first example. (You will need to download the .zip file and decompress it first.) This is a class declaration for a stack of doubles. Store your class declaration in a file called dblstk.h and implement the specified functions exactly as given. You may define whatever private members you think you need. You may also specify additional member functions if you find them useful.

Develop a test program in a file called Stack_driver.cpp for your stack implementation. This driver should define a stack and allow the user to specify, via a menu, the following operations. The user menu should exactly like the following:

Display stack 1
Place a double value onto the top of stack 1
Remove a value from the top of stack 1
Check the total capacity stack 1
Check current number of items on stack 1
Copy stack 1 to stack 2
Check to see if the two stacks are equal
Quit


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
  the .h file:
#include	<stdlib.h>
using namespace std;

class DoubleStack { 

	private:
	double	*data;		// Pointer to dynamic stack area.
	size_t	stack_size;	// Capacity of stack.
	size_t	tos;		// Top of stack. tos==0 ==> empty stack.

	public:
		// Constructor.
	DoubleStack( size_t	capacity );
	 
		// Copy Constructor.	
	DoubleStack( const DoubleStack& rhs );
	
		// Destructor.
	~DoubleStack(void);
	
		// Assignment operator=.		
	DoubleStack&	operator=( DoubleStack& rhs);

	size_t	getCapacity();
	
		// Add item to stack, making it the topmost item.
		// Return value 1=success/0=failure.
	int	push( double&	item );

		// Remove top item from stack and return it in item.
		// Return value 1=success/0=failure.
	int	pop( double&	item );

		// Determine if the stack is empty.
		// Return value 1=empty/0=non-empty.
	int	empty(void);
	
		// Inquire the capacity of the stack.
	size_t	capacity(void);
	
		// Inquire the number of items on the stack.	
	size_t	size(void);

		// Compare 2 stacks to see if the contain the same data.
	int	operator==( DoubleStack&	rhs );
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 my .cpp file:
#include "../include/dblstk.h"


DoubleStack::DoubleStack(size_t capacity):stack_size(capacity) {}

DoubleStack::DoubleStack(const DoubleStack& rhs):stack_size(this->getCapacity()){};

size_t	DoubleStack::getCapacity() {
	return stack_size;
}

DoubleStack::~DoubleStack(void){}

DoubleStack& DoubleStack::operator=( DoubleStack& rhs){
	
}
What about the assignment do you not understand? The stack data structure? The implementation of it using an array? Memory Management?

Here is a useful interactive demo of how a stack, implemented via an array works:
https://www.cs.usfca.edu/~galles/visualization/StackArray.html

You can play around with it to see what should be going on in your code, conceptually.

Wikipedia Link to Stacks:
https://en.wikipedia.org/wiki/Stack_(abstract_data_type)

The concept of a stack is captured in a simple statement, that is "first-in, last-out" or in other words "last-in, first-out." Essentially the implementation they want you to make is one with an array, so your going to need an index for the top of the stack, because that's where your going to be removing from. Whenever you add an item to your stack, you increment the top index, and when you remove an element you decrease the top index.

Remember though that for a stack you can only add onto the end/top of the stack.
Last edited on
Hi, okay, thanks. So i got what a stack should look like. I never knew that's how it operates, I thought it was only a place to store variables (like stack vs. heap...).

So now that I understand what a stack should operate as, what is a double stack?
Double stack probably means a stack that stores values of type double.
Last edited on
Thank you!! I think I sort of understood the overall idea of what I was supposed to do. Here are the updates I made and I still have 2 warnings generating when I compile my .cpp file. Please let me know if I implemented the .h file right! Thanks again for all the help!

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
.h file:
#include	<stdlib.h>
using namespace std;

class DoubleStack { 

	private:
	double	*data;		// Pointer to dynamic stack area.
	size_t	stack_size;	// Capacity of stack.
	size_t	tos;		// Top of stack. tos==0 ==> empty stack.

	public:
		// Constructor.
	DoubleStack(size_t capacity);
	 
		// Copy Constructor.	
	DoubleStack(const DoubleStack& rhs);
	
		// Destructor.
	~DoubleStack(void);
	
		// Assignment operator=.		
	DoubleStack&	operator=(DoubleStack& rhs);
	
	size_t getTOS();
	
		// Add item to stack, making it the topmost item.
		// Return value 1=success/0=failure.
	int	push(double& item);

		// Remove top item from stack and return it in item.
		// Return value 1=success/0=failure.
	int	pop(double& item);

		// Determine if the stack is empty.
		// Return value 1=empty/0=non-empty.
	int	empty(void);
	
		// Inquire the capacity of the stack.
	size_t	capacity(void);
	
		// Inquire the number of items on the stack.	
	size_t	size(void);

		// Compare 2 stacks to see if the contain the same data.
	int	operator==(DoubleStack& rhs);
};

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
.cpp file
#include "../include/dblstk.h"
#include <stack>


DoubleStack::DoubleStack(size_t capacity):stack_size(capacity) {}

DoubleStack::DoubleStack(const DoubleStack& rhs):stack_size(this->capacity()){};

size_t	DoubleStack::capacity() {
	return stack_size;
}

size_t DoubleStack::getTOS(){
	return tos;
}

DoubleStack::~DoubleStack(void){}

DoubleStack& DoubleStack::operator=(DoubleStack& rhs){
	
}

int	DoubleStack::push(double& item) {
	stack<double> mystack;
	int index = 0;
	while (index < this->capacity()){
		mystack.push(item);
		index++;
	}

	if(this->getTOS() == item){
		return 1;
	} else {
		return 0;
	}
}

int	DoubleStack::pop(double& item){
	stack<double> mystack;
	int index = 0;
	while (index > 0) {
		mystack.pop();
		index++;
	}

	if (this->getTOS() != item){
		return 1;
	} else {
		return 0;
	}
}

int	DoubleStack::empty(void) {
	stack<double> mystack;
	if (mystack.empty()){
		return 1;
	} else {
		return 0;
	}
}

size_t	DoubleStack::size(void) {
	stack<double> mystack;
	return mystack.size();
}


the errors generated:
In file included from /Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment4/src/dblstk.cpp:9:
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment4/src/../include/dblstk.h:10:17: warning: using directive refers to implicitly-defined namespace 'std'
using namespace std;
                ^
/Users/renathomas/Documents/online_courses/C++Programming/Assignments/Assignment4/src/dblstk.cpp:29:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
2 warnings generated.
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[Finished in 0.5s with exit code 1]
Last edited on
I don't know how to implement this Assignment Operator function and the equal operator function :( !!!

is this right??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
DoubleStack& DoubleStack::operator=(DoubleStack& rhs){
	stack_size = stack_size.rhs;

int	DoubleStack::operator==(DoubleStack& rhs) {
	stack<double> mystack;
	if (for (int i = 0 ; i < mystack.size(); i++) {
			mystack[i] == rhs[i];
	}) {
		return 1;
	} else {
		return 0;
	}
}

}
Last edited on
is this right??

No. And you know it isn't because it won't compile. In such cases, supply the error messages generated. You cannot define one member function inside of another member function, and even if you could it would make no sense to do so.
Last edited on
Topic archived. No new replies allowed.