having trouble finishing my stack

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.
Last edited on
Hi,

You need to work with an object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main() {

  stack<int> MyIntStack; // MyIntStack is the object

  for (int i=0; i<5; ++i) MyIntStack.push(i);

  cout << "Popping out elements";
  while (!MyIntStack.isempty())
  {
     cout << ' ' << MyIntStack.topnode();
     MyIntStack.pop();
  }
  cout <<endl<<MyIntStack.getsize();
    
    return 0;
}




Ohhh ok so by declaring myintstack it will allow me to call my functions from my other classes into the main program?
Yes.

Also, your function calls in the classes need parentheses.

It is confusing that you have a topnode variable and a topnode function. Perhaps you could use the same names as in the assignment? The function topnode needs a type for the return value.

Rhetorical question: Should pop() return a value ?
Ahh ok so instead i should call topnode function something like gettop ()? And use topnode as the variable instead. Also when you say use parenthesis im using parentheiss like this{ function () } when ever i call a function From another class
TheIdeasMan wrote:
Perhaps you could use the same names as in the assignment?
.... should call topnode function something like gettop ()?


Also when you say use parenthesis im using parentheiss like this{ function () } when ever i call a function ...


That's a very basic thing, a function call has to have ( ) even if there are no arguments.

You are also trying to assign the value of an expression to a function call which is very confused on your part. Have a read of the tutorial http://www.cplusplus.com/doc/tutorial/
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:
int gettop()
{
if(!isempty()){

int result;

result= topnode->getdata();

return result;
}
else {
throw<< stackunderflow()
}
}

bool isempty()
{
return==0;
}

void pop(){


node<T>*temp= topnode;

T result= gettop();

topnode=topnode->getnext();

delete temp;

count--;
}


void push(T data){

topnode = new node<T>(data, gettop);

count++;


}

int getsize(){
return count;
}
};


int main() {

stack<int> MyIntStack;

for (int i=0; i<5; ++i) MyIntStack.push(i);

cout << "Popping out elements";
while (!MyIntStack.isempty())
{
cout << ' ' << MyIntStack.gettop();
MyIntStack.pop();
}
cout <<endl<<MyIntStack.getsize();

return 0;
}


your advice helped a lot thank you! the only issue im getting in my stack class is setting up my destructor for stackunderflow.....im suppose to build a separate class for it but struggling on implementing it this is what I have

class stackunderflow(){

public:

~stackunderflow()
{
while(!isempty){ // calls on pop functions and checks to see if empty
pop();
}
};


this is the only part where just really confused on



Topic archived. No new replies allowed.