stack and queue commenting

I am having difficulty in adding well explained comments to this code. does anyone have any help please or guidance?

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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
using namespace std; 


template<typename T>
class Node
{
public:
	T value;
	Node *pNext = nullptr;
	Node *pPrevious = nullptr;
};

template <typename T>
class Stack
{
public:

	~Stack(void);
	void Push(T value);
	Node<T>* NodePop(void);
	T Pop(void);
protected:
	int count = 0;
	Node<T> *pTop = nullptr;
};

template <typename T>
Stack<T>::~Stack(void)
{
	while (pTop != nullptr) { delete NodePop(); }
}

template <typename T>

Node<T>* Stack<T>::NodePop(void)
{
	Node<T> *pTmp = pTop;
	if (pTop != nullptr) pTop = pTop->pNext;
	return pTmp;
}

template <typename T>

void Stack<T>::Push(T value)
{
	Node<T>* pTmp = new Node<T>;
	pTmp->value = value;
	pTmp->pNext = pTop;
	pTop = pTmp;

}

template <typename T>

T Stack<T>::Pop(void)
{
	Node<T> *pTmp = NodePop();
	int ret = 0;

	if (pTmp != nullptr)
	{
		ret = pTmp->value;
	}
	else
	{
		throw "Stack Empty";
	}
	delete pTmp;

	return ret;
}

template <typename T>
class Queue : protected Stack<T>
{
private:
	Node<T> *pFront;
public:
	Queue(void);
	~Queue(void);
	virtual void enqueue(T value);
	virtual Node<T>* nodeDequeue(void);
	virtual T dequeue(void);
};

template<typename T>
void Queue<T>::enqueue(T value)
{
	this->Push(value);

	if (pFront == nullptr) pFront = pTop;
	else (pTop->pNext)->pPrevious = pTop;
}
template<typename T>
Queue<T>::Queue(void)
{
	pFront = pTop = nullptr;
}

template<typename T>
Queue<T>::~Queue(void)
{
	//uses stack destructor
}
template<typename T>
Node<T>* Queue<T>::nodeDequeue(void)
{
	Node<T> *pTmp = pFront;
	if (pFront != nullptr)
	{
		pFront = pFront->pPrevious;
		if (pFront != nullptr) pFront->pNext = nullptr;
	}
	return pTmp;
}

template<typename T>
T Queue<T>::dequeue(void)
{
	Node<T> *pTmp = nodeDequeue();
	T ret = 0;

	if (pTmp != nullptr)
	{
		ret = pTmp->value;
	}
	else
	{
		throw "Queue Empty";
	}
	if (pFront == nullptr) pTop = pFront; //last node has been removed
	delete pTmp;
	return ret;
}

int main(void)
{
	Stack<int> MyStack;
	Queue<int> MyQueue;

	for (int count = 0; count < 20; count++)
	{
		MyStack.Push(count);
		MyQueue.enqueue(count);
	}
	try
	{
		cout << "Stack output:" << endl;

		while (true)
		{
			cout << MyStack.Pop() << " ";
		}

	}
	catch (...)
	{
		cout << endl;
	}

	try
	{
		cout << "Queue output:" << endl;

		while (true)
		{
			cout << MyQueue.dequeue() << " ";
		}
	}
	catch (...)
	{
		cout << endl;
	}
	return 0;
}
Last edited on
if you wrote this program yourself then ask yourself what made your write each bit of it the way you did and jot down those thoughts - this will give you the material you need to make comments out of
IMO, a lot of your code doesn't really need comments. If one chooses meaningful names for variables and functions, then the code itself tells a story of what is happening - so well done there :+)

Comments can be handy to explain why something is being done; invariants; expected ranges of values; pre and post conditions.

Btw, there has never been a requirement in C++ for void as function parameter, just leave it empty.

This looks a bit sketchy - Does it work? I see it throws when the end is reached, that's not a good use for an exception.

151
152
153
154
	while (true)
		{
			cout << MyStack.Pop() << " ";
		}


I am not keen on infinite loops with no obvious exit in the code.

Ideally, one would define iterators, then use a range based for loop to do things like print all the elements.

The catch clauses are pointless at the moment, at least print what the error is.

Good Luck !!

Last edited on
Topic archived. No new replies allowed.