Bubble sort of linked list in c

Bubble sort in linked list

C program for Bubble sort on linked list. Here more solutions.

// Include header file
#include <stdio.h>
#include <stdlib.h>
// C program for
// Bubble Sort For Linked List
typedef struct Node
{
    // Define useful field of Node
    int data;
    struct Node *next;
} Node;
Node *getNode(int data){
    // Create dynamic memory of Node
    Node * ref = (Node *) malloc(sizeof(Node));
    if(ref == NULL){
        // Failed to create memory
        return NULL;
    }
    ref->data = data;
    ref->next = NULL;
    return ref;
}
typedef struct LinkedList
{
    // Define useful field of LinkedList
    struct Node *head;
} LinkedList;
LinkedList *getLinkedList(){
    // Create dynamic memory of LinkedList
    LinkedList * ref = (LinkedList *) malloc(sizeof(LinkedList));
    if(ref == NULL){
        // Failed to create memory
        return NULL;
    }
    ref->head = NULL;
    return ref;
}
// Add node at the beginning of linked list
void  insert (LinkedList* ref, int value)
{
    // Create a new node
    Node *node = getNode(value);
    // Add node at front
    node->next = ref->head;
    // Make new head
    ref->head = node;
}
// Display all elements
void  display (LinkedList* ref)
{
    if (ref->head != NULL)
    {
        Node *temp = ref->head;
        while(temp != NULL)
        {
            // Display node value
            printf("  %d" , temp->data);
            // Visit to next node
            temp = temp->next;
        }
    }
    else
    {
        printf("Empty Linked list");
    }
}
// Perform bubble sort in single linked list
void  bubbleSort (LinkedList* ref)
{
    if (ref->head != NULL)
    {
        Node *current = NULL;
        int status = 0;
        do {
            // Start with first node    
            current = ref->head;
            // Reset working status
            status = 0;
            while(current != NULL && current->next != NULL)
            {
                if (current->data > current->next->data)
                {
                    // Swap node values
                    current->data = current->data + current->next->data;
                    current->next->data = current->data - current->next->data;
                    current->data = current->data - current->next->data;
                    // When node value change    
                    status = 1;
                }
                // Visit to next node
                current = current->next;
            }
        }         while (status);
    }
    else
    {
        printf("Empty Linked list");
    }
}

int main()
{
    LinkedList *task = getLinkedList();
    // Insert element of linked list
    insert(task,15);
    insert(task,5);
    insert(task,42);
    insert(task,9);
    insert(task,50);
    insert(task,7);
    printf(" Before sort : ");
    // Display all node
    display(task);
    bubbleSort(task);
    printf("\n After sort  : ");
    display(task);
	return 0; 
}

Output

 Before sort :   7  50  9  42  5  15
 After sort  :   5  7  9  15  42  50



Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2022, kalkicode.com, All rights reserved