Do not use
using namespace std
Do not useusing namespace std
.In the constructor and
push()
, it looks like you're callingnew
on a function. It should benode
.Prefer
nullptr
toNULL
and0
for the node pointers if you're using C++11. Otherwise, you should choose eitherNULL
or0
for node pointers and keep it consistent.You don't need
this->
everywhere as the code already has the current object in scope.It's needless to call
pop()
in the destructor. For one thing,pop()
returns a value. You should just be usingdelete
.Instead, have
head
point to the first element, loop through the stack (stopping whenNULL
is hit) and calldelete
on each node. With this, you won't need todelete
head
at the end.pop()
andgetTopElement()
might be best asvoid
. Here, a0
is returned if the list is empty. Sure, no errors happening there. But then the calling code will take it as an element0
, never knowing whether or not the stack is empty. If you make themvoid
, you can simplyreturn
if the stack is empty. As for retrieving an element, that's why you havegetTopElement()
. Once you get the value withgetTopElement()
, callpop()
after that.Regarding checking for an empty stack, you could make a
public
empty()
function for calling code. The two functions above should still check for an empty stack, but containers like this usually utilizes such a function. This could go in the header:bool empty() const { return num_elements == 0; }
You could make your class more useful by using templates. This will allow you to populate the stack with any type instead of just
int
s.
In the constructor and
push()
, it looks like you're callingnew
on a function. It should benode
.Prefer
nullptr
toNULL
and0
for the node pointers if you're using C++11. Otherwise, you should choose eitherNULL
or0
for node pointers and keep it consistent.You don't need
this->
everywhere as the code already has the current object in scope.It's needless to call
pop()
in the destructor. For one thing,pop()
returns a value. You should just be usingdelete
.Instead, have
head
point to the first element, loop through the stack (stopping whenNULL
is hit) and calldelete
on each node. With this, you won't need todelete
head
at the end.pop()
andgetTopElement()
might be best asvoid
. Here, a0
is returned if the list is empty. Sure, no errors happening there. But then the calling code will take it as an element0
, never knowing whether or not the stack is empty. If you make themvoid
, you can simplyreturn
if the stack is empty. As for retrieving an element, that's why you havegetTopElement()
. Once you get the value withgetTopElement()
, callpop()
after that.Regarding checking for an empty stack, you could make a
public
empty()
function for calling code. The two functions above should still check for an empty stack, but containers like this usually utilizes such a function. This could go in the header:bool empty() const { return num_elements == 0; }
You could make your class more useful by using templates. This will allow you to populate the stack with any type instead of just
int
s.
In the constructor and
push()
, it looks like you're callingnew
on a function. It should benode
.Prefer
nullptr
toNULL
and0
for the node pointers if you're using C++11. Otherwise, you should choose eitherNULL
or0
for node pointers and keep it consistent.You don't need
this->
everywhere as the code already has the current object in scope.It's needless to call
pop()
in the destructor. For one thing,pop()
returns a value. You should just be usingdelete
.Instead, have
head
point to the first element, loop through the stack (stopping whenNULL
is hit) and calldelete
on each node. With this, you won't need todelete
head
at the end.pop()
andgetTopElement()
might be best asvoid
. Here, a0
is returned if the list is empty. Sure, no errors happening there. But then the calling code will take it as an element0
, never knowing whether or not the stack is empty. If you make themvoid
, you can simplyreturn
if the stack is empty. As for retrieving an element, that's why you havegetTopElement()
. Once you get the value withgetTopElement()
, callpop()
after that.Regarding checking for an empty stack, you could make a
public
empty()
function for calling code. The two functions above should still check for an empty stack, but containers like this usually utilizes such a function. This could go in the header:bool empty() const { return num_elements == 0; }
You could make your class more useful by using templates. This will allow you to populate the stack with any type instead of just
int
s.
In the constructor and
push()
, it looks like you're callingnew
on a function. It should benode
.It's best to have eachPrefer
head->nextnullptr
deal withtoNULL
(orandnullptr0
for the node pointers if you're using C++11) instead of0
. It's more accurate with memory addresses Otherwise, and it'll help you differentiate them from actual values ofshould choose either0NULL
or (such as0
for stack elements)node pointers and keep it consistent.You don't need
this->
everywhere as the code already has the current object in scope.It's needless to call
pop()
in the destructor. For one thing,pop()
returns a value. You should just be doing straight deletionusingdelete
. InsteadInstead, have
head
point to the first element, loop through the stack (stopping whenNULL
is hit) and calldelete
on each node. With this, you won't need todelete
head
at the end. But, to be on the safe side, you can sethead
toNULL
instead.pop()
andgetTopElement()
might be best asvoid
. Here, a0
is returned if the list is empty. Sure, no errors happening there. But then the calling code will take it as an element0
, never knowing whether or not the stack is empty. If you make themvoid
, you can simplyreturn
if the stack is empty. As for retrieving an element, that's why you havegetTopElement()
. Once you get the value withgetTopElement()
, callpop()
after that.Regarding checking for an empty stack, you could make a
public
empty()
function for calling code. The two functions above should still check for an empty stack, but containers like this usually utilizes such a function. This could go in the header:bool empty() const { return num_elements == 0; }
For proper type use,
size()
andnum_elements
should be of typestd::size_t
.You could make your class more useful by using templates. This will allow you to populate the stack with any type instead of just
int
s.
In the constructor and
push()
, it looks like you're callingnew
on a function. It should benode
.It's best to have each
head->next
deal withNULL
(ornullptr
if using C++11) instead of0
. It's more accurate with memory addresses, and it'll help you differentiate them from actual values of0
(such as for stack elements).You don't need
this->
everywhere as the code already has the current object in scope.It's needless to call
pop()
in the destructor. For one thing,pop()
returns a value. You should just be doing straight deletion. Instead, havehead
point to the first element, loop through the stack (stopping whenNULL
is hit) and calldelete
on each node. With this, you won't need todelete
head
at the end. But, to be on the safe side, you can sethead
toNULL
instead.pop()
andgetTopElement()
might be best asvoid
. Here, a0
is returned if the list is empty. Sure, no errors happening there. But then the calling code will take it as an element0
, never knowing whether or not the stack is empty. If you make themvoid
, you can simplyreturn
if the stack is empty. As for retrieving an element, that's why you havegetTopElement()
. Once you get the value withgetTopElement()
, callpop()
after that.Regarding checking for an empty stack, you could make a
public
empty()
function for calling code. The two functions above should still check for an empty stack, but containers like this usually utilizes such a function. This could go in the header:bool empty() const { return num_elements == 0; }
For proper type use,
size()
andnum_elements
should be of typestd::size_t
.You could make your class more useful by using templates. This will allow you to populate the stack with any type instead of just
int
s.
In the constructor and
push()
, it looks like you're callingnew
on a function. It should benode
.Prefer
nullptr
toNULL
and0
for the node pointers if you're using C++11. Otherwise, you should choose eitherNULL
or0
for node pointers and keep it consistent.You don't need
this->
everywhere as the code already has the current object in scope.It's needless to call
pop()
in the destructor. For one thing,pop()
returns a value. You should just be usingdelete
.Instead, have
head
point to the first element, loop through the stack (stopping whenNULL
is hit) and calldelete
on each node. With this, you won't need todelete
head
at the end.pop()
andgetTopElement()
might be best asvoid
. Here, a0
is returned if the list is empty. Sure, no errors happening there. But then the calling code will take it as an element0
, never knowing whether or not the stack is empty. If you make themvoid
, you can simplyreturn
if the stack is empty. As for retrieving an element, that's why you havegetTopElement()
. Once you get the value withgetTopElement()
, callpop()
after that.Regarding checking for an empty stack, you could make a
public
empty()
function for calling code. The two functions above should still check for an empty stack, but containers like this usually utilizes such a function. This could go in the header:bool empty() const { return num_elements == 0; }
You could make your class more useful by using templates. This will allow you to populate the stack with any type instead of just
int
s.
In the constructor and
push()
, it looks like you're callingnew
on a function. If It should benode
is your node type, then use that. It might be a good idea to post your header for more context.It's best to have each
head->next
deal withNULL
(ornullptr
if using C++11) instead of 00
. It's more accurate and safe with memory addresses, and it'll help you differentiate them from actual values of 00
(such as for stack elements themselves).You don't need
this->
everywhere as the code already has the current object in scope.It's needless to call
pop()
in the destructor. For one thing,pop()
returns a value. You should just be doing straight deletion. Instead, havehead
point to the first element, loop through the stack (stopping whenNULL
is hit) and calldelete
on each node. With this, you won't need todelete
head
at the end. But, to be on the safe side, you can sethead
toNULL
instead.pop()
andgetTopElement()
might be best asvoid
. Here, a 00
is returned if the list is empty. Sure, no errors happening there. But then the calling code will take it as an element 00
, never knowing whether or not the stack is empty. If you make themvoid
, you can simplyreturn
if the stack is empty. As for retrieving an element, that's why you havegetTopElement()
. Once you get the value withgetTopElement()
, callpop()
after that.Regarding checking for an empty stack, you could make a
public
empty()
function for calling code. The two functions above should still check for an empty stack, but containers like this usually utilizes such a function. This could go in the header:bool empty() const { return num_elements == 0; }
For proper type use,
size()
should return an andnum_elements
should be of typestd::size_t
.You could make your class more useful by using templates. This will allow you to populate the stack with any type instead of just
int
s.
In the constructor and
push()
, it looks like you're callingnew
on a function. Ifnode
is your node type, then use that. It might be a good idea to post your header for more context.It's best to have each
head->next
deal withNULL
(ornullptr
if using C++11) instead of 0. It's more accurate and safe with memory addresses, and it'll help you differentiate them from actual values of 0 (such as stack elements themselves).You don't need
this->
everywhere as the code already has the current object in scope.It's needless to call
pop()
in the destructor. For one thing,pop()
returns a value. You should just be doing straight deletion. Instead, havehead
point to the first element, loop through the stack (stopping whenNULL
is hit) and calldelete
on each node. With this, you won't need todelete
head
at the end. But, to be on the safe side, you can sethead
toNULL
instead.pop()
andgetTopElement()
might be best asvoid
. Here, a 0 is returned if the list is empty. Sure, no errors happening there. But then the calling code will take it as an element 0, never knowing whether or not the stack is empty. If you make themvoid
, you can simplyreturn
if the stack is empty. As for retrieving an element, that's why you havegetTopElement()
. Once you get the value withgetTopElement()
, callpop()
after that.Regarding checking for an empty stack, you could make a
public
empty()
function for calling code. The two functions above should still check for an empty stack, but containers like this usually utilizes such a function. This could go in the header:bool empty() const { return num_elements == 0; }
For proper type use,
size()
should return anstd::size_t
.You could make your class more useful by using templates. This will allow you to populate the stack with any type instead of just
int
s.
In the constructor and
push()
, it looks like you're callingnew
on a function. It should benode
.It's best to have each
head->next
deal withNULL
(ornullptr
if using C++11) instead of0
. It's more accurate with memory addresses, and it'll help you differentiate them from actual values of0
(such as for stack elements).You don't need
this->
everywhere as the code already has the current object in scope.It's needless to call
pop()
in the destructor. For one thing,pop()
returns a value. You should just be doing straight deletion. Instead, havehead
point to the first element, loop through the stack (stopping whenNULL
is hit) and calldelete
on each node. With this, you won't need todelete
head
at the end. But, to be on the safe side, you can sethead
toNULL
instead.pop()
andgetTopElement()
might be best asvoid
. Here, a0
is returned if the list is empty. Sure, no errors happening there. But then the calling code will take it as an element0
, never knowing whether or not the stack is empty. If you make themvoid
, you can simplyreturn
if the stack is empty. As for retrieving an element, that's why you havegetTopElement()
. Once you get the value withgetTopElement()
, callpop()
after that.Regarding checking for an empty stack, you could make a
public
empty()
function for calling code. The two functions above should still check for an empty stack, but containers like this usually utilizes such a function. This could go in the header:bool empty() const { return num_elements == 0; }
For proper type use,
size()
andnum_elements
should be of typestd::size_t
.You could make your class more useful by using templates. This will allow you to populate the stack with any type instead of just
int
s.