>please check it and give me suggestion.
Um...no. We're not going to error check your homework for you; that's your job. If you have a specific problem or question, we'll be happy to assist.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
I think your idea of selection sort is off. To me, selection sort means taking an existing container and then sorting it, rather than sorting it while you add items to it---which sounds like insertion sort to me, and appears to be what you are doing with your code. To use selection sort to sort a list I would use two separate sections, one to create the list and one to sort it.
My understanding of the underlying principle of selection sort is to:
1) create a temporary variable to hold the location of the smallest item in the collection
2)assume the first item in the collection is the smallest.
3)compare it with the next item in the collection
4)if the next item is smaller than the current smallest store it in the temporary variable
5)repeat the comparison with every other item in the list so in the end you have the smallest item this time through the list
6)remove the smallest item from where it is and make it the first item.
7)go to the second item in the list and eventually remove the next smallest from where it is and make it the second in the list
8)repeat the process taking the smallest in the remaining unsorted section of the list and adding it to the end of the sorted section until you have the entire list sorted
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
If you're interested my initial version of pseudocode for implementing an insertion sort algorhythm for a singly linked list would be something like this:
//using input, create a list where each value from head to tail is in ascending order
declare head and current to be pointers to type mylist
declare a new mylist object using dynamic memory
assign member variables of the new mylist object values based on input
if list empty
assign the new mylist object to head
else
if new mylist object->value less than head->value
//make new mylist object the new head
assign head to new mylist object->next
assign new mylist object to head
else
//find first value in list that is equal to or greater than value in new mylist object
while current not last node in list and current->next->value less than value in new mylist object
assign current->next to current
if current is last node in list //then value in new mylist object is larger than anything in list so far
assign new mylist object to current->next
else //insert new mylist object between current and current->next
assign current->next to new mylist object->next
assign new mylist object to current->next
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396