I need to build a dynamic stack and am struggling to get it to work
here is the pseudo code
Constructor: UnboundedStackImplementation ()
1. Initialize count to zero
2. Initialize top to null
push (T element)
1. Create a new node
2. Set the new node’s data to element
3. Set the new node’s next reference to the stack’s top
4. Set the stack’s top to the new node
5. Increment count
T pop()
1. Set the result to the result of the top algorithm (see below)
Note: A stack underflow exception may be thrown by top()
2. Set old node to top
3. Set top to the top node’s next reference
4. Delete top
5. Decrement count
6. Return result
T top()
1. If the stack is not empty
1.1. Set the result to the top node’s data
2. Else
2.1. Throw a stack underflow exception
3. End if
4. Return result
Boolean isEmpty()
1. Stack is empty if the top reference is null
Integer getSize()
1. Return count
Destructor: ~UnboundedStackImplemenation ()
This algorithm is required only for C++ implementations. Java does this automatically.
1. Loop until the stack is empty
1.1. Remove the top element from the stack
Hint: Use the pop algorithm
2. End loop
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
|
template<typename T>
class node
{
private:
node<T>* next;
T data;
public:
node(T newdata, node<T>* thenext)
{
int data= newdata;
int next=thenext;
}
void getdata()
{
return data;
}
void getnext()
{
return next;
}
};
template <typename T>
class stack
{
private:
int count;
node<T> *topnode;
public:
topnode()
{
if(!isempty())
{
int result;
result= topnode->getdata();
return result;
}
else {
throw<< stackunderflow()
}
}
bool isempty()
{
return (count==0);
}
void pop(){
{
node<T>*temp= topnode;
topnode = topnode->getnext;
delete temp;
count--;
}
void push(T data){
void push(T data){
gettop() = new node<T>(data, gettop);
count++;
}
int getsize(){
return count;
}
};
int main(int argc, char** argv) {
stack<int>
for (int i=0; i<5; ++i) push(i);
cout << "Popping out elements";
while (!isempty())
{
cout << ' ' << topnode();
pop();
}
cout <<endl<<getsize();
return 0;
}
|
I cant get it to run my node class is fine but I keep on getting errors in my stack class saying it ant identify my t datas and topnodes. im also struggling on my destructor and am not sure how to implement it I have most of it done just am really struggling trying to figure out whats wrong with it. REALLY need help, it will be greatly appreciated thank you.