Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. struct node* create_sorted_list(struct node *head)
  2. {
  3. struct node * curr = head;
  4. struct node * sorted_head = malloc(sizeof(struct node));
  5.  
  6. while(curr != NULL){
  7. add_item_sorted(sorted_head, curr->data);
  8. curr=curr->next;
  9. }
  10. return sorted_head;
  11. }
  12.  
  13.  
  14.  
  15. struct node* add_item_sorted(struct node *sorted_head, int data)
  16. {
  17. struct node * curr = sorted_head;
  18. struct node * newN;
  19. while(curr->next != NULL){
  20.  
  21. if(data > curr->next->data){
  22. curr=curr->next;
  23. }
  24. else{
  25. newN = malloc(sizeof(struct node));
  26. newN->data = data;
  27. newN->next = curr->next;
  28. curr->next = newN;
  29. return sorted_head;
  30. }
  31. }
  32. newN = malloc(sizeof(struct node));
  33. curr->next = newN;
  34. newN->data = data;
  35. return sorted_head;
  36. }
  37.  
  38.  
  39.  
  40. int main(int argc, char *argv[])
  41. {
  42. ......
  43. struct node * sorted_head = create_sorted_list(head);
  44.  
  45. //head in this case comes from an unsorted linked list with the
  46. //same data values. Using head linked list to make sorted_head.
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement