#include<iostream>
using namespace std;
struct Link
{
int data;
Link *next;
};
class List
{
private:
Link *first;
public:
List()
{
first=NULL;
}
void set_values(int v)
{
Link *ptr,*temp;
if(first==NULL)
{
first=new Link;
first->data=v;
first->next=NULL;
}
else
{
ptr=first;
while(ptr->next!=NULL)
ptr=ptr->next;
temp=new Link;
temp->data=v;
temp->next=NULL;
ptr->next=temp;
}
}
void get_values()
{
Link *temp;
temp=first;
cout<<"Inserted Integers are..."<<endl;
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
}
cout<<"\n\n";
}
void sort_values()
{
Link *temp1,*temp2;
temp1=first;
int a;
while(temp1->next!=NULL)
{
temp2=first;
while(temp2->next!=NULL)
{
if(temp2->data>temp2->next->data)
{
a=temp2->data;
temp2->data=temp2->next->data;
temp2->next->data=a;
}
temp2=temp2->next;
}
temp1=temp1->next;
}
cout<<"\n\n";
temp1=first;
while(temp1!=NULL)
{
cout<<temp1->data<<"\t";
temp1=temp1->next;
}
}
};
int main()
{
List s1;
Link *temp,*first;
first=NULL;
temp=first;
int value;
char choice;
int count=0;
do
{
cout<<"Do you want to add data in the List,press 'y' for yes & press 'n' for no."<<endl;
cin>>choice;
if(choice=='y')
{
cout<<"Enter an Integer:"<<endl;
cin>>value;
s1.set_values(value);
}
else if(choice=='n')
{
cout<<"\nCongratulations!!!Data has been entered successfully!!!"<<endl;
cout<<"Thank you v.much!!!"<<endl;
}
else
{
cout<<"Invalid choice,,,Please try again!!!"<<endl;
}
count++;
}while(choice!='n');
cout<<"\n\nDo you wish to see the stored data?press 'w' for this query:"<<endl;
cin>>choice;
if(choice=='w')
{
count=count-1;
s1.get_values();
cout<<"Array has "<<count<<" elements in it"<<endl;
s1.sort_values();
}
else
{
cout<<"\n\nThank you very much once again!!!"<<endl;
exit(0);
}
return 0;
}
No comments:
Post a Comment