Hello,
I am trying to programm a linked list with some extra methods, which are "sum", which adds up all the numbers in the linked list(isEmpty is implemented). The second function returns the size of the list (list_size) and the typical at function, which is most likely used for strings (list_at) to return a specific position out of the list. I want the last function to sort the list with a selection sort algorithm, which is my biggest problem right now, because I am new to pointers. Can you look at my implementation and give me some feedback/corrections?
first, feedback.
assuming you already have 2 functions in your class somewhere : 1 that inserts a new node, and one that deletes a node. Is this correct?
if so, one way to do it is
*find the smallest value in the list*
*delete it from the list using existing debugged code*
*insert at top using existing debugged code*
repeat, but ignore the first value, second iteration, ignore first and second value, ... when searching for the smallest.
or similar, make a new list.
find largest value in old list.
delete it using debugged existing routine.
insert at top of new list.
for either one you can get fancy and avoid new/delete pairs by careful reuse of the node pointers but that is an advanced enhancement to it.
this will minimize the hands-on pointer stuff inside the sort, and make it a lot easier to debug.
second: someone new to pointers (and possibly everyone) should probably not be doing this:
while( (t2 = t1 -> next) != NULL)
Its fine, if you understand it, ok. But its a little cryptic and at least intermediate level stuff -- if this were the problem can you debug it and fix it?