can someone help me check whether my linked list implementation is correct, as in without any logic errors? Do comment on any possible improvements that could be done as well
Tested then debug tested then debug until it seems to be working fine, but I want to know if there are still any errors or not as well as improvements in efficiency that can be done to the code.
As a software professional, I would develop a set of test cases:
1) Make sure that every line of code in the linked list implementation got executed
at least once;
2) Determine the "edge" cases and make sure I have tests for them;
3) Test every scenario I can think of that might affect the way the code executes.
Here's a start at a testcase list:
a) addToHead() called on empty list
b) addToHead() called on non-empty list
c) removeFromHead() called with list containing 0 elements;
d) removeFromHead() called with list containing exactly 1 element;
e) removeFromHead() called with list containing >1 element.
f) addNode() called on empty list;
g) addNode() called with list containing 1 element less than the value to insert;
h) addNode() called with list containing 1 element greater than value to insert;
i) addNode() called with list containing 1 element equal to value to insert;
j) g, h, and i with list containing > 1 element;
k) deleteNode() called on empty list;
l) deleteNode() called on list with 1 element, target element does not exist;
m) deleteNode() called on list with 1 element, target element exists;
n) l and m with list containing >1 element;
o) deleteAllNodes() called on empty list;
p) deleteAllNodes() called on non-empty list;
q) isEmpty() called on empty list;
r) isEmpty() called on non-empty list;
s) getNoOfNodes() called on initially empty list;
t) getNoOfNodes() called on non-empty list;
u) getNoOfNodes() called after addNode();
v) getNoOfNodes() called after addToHead();
w) getNoOfNodes() called after removeFromHead();
x) getNoOfNodes() called after deleteNode();
y) displayAllNodes() called on empty list;
z) displayAllNodes() called on non-empty list.
In true blackbox testing, there would still be more testcases I could argue should be executed, however I focused more on whitebox testing since this is development test.
You should write all of the testcases as functions that main() calls.
The bottom line is that software testing cannot prove that code is defect-free; it can only show that bugs do exist by virtue of a failed test case.