Slip 30 - B) Write a ‘C’ program to sort elements of a singly linked list in ascending order and display the sorted List.

Solution:

// Write a 'C' program to sort elements of a singly linked list in ascending order and display the sorted List.

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

struct node {
int data;
struct node* link;
};
 
struct node* root = NULL;



void append() 
{
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
scanf("%d", &temp->data);
temp->link = NULL;
if(root == NULL) {
root = temp;
}
else {
struct node* p;
p = root;
while(p->link != NULL) {
p = p->link;
}
p->link = temp;
}
}

void display() {
struct node* temp;
temp = root;
if(temp == NULL) {
printf("\nLIST IS EMPTY!!\n");
}
else {
printf("\n");
while(temp != NULL) {
printf("%d\t", temp->data);
temp = temp->link;
}
printf("\n");
}
}

void sort() {
struct node *p, *q;
int temp;
for(p = root; p != NULL; p = p->link) {
for(q = p->link; q != NULL; q = q->link) {
if(p->data > q->data) {
temp = p->data;
p->data = q->data;
q->data = temp;
}
}
}
}
void main() {
int n, i;
printf("\nEnter the number of nodes: ");
scanf("%d", &n);
printf("\nEnter node data: \n");
for(i=0; i<n; i++)
{
append();
}
printf("\nOriginal linked list: \n");
display();
sort();
printf("\nSorted linked list is: \n");
display();
}

Post a Comment

0 Comments