linkedlist

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2006
Posts: 5
Reputation: vamsi.rgiit is an unknown quantity at this point 
Solved Threads: 0
vamsi.rgiit vamsi.rgiit is offline Offline
Newbie Poster

linkedlist

 
0
  #1
Mar 21st, 2006
help me with this code please.this code is for selection sort using linked list.please check it and give me suggestion.
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 716
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: linkedlist

 
0
  #2
Mar 21st, 2006
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: linkedlist

 
0
  #3
Mar 21st, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 5
Reputation: vamsi.rgiit is an unknown quantity at this point 
Solved Threads: 0
vamsi.rgiit vamsi.rgiit is offline Offline
Newbie Poster

Re: linkedlist

 
0
  #4
Mar 21st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

Re: linkedlist

 
0
  #5
Mar 22nd, 2006
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:
  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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC