944,099 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2169
  • C++ RSS
Mar 21st, 2006
0

linkedlist

Expand Post »
help me with this code please.this code is for selection sort using linked list.please check it and give me suggestion.
C++ Syntax (Toggle Plain Text)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. struct mylist{
  6. mylist * nxt;
  7. int val;
  8. };
  9. int a,b,c,d=1,i,j;mylist * t;
  10. clrscr();
  11. cout<<"Enter the no&the total no of numbers";
  12. cin>>a>>b;
  13. mylist * head=new mylist;
  14. head=NULL;
  15. mylist * newnode=new mylist;
  16. newnode->val=a;
  17. newnode->nxt=head;
  18. head=newnode;
  19. for(i=1;i<b;i++)
  20. {
  21. d=1;
  22. cout<<"Enter the next number";
  23. cin>>c;
  24. mylist * newnode=new mylist;
  25. newnode->val=c;
  26. for(j=1;j<=i;j++)
  27. {
  28. if(newnode->val<head->val)
  29. {
  30.  
  31. if(d==1)
  32. {
  33. newnode->nxt=head;
  34. head=newnode;
  35. }
  36. if(d==b)
  37. {
  38. newnode->nxt=NULL;
  39. t->nxt=newnode;
  40. }
  41. if(1<d<b)
  42. {
  43. newnode->nxt=head;
  44. t->nxt=newnode;
  45. }
  46. }
  47. else
  48. {
  49. t=head;
  50. head=head->nxt;
  51. d=d+1;
  52. }
  53. }
  54. }
  55. cout<<"the new order is:\n ";
  56. while(head)
  57. {
  58. cout<<head->val<<" ";
  59. head=head->nxt;
  60. }
  61. getch();
  62. }
Last edited by Narue; Mar 21st, 2006 at 12:29 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vamsi.rgiit is offline Offline
5 posts
since Mar 2006
Mar 21st, 2006
0

Re: linkedlist

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 21st, 2006
0

Re: linkedlist

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
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Mar 21st, 2006
0

Re: linkedlist

Quote originally posted by Lerner ...
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:
create a temporary variable to hold the location of the smallest item in the collection
assume the first item in the collection is the smallest.
compare it with the next item in the collection
if the next item is smaller than the current smallest store it in the temporary variable
repeat the comparison with every other item in the list so in the end you have the smallest item
exchange the smallest item with the first item.
go to the second item in the list and eventually replace the second smallest with the second in the list
repeat the process until you have the entire list sorted




sorry its mistake to type selection its insertion.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vamsi.rgiit is offline Offline
5 posts
since Mar 2006
Mar 22nd, 2006
0

Re: linkedlist

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:
C++ Syntax (Toggle Plain Text)
  1. //using input, create a list where each value from head to tail is in ascending order
  2.  
  3. declare head and current to be pointers to type mylist
  4.  
  5. declare a new mylist object using dynamic memory
  6. assign member variables of the new mylist object values based on input
  7.  
  8. if list empty
  9. assign the new mylist object to head
  10.  
  11. else
  12. if new mylist object->value less than head->value
  13. //make new mylist object the new head
  14. assign head to new mylist object->next
  15. assign new mylist object to head
  16.  
  17. else
  18.  
  19. //find first value in list that is equal to or greater than value in new mylist object
  20. while current not last node in list and current->next->value less than value in new mylist object
  21. assign current->next to current
  22.  
  23. if current is last node in list //then value in new mylist object is larger than anything in list so far
  24. assign new mylist object to current->next
  25.  
  26. else //insert new mylist object between current and current->next
  27. assign current->next to new mylist object->next
  28. assign new mylist object to current->next
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Please Help
Next Thread in C++ Forum Timeline: Arrays problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC