943,902 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3145
  • C RSS
Jan 16th, 2009
0

Implementing a Sorting Algorithm into a Linked List Insert Function

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ahspats is offline Offline
17 posts
since Sep 2008
Jan 16th, 2009
0

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

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jan 17th, 2009
0

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

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;
Reputation Points: 124
Solved Threads: 18
Junior Poster
devnar is offline Offline
148 posts
since Sep 2008
Jan 17th, 2009
0

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

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. }
Reputation Points: 83
Solved Threads: 61
Posting Pro in Training
Luckychap is offline Offline
442 posts
since Aug 2006

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: help to write a function
Next Thread in C Forum Timeline: exchange 2 variables values without using 3rd variable.





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


Follow us on Twitter


© 2011 DaniWeb® LLC