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
the .h file:
#include <stdlib.h>
usingnamespace 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.
intoperator==( DoubleStack& rhs );
};
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.
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?
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!
.h file:
#include <stdlib.h>
usingnamespace 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.
intoperator==(DoubleStack& rhs);
};
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]
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.