زبان ++c-یک لیست پیوندی طبقه بندی شده
/* Strictly --- Enter Only Integer Data otherwise It'll go in Infinite Loop */
#include "iostream.h"
#include " process.h"
class link
{
link* next;
int data,no;
public :
link* sort(link*);
void traverse(link*);
//link* dele (link*);
};
link * link::sort(link* temp)
{
link* newlink;
link* dummy1;
newlink=new link;
cout<<"\nEnter Data (only positive number) :";
cin>>no;
dummy1 = temp;
if (temp==NULL || dummy1->data > no)
{
newlink->data = no;
newlink->next=temp;
temp=newlink;
return temp;
}
else
{
link *cur;
link *dummy;
cur=temp;
dummy = cur->next ;
while(cur->next!=NULL)
{
if(dummy->data > no)
{
newlink->next = cur->next;
newlink->data = no;
cur->next = newlink;
return temp;
}
dummy=dummy->next ;
cur=cur->next;
}
newlink->data = no;
newlink->next=NULL;
cur->next=newlink;
}
return temp;
}
void link::traverse(link *temp)
{
while(temp!=NULL)
{
cout<
}
}
void main()
{
link *first=NULL,l1;
int choice;
while(1)
{
cout<<"\n*************** SORTED LINK LIST (For Integer Only) ***************\n\n";
cout<<"Choices Are :-\n=> [1] For Insert \n=> [2] For Traverse \n=> [3] For Exit";
cout<<"\n\nEnter Your choice : ";
cin>>choice;
switch (choice)
{
case 1:
first=l1.sort(first);
break;
case 2:
l1.traverse(first);
break;
case 3:
exit(0);
}
}
}