void insertAtTheBegin(struct Node **start_ref, int data)
{
struct Node *ptr1 = (struct Node*)malloc(sizeof(struct Node));
ptr1->data = data;
ptr1->next = *start_ref;
*start_ref = ptr1;
}
/* Function to print nodes in a given linked list */
void printList(struct Node *start)
{
struct Node *temp = start;
printf("\n");
while (temp!=NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
/* Bubble sort the given linked list */
void bubbleSort(struct Node *start)
{
int swapped, i;
struct Node *ptr1;
struct Node *lptr = NULL;
/* Checking for empty list */
if (start == NULL)
return;
do
{
swapped = 0;
ptr1 = start;
while (ptr1->next != lptr)
{
if (ptr1->data > ptr1->next->data)
{
swap(ptr1, ptr1->next);
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
}
while (swapped);
}
/* function to swap data of two nodes a and b*/
void swap(struct Node *a, struct Node *b)
{
int temp = a->data;
a->data = b->data;
b->data = temp;
}
give me a function sort to sort the linked list
`
Assistant
To add a sort function for sorting a linked list, you can simply call the existing bubbleSort function you provided. The sort function would act as a wrapper that could take the head of the linked list as an argument and invoke the sorting process.
Here’s an example of how you could implement the sort function:
c
// Function to sort the linked list
void sort(struct Node **start_ref)
{
// Check if the linked list is empty or has only one element
if (*start_ref == NULL || (*start_ref)->next == NULL)
return; // No need to sort
// Call the bubble sort function on the linked list
bubbleSort(*start_ref);
}
Complete Integration
You can integrate this sort function with the existing code like this:
c
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a Node
struct Node {
int data;
struct Node *next;
};
// Function to insert an element at the beginning of the linked list
void insertAtTheBegin(struct Node **start_ref, int data)
{
struct Node *ptr1 = (struct Node*)malloc(sizeof(struct Node));
ptr1->data = data;
ptr1->next = *start_ref;
*start_ref = ptr1;
}
// Function to print nodes in a given linked list
void printList(struct Node *start)
{
struct Node *temp = start;
printf("\n");
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
// Bubble sort the given linked list
void bubbleSort(struct Node *start)
{
int swapped, i;
struct Node *ptr1;
struct Node *lptr = NULL;
if (start == NULL)
return;
do
{
swapped = 0;
ptr1 = start;
while (ptr1->next != lptr)
{
if (ptr1->data > ptr1->next->data)
{
swap(ptr1, ptr1->next);
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
}
while (swapped);
}
// Function to swap data of two nodes
void swap(struct Node *a, struct Node *b)
{
int temp = a->data;
a->data = b->data;
b->data = temp;
}
// Function to sort the linked list
void sort(struct Node **start_ref)
{
if (*start_ref == NULL || (*start_ref)->next == NULL)
return;
bubbleSort(*start_ref);
}
// Main function to demonstrate the linked list and sort function
int main() {
struct Node* head = NULL;
insertAtTheBegin(&head, 4);
insertAtTheBegin(&head, 1);
insertAtTheBegin(&head, 3);
insertAtTheBegin(&head, 2);
printf("Original list:");
printList(head);
sort(&head);
printf("Sorted list:");
printList(head);
return 0;
}
Explanation
The sort function checks if the linked list is empty or has only one element. If that's the case, it simply returns.
Otherwise, it calls the bubbleSort function to perform the sorting.
The main function demonstrates how to create a linked list, insert some elements, print the list, sort it, and print the sorted list.
This approach keeps your implementation concise and utilizes your existing bubble sort functionality.