Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct node* create_sorted_list(struct node *head)
- {
- struct node * curr = head;
- struct node * sorted_head = malloc(sizeof(struct node));
- while(curr != NULL){
- add_item_sorted(sorted_head, curr->data);
- curr=curr->next;
- }
- return sorted_head;
- }
- struct node* add_item_sorted(struct node *sorted_head, int data)
- {
- struct node * curr = sorted_head;
- struct node * newN;
- while(curr->next != NULL){
- if(data > curr->next->data){
- curr=curr->next;
- }
- else{
- newN = malloc(sizeof(struct node));
- newN->data = data;
- newN->next = curr->next;
- curr->next = newN;
- return sorted_head;
- }
- }
- newN = malloc(sizeof(struct node));
- curr->next = newN;
- newN->data = data;
- return sorted_head;
- }
- int main(int argc, char *argv[])
- {
- ......
- struct node * sorted_head = create_sorted_list(head);
- //head in this case comes from an unsorted linked list with the
- //same data values. Using head linked list to make sorted_head.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement