#include<iostream.h> class node { private: int data; int size; node *next; public: node() { size=1; data=0; next=NULL; } node(int d,int a) { data=d; size=a; size++; next=NULL; } void setdata(int d) { data=d; } void setsize(int s) { size=s; //size++; } int getsize() { return size; } int getdata() { return data; } void setnext(node *n) { next=n; } node *getnext() { return next; } void display() { cout<<data<<"|"<<size<<endl; } }; class nodelist { private: node *head; node *current; node *lastcurrent; public: nodelist() { head=current=lastcurrent=NULL; } void add(int d)// i want to increment size after addition of every node for eg when a node is added it size is 1 then another adds its size becomes 2 and so on { int si=1; node *temp=new node(d,si); if(head==NULL) { head=current=lastcurrent=temp; // head->setsize(si); } while(current->getnext()!=NULL) { cout<<"add loop"<<endl; lastcurrent=current; current=current->getnext(); current->getnext(); } current->setnext(temp); lastcurrent=current; current=temp; current->setnext(NULL); } void del() { int a=0; cout<<"enter the choice:"<<endl; cout<<"1:remove from tail"<<endl; cout<<"2:remove from the head"<<endl; cout<<"3:remove from user choosed node"<<endl; cin>>a; cout<<endl<<endl; switch(a) { case 1: { cout<<"the list after tail deletion is:"<<endl<<endl; node *temp=new node(); temp=current; current=lastcurrent; delete []temp; current->setnext(NULL); } break; case 2: { cout<<"the list after the deltion of data from head is:"<<endl<<endl; node *temp=new node(); temp=head; head=head->getnext(); delete []temp; } break; case 3: { node *temp=new node(); temp=head; node *temp1=new node(); node *temp2=new node(); int c=0; cout<<"enter the data from the list to delete:"<<endl<<endl; cin>>c; while(temp->getnext()!=NULL) { temp1=temp; temp=temp->getnext(); temp->getnext(); if(c==head->getdata()) { node *temp=new node(); temp=head; head=head->getnext(); delete []temp; break; } else if(c==temp->getdata()) { temp2=temp; temp=temp1; temp->setnext(temp2->getnext()); delete []temp2; break; } else if(c==current->getdata()) { node *temp=new node(); temp=current; current=lastcurrent; delete []temp; current->setnext(NULL); break; } } } } } void searching() { node *temp=new node(); temp=head; int c=0,count=1; cout<<"enter the number to search in the list:"<<endl; cin>>c; while(temp->getnext()!=NULL) { if(c==temp->getdata()) { cout<<"found "<<c<<" at node"<<count<<endl<<endl; } temp=temp->getnext(); temp->getnext(); count++; } }void display() { node *temp; temp=head; while(temp!=NULL) { temp->display(); temp=temp->getnext(); } } //void xyz(nodelist x,nodelist y) //{ //} void sort() {int t=0; for(node *temp=head;temp!=NULL;temp=temp->getnext()) { for(node *temp1=temp->getnext();temp1!=NULL;temp1=temp1->getnext()) {//cout<<"temp 1 "<<temp1->getdata()<<" temp "<<temp->getdata()<<endl; if(temp1->getdata()<temp->getdata()) {//cout<<"temp "<<temp->getdata()<<" temp 1 "<<temp1->getdata()<<endl; t=temp->getdata(); temp->setdata(temp1->getdata()); temp1->setdata(t); } } } } }; void main() { char ch=NULL; //int a=1,b=2,c=3,d=15,e=20,f=27; //int g=1,h=3,i=4,j=6; nodelist x,y;int a; //node l,p; /*x.add(a); x.add(b); x.add(c); x.add(d); x.add(e); x.add(f); x.display(); y.add(g); y.add(h); y.add(i); y.add(j);*/ while(ch!='6') { cout<<"1:To add data"<<endl; cout<<"2:To delete data from list"<<endl; cout<<"3:To Sort list in ascendin order"<<endl; cout<<"4:To display list"<<endl; cout<<"5:To search data in list"<<endl; cout<<"6:to quit"<<endl; cout<<"Enter the choice:"<<endl; cin>>ch; switch(ch) { case '1': { int size=0; cout<<"Enter the size to enter data:"<<endl; cin>>size; cout<<"enter integer data in list:"<<endl; for(int i=1;i<=size;i++) {cin>>a; x.add(a); } } break; case '2': { x.del(); x.display(); } break; case '3': {cout<<"the sorted list is:"<<endl; x.sort(); x.display(); } break; case '4': { cout<<"the data stored in list is"<<endl; x.display(); } break; case '5': { cout<<endl; x.searching(); } break; } } }
size
node
int getdata()
int getdata() const
setdata
del
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)