Your "getters" should be
const
since they don't modify any data members. This will also protect you against any accidental modifications in those functions.unsigned int getLength() const { return length; }
The constructor can instead be an initializer list:
linked_list() : length(0), start(NULL) {}
Be aware of the issues regarding
using namespace std
Be aware of the issues regardingusing namespace std
.If you have a C++11-compliant compiler, use
nullptr
nullptr
instead ofNULL
.Be aware that
_tmain()
and_TCHAR*
are non-portable, so anyone using this code outside of Visual Studio will not be able to compile it. In order to make this portable, change them tomain()
andchar*
respectively.Since you're using
std::pow()
, you should include<cmath>
. Although<iostream>
is already covering it, you should add this library anyway.There's not really a need for
getLength2()
. You just need to increment or decrementlength
, depending on the list operation. Your inline "getter" will, of course, return this updated length.Regarding incrementing/decrementing,
length += 1
andlength -= 1
can respectively belength++
andlength--
.The
val
argument foradd()
should be passed byconst&
, especially if it happens to be of a non-primitive type.You should consider error-checking (empty list) in
pop()
if you'd still like it to return the popped value. Otherwise, you can make itvoid
and simplyreturn
if the list is already empty.Instead of
(*start).value
, use the->
operator for readability:start->value
.
Your "getters" should be
const
since they don't modify any data members. This will also protect you against any accidental modifications in those functions.unsigned int getLength() const { return length; }
The constructor can instead be an initializer list:
linked_list() : length(0), start(NULL) {}
If you have a C++11-compliant compiler, use
nullptr
instead ofNULL
.Be aware that
_tmain()
and_TCHAR*
are non-portable, so anyone using this code outside of Visual Studio will not be able to compile it. In order to make this portable, change them tomain()
andchar*
respectively.Since you're using
std::pow()
, you should include<cmath>
. Although<iostream>
is already covering it, you should add this library anyway.There's not really a need for
getLength2()
. You just need to increment or decrementlength
, depending on the list operation. Your inline "getter" will, of course, return this updated length.Regarding incrementing/decrementing,
length += 1
andlength -= 1
can respectively belength++
andlength--
.The
val
argument foradd()
should be passed byconst&
, especially if it happens to be of a non-primitive type.You should consider error-checking (empty list) in
pop()
if you'd still like it to return the popped value. Otherwise, you can make itvoid
and simplyreturn
if the list is already empty.Instead of
(*start).value
, use the->
operator for readability:start->value
.
Your "getters" should be
const
since they don't modify any data members. This will also protect you against any accidental modifications in those functions.unsigned int getLength() const { return length; }
The constructor can instead be an initializer list:
linked_list() : length(0), start(NULL) {}
If you have a C++11-compliant compiler, use
nullptr
instead ofNULL
.Be aware that
_tmain()
and_TCHAR*
are non-portable, so anyone using this code outside of Visual Studio will not be able to compile it. In order to make this portable, change them tomain()
andchar*
respectively.Since you're using
std::pow()
, you should include<cmath>
. Although<iostream>
is already covering it, you should add this library anyway.There's not really a need for
getLength2()
. You just need to increment or decrementlength
, depending on the list operation. Your inline "getter" will, of course, return this updated length.Regarding incrementing/decrementing,
length += 1
andlength -= 1
can respectively belength++
andlength--
.The
val
argument foradd()
should be passed byconst&
, especially if it happens to be of a non-primitive type.You should consider error-checking (empty list) in
pop()
if you'd still like it to return the popped value. Otherwise, you can make itvoid
and simplyreturn
if the list is already empty.Instead of
(*start).value
, use the->
operator for readability:start->value
.
Your "getters" should be
const
since they don't modify any data members. This will also protect you against any accidental modifications in those functions.unsigned int getLength() const { return length; }
The constructor can instead be an initializer list:
linked_list() : length(0), start(NULL) {}
If you have a C++11-compliant compiler, use
nullptr
instead ofNULL
.Be aware that
_tmain()
and_TCHAR*
are non-portable, so anyone using this code outside of Visual Studio will not be able to compile it. In order to make this portable, change them tomain()
andchar*
respectively.Since you're using
std::pow()
, you should include<cmath>
. Although<iostream>
is already covering it, you should add this library anyway.There's not really a need for
getLength2()
. You just need to increment or decrementlength
, depending on the list operation. Your inline "getter" will, of course, return this updated length.Regarding incrementing/decrementing,
length += 1
andlength -= 1
can respectively belength++
andlength--
.The
val
argument foradd()
should be passed byconst&
, especially if it happens to be of a non-primitive type.You should consider error-checking (empty list) in
pop()
if you'd still like it to return the popped value. Otherwise, you can make itvoid
and simplyreturn
if the list is already empty.Instead of
(*start).value
, use the->
operator for readability:start->value
.