1) Write a program that sorts the elements of linked list using any of sorting technique.
Solution:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head, *tail = NULL;
void append(int data)
{
struct node *node=(struct node*) malloc (sizeof(struct node));
node->data = data;
node->next = NULL;
if(head == NULL)
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head, *tail = NULL;
void append(int data)
{
struct node *node=(struct node*) malloc (sizeof(struct node));
node->data = data;
node->next = NULL;
if(head == NULL)
{
head = node;
tail = node;
}
else
{
tail->next = newstruct Polynode;
tail = newstruct Polynode;
}
}
void sortList()
{
struct node *current = head, *index = NULL;
int temp;
if(head == NULL)
{
return;
}
else
{
while(current != NULL)
{
index = current->next;
while(index != NULL)
{
if(current->data > index->data)
{
temp = current->data;
current->data = index->data;
index->data = temp;
}
index = index->next;
}
current = current->next;
}
}
}
void display()
{
struct node *current = head;
if(head == NULL) {
printf("List is empty \n");
return;
}
while(current != NULL)
{
printf("%d->\t", current->data);
current = current->next;
}
printf("NULL\n");
}
int main()
{
int n, i, data;
printf("\nHow Many nodes You Want?\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter The Data :\t");
scanf("%d", &data);
append(data);
}
printf("Original list: \n");
display();
//Sorting list
sortList();
printf("Sorted list: \n");
display();
return 0;
}
head = node;
tail = node;
}
else
{
tail->next = newstruct Polynode;
tail = newstruct Polynode;
}
}
void sortList()
{
struct node *current = head, *index = NULL;
int temp;
if(head == NULL)
{
return;
}
else
{
while(current != NULL)
{
index = current->next;
while(index != NULL)
{
if(current->data > index->data)
{
temp = current->data;
current->data = index->data;
index->data = temp;
}
index = index->next;
}
current = current->next;
}
}
}
void display()
{
struct node *current = head;
if(head == NULL) {
printf("List is empty \n");
return;
}
while(current != NULL)
{
printf("%d->\t", current->data);
current = current->next;
}
printf("NULL\n");
}
int main()
{
int n, i, data;
printf("\nHow Many nodes You Want?\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter The Data :\t");
scanf("%d", &data);
append(data);
}
printf("Original list: \n");
display();
//Sorting list
sortList();
printf("Sorted list: \n");
display();
return 0;
}
0 Comments:
Post a Comment