Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- include <iostream>
- using namespace std;
- class lln
- {
- public:
- int d;
- lln *ad;
- }*r;
- class ll
- {
- public:
- ll()
- {
- r = NULL;
- }
- void addbeg(int v);
- void addmiddle(int x, int v);
- void addend(int v);
- void deletebeg();
- int deletev(int x);
- void deleteend();
- int countlist();
- void showlist();
- };
- void ll::addbeg(int v)
- {
- lln *t1 = r;
- lln *t2 = new lln();
- t2->d = v;
- t2->ad = NULL;
- if (r == NULL)
- {
- t1 = r = t2;
- }
- else
- {
- t2->ad = t1;
- r = t2;
- }
- cout<<"\nThe Value "<<v<<" Added."<<endl;
- }
- void ll::addmiddle(int x, int v)
- {
- lln *t1 = r;
- lln *t2 = new lln();
- t2->d = v;
- t2->ad = NULL;
- while (t1->d != x)
- {
- t1 = t1->ad;
- }
- if (t1->ad == NULL)
- {
- t1->ad = t2;
- }
- else
- {
- lln *t3 = t1->ad;
- t1->ad = t2;
- t2->ad = t3;
- }
- cout<<"\nThe Value "<<v<<" Added."<<endl;
- }
- void ll::addend(int v)
- {
- lln *t1 = r;
- lln *t2 = new lln();
- t2->d = v;
- t2->ad = NULL;
- if (r == NULL)
- {
- t1 = r = t2;
- }
- else
- {
- while (t1->ad != NULL)
- {
- t1 = t1->ad;
- }
- t1->ad = t2;
- }
- cout<<"\nThe Value "<<v<<" Added."<<endl;
- }
- void ll::deletebeg()
- {
- lln *t = r;
- r = r->ad;
- cout<<"\nThe Value "<<t->d<<" Deleted."<<endl;
- delete t;
- }
- int ll::deletev(int x)
- {
- int dcu = 0;
- lln *t1 = NULL;
- lln *t2 = r;
- while (t2->ad != NULL)
- {
- if (t2->d != x)
- {
- t1 = t2;
- t2 = t2->ad;
- }
- else if ( (t2 == r) && (t2->d == x) )
- {
- lln *t = r;
- t1 = t2 = r = r->ad;
- delete t;
- dcu++;
- }
- else
- {
- lln *t = t2;
- t2 = t2->ad;
- t1->ad = t2;
- delete t;
- dcu++;
- }
- } if ( (t2->ad == NULL) && (t2->d == x) )
- {
- lln *t = t2;
- t1->ad = NULL;
- t2 = t1;
- delete t;
- dcu++;
- }
- return dcu;
- }
- void ll::deleteend()
- {
- lln *t = NULL;
- lln *t1 = r;
- do
- {
- if (t1->ad != NULL)
- {
- t = t1;
- t1 = t1->ad;
- }
- }
- while (t1->ad != NULL);
- lln *t2 = t1;
- t->ad = NULL;
- t1 = t;
- cout<<"\nThe Value "<<t2->d<<" Deleted."<<endl;
- delete t2;
- }
- int ll::countlist()
- {
- int cun = 0;
- if (r == NULL)
- {
- return 0;
- }
- else
- {
- lln *t = r;
- while (t->ad != NULL)
- {
- cun++;
- t = t->ad;
- }
- cun++;
- return cun;
- }void ll::showlist()
- {
- if (r == NULL)
- {
- cout<<"\nList is Empty"<<endl;
- }
- else
- {
- cout<<"\nList Values:-"<<endl;
- lln *t = r;
- while (t->ad != NULL)
- {
- cout<<t->d<<endl;
- t = t->ad;
- }
- cout<<t->d<<endl;
- }
- }
- int main()
- {
- ll *ru = new ll();
- int ch, x, v;
- do
- {
- cout<<"\n1. Add Value at Beginning."<<endl;
- cout<<"2. Add Value at a Position."<<endl;
- cout<<"3. Add Value at End."<<endl;
- cout<<"4. Delete First Value."<<endl;
- cout<<"5. Delete Particular Value."<<endl;
- cout<<"6. Delete Last Value."<<endl;
- cout<<"7. Count Number of Values."<<endl;
- cout<<"8. Show All Values."<<endl;
- cout<<"9. Exit..."<<endl;
- cout<<"Enter Operation No.: ";
- cin>>ch;
- switch(ch)
- {
- case 1:
- {
- cout<<"\nEnter a Value: ";
- cin>>v;
- ru->addbeg(v);
- break;
- }
- case 2:
- {
- cout<<"\nEnter a Value: ";
- cin>>v;
- int cu = ru->countlist();
- if (cu == 0)
- {
- ru->addbeg(v);
- }
- else
- {
- ru->showlist();
- cout<<"Enter The Value to Add After it: ";
- cin>>x;
- ru->addmiddle(x, v);
- }
- break;
- }
- case 3:
- {
- cout<<"\nEnter a Value: ";
- cin>>v;
- ru->addend(v);
- break;
- }
- case 4:
- {
- int cu = ru->countlist();
- if (cu == 0)
- {
- cout<<"\nThe List is Empty"<<endl;
- }
- else
- {
- ru->deletebeg();
- }
- break;
- }
- case 5:
- {
- int cu = ru->countlist();
- if (cu == 0)
- {
- cout<<"\nThe List is Empty"<<endl;
- }
- else
- {
- ru->showlist();
- cout<<"Enter Value to Delete: ";
- cin>>x;
- int dcu = ru->deletev(x);
- if (dcu == 0)
- {
- cout<<"\nThe Value "<<x<<" is NOT on The List."<<endl;
- }
- else
- {
- cout<<"\nThe Values "<<x<<" Deleted."<<endl;
- }
- }
- break;
- }
- case 6:
- {
- int cu = ru->countlist();
- if (cu == 0)
- {
- cout<<"\nThe List is Empty"<<endl;
- }
- else
- {
- ru->deleteend();
- }
- break;
- }
- case 7:
- {
- int cu = ru->countlist();
- if (cu==0)
- {
- cout<<"\nThe List is Empty"<<endl;
- }
- else
- {
- cout<<"\nThe List has "<<cu<<" Value."<<endl;
- }
- break;
- }
- case 8:
- {
- ru->showlist();
- break;
- }
- case 9:
- {
- cout<<"\n..."<<endl;
- break;
- }
- default:
- {
- cout<<"\nInvalid Operation No"<<endl;
- break;
- }
- }
- }
- while(ch != 9);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement