Quantcast
Channel: C Programming Archives - QnA Plus
Viewing all articles
Browse latest Browse all 93

C Program to Sort Linked List without Allocating Extra Memory

$
0
0

We’ll first create a linked list which is not sorted, then we’ll re-arrange the list such that the list becomes sorted.

Logic is simple, we’ll iterate the list for all nodes, in every iteration we’ll take the first node out of the original list and insert that node into a new list. At any point of time the new list is sorted, and we’ll insert a new node in the new list such a way that new list remains sorted.

It is demonstrated graphically.

Linked list sorting

 

#include <stdio.h>
#include <stdlib.h>

struct node{
    int val;
    struct node *next;
};

void print_list(struct node *head)
{
    printf("H->");

    while(head)
    {
        printf("%d->", head->val);
        head = head->next;
    }

    printf("|||\n");
}

void insert_front(struct node **head, int value)
{
    struct node * new_node = NULL;

    new_node = (struct node *)malloc(sizeof(struct node));

    if (new_node == NULL)
    {
        printf("Failed to insert element. Out of memory");
    }

    new_node->val = value;
    new_node->next = *head;

    *head = new_node;
}

void insert_sorted(struct node **head, struct node *new_node)
{
    struct node *tmp_node = NULL;

    if(*head == NULL || new_node->val < (*head)->val)
    {
        new_node->next = *head;
        *head = new_node;
        return;
    }

        tmp_node = *head;

    while(tmp_node->next && new_node->val > tmp_node->next->val)
    {
        tmp_node = tmp_node->next;
    }

    new_node->next = tmp_node->next;
    tmp_node->next = new_node;
}

void sort_list(struct node **head)
{
    struct node *new_head = NULL;
    struct node *tmp = *head;
    struct node *tmp1 = NULL;

    while(tmp)
    {
        tmp1 = tmp;
        tmp = tmp->next;

        insert_sorted(&new_head, tmp1);
    }

    *head = new_head;
}

void main()
{
    int count = 0, i, val;
    struct node * head = NULL;

    printf("Enter number of elements: ");
    scanf("%d", &count);

    for (i = 0; i < count; i++)
    {
        printf("Enter %dth element: ", i);
        scanf("%d", &val);
        insert_front(&head, val);
    }

    printf("Link list before sorting: ");
    print_list(head);

    sort_list(&head);

    printf("Sorted linked list: ");
    print_list(head);
}

Output:

Enter number of elements: 5
Enter 0th element: 10
Enter 1th element: 6
Enter 2th element: 18
Enter 3th element: 9
Enter 4th element: 24
Link list before sorting: H->24->9->18->6->10->|||
Sorted linked list: H->6->9->10->18->24->|||

The logic described above is implemented in this program. The sort_list() function takes the first node out of the original list and passes that to the insert_sorted() function. The insert_sorted() places the node in the new list such that the list remains sorted. This is like insertion sort, at any moment, a portion of the list will be sorted. And in every iteration, a new node will be inserted into the sorted portion such that the sorted portion remains sorted with one more node.

The post C Program to Sort Linked List without Allocating Extra Memory appeared first on QnA Plus.


Viewing all articles
Browse latest Browse all 93

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>