Sei sulla pagina 1di 5

// linked list implementation using class templates #include<stdlib.h> #include<iostream.h> #include<conio.

h> template<class t1> class Node { struct list { t1 data; struct list *link; }*head,*ptr,*temp,*r; public: void initialise(); void create(t1); void search(t1); void insert(int,t1); void del(int); void print(); }; template<class t1> void Node<t1>::initialise() { //struct list<t1> *head; head=(struct list*)malloc(sizeof(struct list)); head->data=0; head->link=NULL; } template<class t1> void Node<t1>::create(t1 num) { //struct list<t1> *temp,*ptr; temp=(struct list*)malloc(sizeof(struct list)); temp->data=num; if(head->link==NULL) { head->link=temp; temp->link=NULL; } else { for(ptr=head;ptr!=NULL;ptr=ptr->link) { if(ptr->link==NULL) { ptr->link=temp; temp->link=NULL; } } } } template<class t1> void Node<t1>::print() { //struct list<t1> *ptr; cout<<"\n The items in the list are\n";

for(ptr=head->link;ptr!=NULL;ptr=ptr->link) { cout<<ptr->data<<"\t"; } } template<class t1> void Node<t1>::del(int pos) { //struct list<t1> *temp,*ptr; int n=0; int b; if(pos==1) { temp=head->link; head->link=temp->link; } else { for(ptr=head->link;ptr!=NULL;ptr=ptr->link) { n++; b=n+1; if(b==pos) { temp=ptr->link; ptr->link=temp->link; } } } } template<class t1> void Node<t1>::search(t1 num) { //struct list<t1> *ptr; for(ptr=head->link;ptr!=NULL;ptr=ptr->link) { if(ptr->data==num) { cout<<"\n Element Found"; goto end; } } cout<<"\n Elemnt not found"; end: cout<<"\n"; } template<class t1> void Node<t1>::insert(int pos,t1 num) { //struct list<t1> *temp,*ptr,*r; int n=0,b=0; temp=(struct list*)malloc(sizeof(struct list)); temp->data=num; if(pos==1) { r=head->link;

head->link=temp; temp->link=r; } else { for(ptr=head->link;ptr!=NULL;ptr=ptr->link) { n++; b=n+1; if(b==pos) { r=ptr->link; ptr->link=temp; temp->link=r; } } } } int main() { Node<int> h; Node<float> h2; int ch1,ch2,num,pos,i,no; float n; char ans,ans2,ans3; //clrscr(); cout<<"\t\t\t Linked List implementation using class templates"; do{ cout<<"\n Enter the data type"; cout<<"\n1.Integer"; cout<<"\n2.Floating point"; cout<<"\n Enter choice"; cin>>ch1; if(ch1==1) { h.initialise(); do{ cout<<"\n Integer"; cout<<"\n Operations available:"; cout<<"\n1.Create"; cout<<"\n2.Insert"; cout<<"\n3.Delete"; cout<<"\n4.Find"; cout<<"\n5.Exit"; cout<<"\n Enter the choice"; cin>>ch2; switch(ch2) { case 1:cout<<"\n Enter the number of elemnts that you wa nt to insert"; cin>>no; cout<<"\n Enter the number"; for(i=0;i<no;i++) { cin>>num; h.create(num); } h.print(); break;

case 2: int p,n1; cout<<"\n Enter the number and the position at w hich you want to insert"; cin>>n1>>p; h.insert(p,n1); h.print(); break; case 3:cout<<"\n Enter the position of the elemnt to be deleted"; cin>>pos; h.del(pos); h.print(); break; case 4: int y; cout<<"\n Enter the number to be searched"; cin>>num; h.search(num); /*if(y==1) cout<<"\n Element found"; else cout<<"\n Not found";*/ break; case 5:break; } cout<<"\n Do you want to continue with integer operations(y/n)"; cin>>ans; }while(ans=='y'); } else { h2.initialise(); do{ cout<<"\n Floating point"; cout<<"\n Operations available:"; cout<<"\n1.Create"; cout<<"\n2.Insert"; cout<<"\n3.Delete"; cout<<"\n4.Search"; cout<<"\n4.Exit"; cout<<"\nEnter the choice"; cin>>ch2; switch(ch2) { case 1:cout<<"\n Enter the number of elemnts that you wa nt to insert"; cin>>no; cout<<"\n Enter the number"; for(i=0;i<no;i++) { cin>>n; h2.create(n); } h2.print(); break; case 2: float nm; cout<<"\n Enter the number and the position at w hich it has to be inserted";

cin>>nm; int p; cin>>p; h2.insert(p,nm); h2.print(); break; case 3:cout<<"\n Enter the position of the elemnt to be deleted"; cin>>pos; h2.del(pos); h2.print(); break; case 4: int y; cout<<"\n Enter the number to be searched"; cin>>nm; h2.search(nm); /*if(y==1) cout<<"\n Element found"; else cout<<"\n Not found";*/ break; case 5:break; } cout<<"\n Do you want to continue with floating point operations(y/n)"; cin>>ans2; }while(ans2=='y'); } cout<<"\n Do you want to continue with integer and floating point operat ions(y/n)"; cin>>ans3; }while(ans3=='y'); getch(); }

Potrebbero piacerti anche