badge

C++ Program to imlpement SORTING LINK LIST

Program in c++ for SORTING IN LINK LIST...

DESCRIPTION:- In this program  i just entered  elements which u want to sort. Then a temp variable . In this i stored the value and compare each value with another one and then swap the value and print the value

OUTPUT:-




SOURCE CODE:-


#include<iostream>

#include<conio .h>

using namespace std;

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

void insert(int num)
{
node *temp;
node *new1=new node;
temp=p;
if(p==NULL)
{
new1->data=num;
new1->link=NULL;
p=new1;
}
else
{
while(temp->link!=NULL)
{
temp=temp->link;
}
new1->data=num;
new1->link=NULL;
temp->link=new1;
}
}


void sort()
{
node *temp,*temp1;
node *x=new node;
temp=p;
while(temp!=NULL)
{
temp1=temp->link;
while(temp1!=NULL)
{
if(temp->data>temp1->data)
{
x->data=temp->data;
temp->data=temp1->data;
temp1->data=x->data;
}
temp1=temp1->link;
}
temp=temp->link;
}
}


void display()
{
node *temp2;
temp2=p;
while(temp2!=NULL)
{
cout<data<<"\n";
temp2=temp2->link;
}
}


int main()
{
int num,i;
cout<<"How many elements u want to insert";
cin>>i;
while(i>0)
{
cout<<"Enter no";
cin>>num;
insert(num);
i--;
}
display();
cout<<"The sorted list is\n";
sort();
display();
}



/* IF U FOUND ANY PROBLEM IN THIS PROGRAM  OR IF YOU HAVE ANY QUERY RELATED THIS PROGRAM THEN PLEASE COMMENT IN THIS POST OR YOU CAN DIRECTLY MAIL ME ON piyushkhandelwal029@gmail.com . */

THANKU.


SHARE

Admin

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments: