Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
  • 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 regarding using namespace std.

  • If you have a C++11-compliant compiler, use nullptr nullptr instead of NULL.

  • 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 to main() and char* 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 decrement length, depending on the list operation. Your inline "getter" will, of course, return this updated length.

    Regarding incrementing/decrementing, length += 1 and length -= 1 can respectively be length++ and length--.

  • The val argument for add() should be passed by const&, 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 it void and simply return 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) {}
    
  • Be aware of the issues regarding using namespace std.

  • If you have a C++11-compliant compiler, use nullptr instead of NULL.

  • 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 to main() and char* 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 decrement length, depending on the list operation. Your inline "getter" will, of course, return this updated length.

    Regarding incrementing/decrementing, length += 1 and length -= 1 can respectively be length++ and length--.

  • The val argument for add() should be passed by const&, 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 it void and simply return 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) {}
    
  • Be aware of the issues regarding using namespace std.

  • If you have a C++11-compliant compiler, use nullptr instead of NULL.

  • 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 to main() and char* 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 decrement length, depending on the list operation. Your inline "getter" will, of course, return this updated length.

    Regarding incrementing/decrementing, length += 1 and length -= 1 can respectively be length++ and length--.

  • The val argument for add() should be passed by const&, 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 it void and simply return if the list is already empty.

  • Instead of (*start).value, use the -> operator for readability: start->value.

Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
  • 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.

  • If you have a C++11-compliant compiler, use nullptr instead of NULL.

  • 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 to main() and char* 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 decrement length, depending on the list operation. Your inline "getter" will, of course, return this updated length.

    Regarding incrementing/decrementing, length += 1 and length -= 1 can respectively be length++ and length--.

  • The val argument for add() should be passed by const&, 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 it void and simply return if the list is already empty.

  • Instead of (*start).value, use the -> operator for readability: start->value.

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /