Issue with stack program

so first off I am having issues with passing the T value cause for some reason im getting errors saying it cant resolve identifiers for T data or even in my constructors(same issue in stack class)secondly I also have to implement a destructor where I threw the stack underflow. its suppose to loop until stack is empty which I figure I should use a while loop and call my pop function but I am not quite sure. and lastly I am not sure if I wrote my push algorithm correctly. I am still a novice so please be patient with me try and go easy on me, I can use all the help I can get. thank you for time and 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
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
114
115
116
117
118
119
120
121
122
123
124
  template<typename T>

class node
{
	private: 
	node<T>* next;
	T data;
	

	public:
		node(T data, node<T>* next)
		{
			data= newdata;
			next=thenext;
		}


	void getdata()
        {
		return data;
	}
	void getnext()
        {
		return next;
	}
};



#include "node.h"

template <typename T>

class stack

	{
	
	private:
	
	int count;
	node<T> *topnode;

	
	public:
			 gettop()
			 {
				if(!isempty()){
				
				int result;
				
				result= gettop->getdata();
			
				return result;
			}
				else {
					throw<< stackunderflow()
				}
			}
			
			bool isempty()
			{
				return==0;
			}
			
			void pop(){
				
				
				node<T>*temp= gettop;
				
				T result= gettop();
				
				gettop=gettop->getnext();
				
				delete temp;
				
				count--;
			}
	
	
			void push(T data){
				
 				node<T>* newnode;
 				
  				newnode= newdata;
  				
 				newdata->thenext = gettop;
 				
  				newnode = gettop;
  				
				count++;
				
			
			}
			
			int getsize(){
				return count;
			}
};



#include <cstdlib>
#include <iostream>
#include"stack.h"

using namespace std;

int main(int argc, char** argv) {

  stack<int> stack;

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

  cout << "Popping out elements";
  while (!stack.empty())
  {
     cout << ' ' << mystack.gettop();
     mystack.pop();
  }
  cout <<endl<<getsize();
    


Line 62: return==0; must be return (count==0);

1
2
3
4
5
		node(T data newdata, node<T>* next thenext)
		{
			data= newdata;
			next=thenext;
		}

gettop is needless. Instead check it in pop():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
			void pop(){

	if(topnode) // Note
	{				
				node<T>*temp= topnode;
				
				T result= gettop(); // No result
				
				topnode=topnode->getnext();
				
				delete temp;
				
				count--;
}
	else  // Note
	  throw stackunderflow();
			}

push():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
			void push(T data){
				
 				topnode = new node<T>(data, topnode);
 				
  				newnode= newdata;
  				
 				newdata->thenext = gettop;
 				
  				newnode = gettop;
  				
				count++;
				
			
			}
Thank you for the reply and help! my node stack seems to be ok now. but the only thing is that I need that I need the top function this is the pseudo code that I need to implement so is there a way I can do it with the top function

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

Topic archived. No new replies allowed.