Implementing a Sorting Algorithm into a Linked List Insert Function

Reply

Join Date: Sep 2008
Posts: 12
Reputation: ahspats is an unknown quantity at this point 
Solved Threads: 0
ahspats ahspats is offline Offline
Newbie Poster

Implementing a Sorting Algorithm into a Linked List Insert Function

 
0
  #1
Jan 16th, 2009
I'd like to implement a sorting algorithm(quicksort or bubblesort or mergesort... it doesn't matter. anything that sorts is ok) into my insert function, so that everytime i create a new element it'll be inserted into right position. The variable listptr is the pointer pointing to the address of the first element of the list, val is value according to which the sorting should be done and next is pointer pointing to position of next element of the list.

Could someone help me with this???

  1. liste_t* Insert (liste_t *listptr, int val) {
  2.  
  3. liste_t *lp = listptr;
  4.  
  5. if (listptr != 0) {
  6. while (listptr->next != 0){
  7. listptr = listptr->next;
  8. }
  9. listptr->next = (struct liste_t *) malloc (sizeof(liste_t));
  10. listptr = listptr->next;
  11. listptr->next = 0;
  12. listptr->Zahl = val;
  13. printf("\n");
  14. return lp;
  15. }
  16. else {
  17. listptr = (struct liste_t *) malloc (sizeof(liste_t));
  18. listptr->next = 0;
  19. listptr->Zahl = val;
  20. printf("\n");
  21. return listptr;
  22. }
  23. }
Last edited by ahspats; Jan 16th, 2009 at 9:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,563
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 196
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Implementing a Sorting Algorithm into a Linked List Insert Function

 
0
  #2
Jan 16th, 2009
That doesn't make sense. Maintain a sorted linked list by making sure that every time you do an insert, it is inserted in sorted order. No need for a sorting algorithm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: Implementing a Sorting Algorithm into a Linked List Insert Function

 
0
  #3
Jan 17th, 2009
As mentioned before, it makes more sense to make sure that the list is in order while inserting than to apply a sorting algorithm later.

You can try this:

  1. liste_t* Insert (liste_t *listptr, int val)
  2. {
  3.  
  4. liste_t *lp = listptr;
  5. liste_t *prev = NULL;
  6. liste_t *info = (struct liste_t *) malloc (sizeof(liste_t));
  7. info->Zahl = val;
  8. info->next = NULL;
  9.  
  10. if (listptr != 0)
  11. {
  12. while (listptr != 0 && listptr->Zahl < val)
  13. {
  14. prev = listptr;
  15. listptr = listptr->next;
  16. }
  17.  
  18. prev->next = info;
  19. info->next = listptr;
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: Implementing a Sorting Algorithm into a Linked List Insert Function

 
0
  #4
Jan 17th, 2009
In both the code above head of the list is changed, which is wrong!

Check this out:

  1. liste_t* Insert (liste_t *listptr, int val) {
  2. liste_t *tempHead = listptr;
  3. liste_t *pre;
  4.  
  5. liste_t *newNode = (struct liste_t *) malloc (sizeof(liste_t));
  6. newNode->val = val;
  7. newNode->next = NULL;
  8.  
  9. //if list is empty then make newNode as head
  10. if(tempHead == NULL) {
  11. listptr = newNode;
  12. return listptr;
  13. } else {
  14. pre = NULL;
  15. // if list is not empty loop through all the nodes
  16. while(tempHead != NULL && tempHead->val < val) {
  17. pre = tempHead;
  18. tempHead = tempHead->next;
  19. }
  20.  
  21. // if only one node is there
  22. if(pre == NULL) {
  23. // insert new nofde on top making newNode as head
  24. newNode->next = tempNode;
  25. listptr = newNode; // now head points to newNode at top
  26. } else {
  27. // insert newNode between pre and tempHead (current)
  28. pre->next = newNode;
  29. newNode->next = tempHead;
  30. }
  31. return listptr;
  32. }
  33. }
When you think you have done a lot, then be ready for YOUR downfall.
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