Sei sulla pagina 1di 15

1. a. Program for virtual function in C+ + #include <iostream.

h> class Base { Public: Void display() { cout<<\n Display base; } Virtual void show() { Cout<<\n show base; } }; Class Derive : public Base { Public: Void display() { Cout<<\n Display derive; } void show() { cout<<\n show derived; ] }; Int main() { Base B; Derived D; Base *bptr; cout<<\n bptr points to Base \n; bptr=&B; bptr->display(); bptr->show(); Cout<<\n\n bptr points to Derived\n; bptr=&D; bptr->display(); bptr->show(); Return 0; } 1. b. Program for Templates #include<iostream.h> template <class T> class myClass {

T a,b; public: myClass(T first,T second) { a=first; b=second; } T getmax(); }; template <class T> T myClass<T>::gemax() { T retval; retval=a>b?a:b; return retval; } int main() { myClass <int>myObject(100,75); cout<<myObject.getmax(); return 0; } 2. Program for array implementation of list ADT. #include<iostream.h> #include<conio.h> #include<process.h> void create(); void insert(); void deletion(); void search(); void display(); int a,b[20],n,d,e,f,i; void main() { int c; char g='y'; clrscr(); do { cout<<"\n Main Menu"; cout<<"\n 1.Create \n 2.Delete \n 3.Search \n 4.insert \n 5.Display \n 6.Exit"; cout<<"\n enter your choice\n"; cin>>c; switch(c)
1

{ case 1: create(); break; case 2: deletion(); break; case 3: search(); break; case 4: insert(); break; case 5: display(); break; case 6: exit(0); break; default: cout<<"The given number is not between 1-5\n"; } cout<<"\nDo u want to continue \n"; cin>>g; clrscr(); } while(g=='y'|| g=='Y'); getch(); } void create() { cout<<"\n Enter the limit\n"; cin>>n; for(i=0;i<n;i++) { cin>>b[i]; }} void deletion() { cout<<"Enter the number u want to delete \n"; cin>>d; for(i=0;i<n;i++) { if(b[i]==d) { b[i]=0; }}} void search() { cout<<"Enter the number to search \n"; cin>>e; for(i=0;i<n;i++) { if(b[i]==e) { cout<<"Value found the position\n"<<b[i]; }}} void insert() {

cout<<"enter how many number u want to insert \n"; cin>>f; for(i=0;i<f;i++) { cin>>b[n++]; }} void display() { cout<<"\n\n\n"; for(i=0;i<n;i++) { cout<<"\n\n\n"<<b[i]; }} 3. Program for linked list implementation of list ADT. #include<iostream.h> #include<conio.h> #include<stdlib.h> class list { struct node { int data; node *link; }*p; public: void inslast(int); void insbeg(int); void insnext(int,int); void delelement(int); void delbeg(); void dellast(); void disp(); int seek(int); list(){p=NULL;} ~list(); }; void list::inslast(int x) { node *q,*t; if(p==NULL) { p=new node; p->data=x; p->link=NULL; } else {
2

q=p; while(q->link!=NULL) q=q->link; t=new node; t->data=x; t->link=NULL; q->link=t; } cout<<"\n\nInserted successfully at the end.."; disp(); } void list:: insbeg(int x) { node *q; q=p; p=new node; p->data=x; p->link=q; cout<<"\n\nInserted successfully at the begining.."; disp(); } void list::delelement(int x) { node *q,*r; q=p; if(q->data==x) { p=q->link; delete q; return; } r=q; while(q!=NULL) { if(q->data==x) { r->link=q->link; delete q; return; } r=q; q=q->link; } cout<<"\n\nElement you entered "<<x<<" is not found.."; } void list:: delbeg()

{ cout<<"\n\nThe list before deletion:"; disp(); node *q; q=p; if(q==NULL) { cout<<"\n\nNo data is present.."; return; } p=q->link; delete q; return; } void list:: dellast() { cout<<"\n\nThe list before deletion:"; disp(); node *q,*t; q=p; if(q==NULL) { cout<<"\n\nThere is no data in the list.."; return; } if(q->link==NULL) { p=q->link; delete q; return; } while(q->link->link!=NULL) q=q->link; q->link=NULL; return; } list::~list() { node *q; if(p==NULL) return; while(p!=NULL) { q=p->link; delete p; p=q; } } void list::disp()
3

{ node *q; q=p; if(q==NULL) { cout<<"\n\nNo data is in the list.."; return; } cout<<"\n\nThe items present in the list are \n"; while(q!=NULL) { cout<<q->data<<" -> "; q=q->link; } cout<<"NULL"; } void list :: insnext(int value,int position) { node *temp,*temp1; temp=p; if(temp1==NULL) { temp1= new node; temp1->data=value; temp1->link=NULL; p=temp1; return; } for(int i=1;((i<=position)&&(temp>link!=NULL)) ;i++) { if(i==(position-1)) { temp1= new node; temp1->data= value; temp1->link=temp->link; temp->link=temp1; } temp=temp->link; } cout<<"\n\nInserted successfully at position "<<position; disp(); } int list::seek(int value) { node *temp; temp=p; int position=0;

while(temp!=NULL) { if(temp->data==value) return position+1; else { temp=temp->link; position=position+1; } } cout<<"\n\nElement "<<value<<" not found"; return 0; } void main() { list l; int ch,v,p,ps; do { clrscr(); cout<<"\n\nOperations on List.."; cout<<"\n\n1.Insertion\n2.Deletion\n3.D isplay\n4.Seek\n5.Exit"; cout<<"\n\nEnter ur Option :"; cin>>ch; switch(ch) { case 1: clrscr(); cout<<"INSERTION"; cout<<"\n\n1.Insertion at begining\n2.Insertion at the end"; cout<<"\n3.Insertion between two Nodes"; cout<<"\n\nEnter ur choice:"; cin>>ps; cout<<"Enter the value to insert:"; cin>>v; switch(ps) { case 1: l.insbeg(v); break; case 2: l.inslast(v); break; case 3: cout<<"\nEnter the position to insert the value:";
4

cin>>p; l.insnext(v,p); break; default: cout<<"\nThe choice is invalid"; return; } break; case 2: clrscr(); cout<<"\n1.Delete the first element\n2.Delete the last element"; cout<<"\n3.Enter the element to delete from the list"; cout<<"\n\nEnter ur choice:"; cin>>ps; switch(ps) { case 1: l.delbeg(); cout<<"\nThe list after deletion:"; l.disp(); break; case 2: l.dellast(); cout<<"\nThe list after deletion:"; l.disp(); break; case 3: l.disp(); cout<<"\nEnter the element to delete : "; cin>>v; l.delelement(v); cout<<"\nThe list after deletion:"; l.disp(); break; default: cout<<"\nThe option is invalid..."; break; } break; case 3: clrscr(); l.disp(); break; case 4: clrscr(); l.disp(); cout<<"\nEnter the element to search:";

cin>>v; cout<<"\nThe position of the element "<< v<<" is "<<l.seek(v); getch(); break; case 5: exit(1); default: cout<<"\nThe option is invalid..."; return; } getch(); }while(ch!=5); getch(); return; } 4.Write a C++ Program for Cursor implementation of list ADT. #include<iostream.h> #include<conio.h> #include<process.h> #define max 5 struct node { int data; int next; }; typedef struct node NODE; int avail,list = -1; NODE curs[max]; void initial() { int i; avail = 0; for(i=0;i<max-1;i++) curs[i].next=i+1; curs[i].next=-1; } void create() { int n,i,item,temp; cout<<"\nEnter the no of elements: "; cin>>n; //cout<<n; if(n>=max) cout<<"\n Size exists"; else {
5

initial(); if(avail==-1) { cout<<"\nThere is no space to insert"; exit(0); } list = avail; if(n==max-1) avail = -1; else avail=n+1; cout<<"\nEnter the elements one by one: "; for(i=0;i<n;i++) { cout<<"\n Enter the "<<i+1<<"th element "; cin>>item; cout<<"\t"; //cout<<item; curs[i+1].data=item; } curs[n].next=-1; } } void display() { int i; cout<<"\nCursor space: "; cout<<"\n\nAvail = "<<avail<<" \t "; cout<<"List = "<<list<<"\n"; cout<<"_______________"; cout<<"\n DATA NEXT \n"; cout<<"_______________"; i=0; while(i<max) { cout<<"\n"<<curs[i].data<<" \t "<<curs[i].next; cout<<"\n_______________"; i++; } } void insbeg() { int item,i; cout<<"\nEnter the item to be inserted: "; cin>>item; //cout<<item; cout<<"\n"; i=avail;

avail=curs[avail].next; curs[i].data=item; curs[i].next=curs[list].next; curs[list].next=i; } void insend() { int item,i; cout<<"\nEnter the item to be inserted: "; cin>>item; //cout<<item; cout<<endl; i=list; while(curs[i].next!=-1) i=curs[i].next; curs[i].next=avail; avail=curs[avail].next; i=curs[i].next; curs[i].data=item; curs[i].next=-1; } void insint() { int item,i,pos,count,temp; cout<<"\nEnter the item to be inserted: "; cin>>item; //cout<<item; cout<<endl; cout<<"\nEnter the position of the item: "; cin>>pos; //cout<<pos; cout<<endl; i=list; count=1; while(count<pos) { i=curs[i].next; count=count+1; } temp=avail; avail=curs[avail].next; curs[temp].data=item; curs[temp].next=curs[i].next; curs[i].next=temp; } void delbeg() {
6

int i; i=curs[list].next; curs[list].next=curs[i].next; curs[i].next=avail; avail=i; curs[avail].data=0; if(curs[list].next==-1) { curs[list].next=avail; avail=list; list=-1; } } void delend() { int i,prev; i=list; while(curs[i].next!=-1) { prev=i; i=curs[i].next; } curs[prev].next=-1; curs[i].next=avail; avail=i; curs[avail].data=0; if(curs[list].next==-1) { curs[list].next=avail; avail=list; list=-1; } } void delint() { int pos,i,count,prev; cout<<"\nEnter the position: "; cin>>pos; //cout<<pos; cout<<endl; i=list; count=1; while(count<=pos) { prev=i; i=curs[i].next; count=count+1; } curs[prev].next=curs[i].next; curs[i].next=avail;

avail=i; curs[avail].data=0; if(curs[list].next==-1) { curs[list].next=avail; avail=list; list=-1; } } void main() { int ch; clrscr(); do { cout<<"\n ********************************** *************************"; cout<<"\n\t\t\t\t LIST\n"; cout<<" ********************************** *************************"; cout<<"\n1. Create"; cout<<"\n2. Insert at begin"; cout<<"\n3. Insert at end"; cout<<"\n4. Insert at intermediate"; cout<<"\n5. Delete at begin"; cout<<"\n6. Delete at end"; cout<<"\n7. Delete at intermediate"; cout<<"\n8. Display"; cout<<"\n9. Exit"; cout<<"\n\nEnter your choice:"; cin>>ch; //cout<<"\n"<<ch; switch(ch) { case 1: create(); break; case 2: if(avail==-1) cout<<"\nThere is no space"; else insbeg(); break; case 3: if(avail==-1) cout<<"\nThere is no space"; else insend(); break; case 4:
7

if(avail==-1) cout<<"\nThere is no space"; else insint(); break; case 5: if(list==-1) cout<<"\nNo element to delete"; else delbeg(); break; case 6: if(list==-1) cout<<"\nNo element to delete"; else delend(); break; case 7: if(list==-1) cout<<"\nNo element to delete"; else delint(); break; case 8: display(); break; case 9: cout<<"\nEnd of the operation"; break; default: cout<<"\nEnter only 1 to 9: "; } }while(ch!=9); } 5. C++ Program to Stack ADT implementation using array #include<iostream.h> #include<conio.h> #include<stdlib.h> class stack { int stk[5]; int top; public: stack() { top=-1; } void push(int x) {

if(top > 4) { cout <<"stack over flow"; return; } stk[++top]=x; cout <<"inserted" <<x; } void pop() { if(top <0) { cout <<"stack under flow"; return; } cout <<"deleted" <<stk[top--]; } void display() { if(top<0) { cout <<" stack empty"; return; } for(int i=top;i>=0;i--) cout <<stk[i] <<" "; } }; main() { Clrscr(); int ch; stack st; while(1) { cout <<"\n1.push 2.pop 3.display 4.exit\nEnter ur choice"; cin >> ch; switch(ch) { case 1: cout <<"enter the element"; cin >> ch; st.push(ch); break; case 2: st.pop(); break; case 3: st.display();break; case 4: exit(0); }
8

} return (0); } 6. C++ Program to Stack ADT implementation using linked list #include<iostream.h> #include<conio.h> #include<stdlib.h> class node { public: class node *next; int data; }; class stack : public node { node *head; int tos; public: stack() { tos=-1; } void push(int x) { if (tos < 0 ) { head =new node; head->next=NULL; head->data=x; tos ++; } else { node *temp,*temp1; temp=head; if(tos >= 4) { cout <<"stack over flow"; return; } tos++; while(temp->next != NULL) temp=temp->next; temp1=new node; temp->next=temp1;

temp1->next=NULL; temp1->data=x; } } void display() { node *temp; temp=head; if (tos < 0) { cout <<" stack under flow"; return; } while(temp != NULL) { cout <<temp->data<< " "; temp=temp->next; } } void pop() { node *temp; temp=head; if( tos < 0 ) { cout <<"stack under flow"; return; } tos--; while(temp->next->next!=NULL) { temp=temp->next; } temp->next=NULL; } }; main() { Clrscr(); stack s1; int ch; while(1) { cout <<"\n1.PUSH\n2.POP\n3.DISPLAY\n4. EXIT\n enter ur choice:"; cin >> ch; switch(ch) {
9

case 1: cout <<"\n enter a element"; cin >> ch; s1.push(ch); break; case 2: s1.pop();break; case 3: s1.display(); break; case 4: exit(0); } } return (0); } 7. C++ Program to Queue ADT implementation using array #include<iostream.h> #include<conio.h> #include<stdlib.h> class queue { int queue1[5]; int rear,front; public: queue() { rear=-1; front=-1; } void insert(int x) { if(rear > 4) { cout <<"queue over flow"; front=rear=-1; return; } queue1[++rear]=x; cout <<"inserted" <<x; } void delet() { if(front==rear) { cout <<"queue under flow"; return; } cout <<"deleted" <<queue1[++front]; }

void display() { if(rear==front) { cout <<" queue empty"; return; } for(int i=front+1;i<=rear;i++) cout <<queue1[i]<<" "; } }; main() { Clrscr(); int ch; queue qu; while(1) { cout <<"\n1.insert 2.delet 3.display 4.exit\nEnter ur choice"; cin >> ch; switch(ch) { case 1: cout <<"enter the element"; cin >> ch; qu.insert(ch); break; case 2: qu.delet(); break; case 3: qu.display();break; case 4: exit(0); } } return (0); } 8. C++ Program to Queue ADT implementation using linked list #include<iostream.h> #include<conio.h> #include<stdlib.h> class node { public: class node *next; int data; }; class queue : public node { node *head;
10

int front,rare; public: queue() { front=-1; rare=-1; } void push(int x) { if (rare < 0 ) { head =new node; head->next=NULL; head->data=x; rare ++; } else { node *temp,*temp1; temp=head; if(rare >= 4) { cout <<"queue over flow"; return; } rare++; while(temp->next != NULL) temp=temp->next; temp1=new node; temp->next=temp1; temp1->next=NULL; temp1->data=x; }} void display() { node *temp; temp=head; if (rare < 0) { cout <<" queue under flow"; return; } while(temp != NULL) { cout <<temp->data<< " "; temp=temp->next; } } void pop()

{ node *temp; temp=head; if( rare < 0) { cout <<"queue under flow"; return; } if(front == rare) { front = rare =-1; head=NULL; return; } front++; head=head->next; } }; main() { Clrscr(); queue s1; int ch; while(1) { cout <<"\n1.PUSH\n2.POP\n3.DISPLAY\n4. EXIT\n enter ru choice:"; cin >> ch; switch(ch) { case 1: cout <<"\n enter a element"; cin >> ch; s1.push(ch); break; case 2: s1.pop();break; case 3: s1.display();break; case 4: exit(0); } } return (0); } 9. C++ Program to to perform Insert, Delete, Search an element into a binary search tree #include<iostream.h> #include<conio.h>
11

#include<stdlib.h> void insert(int,int ); void delte(int); void display(int); int search(int); int search1(int,int); int tree[40],t=1,s,x,i; main() { clrscr(); int ch,y; for(i=1;i<40;i++) tree[i]=-1; while(1) { cout <<"1.INSERT\n2.DELETE\n3.DISPLA Y\n4.SEARCH\n5.EXIT\nEnter your choice:"; cin >> ch; switch(ch) { case 1: cout <<"enter the element to insert"; cin >> ch; insert(1,ch); break; case 2: cout <<"enter the element to delete"; cin >>x; y=search(1); if(y!=-1) delte(y); else cout<<"no such element in tree"; break; case 3: display(1); cout<<"\n"; for(int i=0;i<=32;i++) cout <<i; cout <<"\n"; break; case 4: cout <<"enter the element to search:"; cin >> x; y=search(1); if(y == -1) cout <<"no such element in tree"; else cout <<x << "is in" <<y <<"position"; break;

case 5: exit(0); } } } void insert(int s,int ch ) { int x; if(t==1) { tree[t++]=ch; return; } x=search1(s,ch); if(tree[x]>ch) tree[2*x]=ch; else tree[2*x+1]=ch; t++; } void delte(int x) { if( tree[2*x]==-1 && tree[2*x+1]==-1) tree[x]=-1; else if(tree[2*x]==-1) { tree[x]=tree[2*x+1]; tree[2*x+1]=-1; } else if(tree[2*x+1]==-1) { tree[x]=tree[2*x]; tree[2*x]=-1; } else { tree[x]=tree[2*x]; delte(2*x); } t--; } int search(int s) { if(t==1) { cout <<"no element in tree"; return -1; } if(tree[s]==-1) return tree[s]; if(tree[s]>x)
12

search(2*s); else if(tree[s]<x) search(2*s+1); else return s; } void display(int s) { if(t==1) {cout <<"no element in tree:"; return;} for(int i=1;i<40;i++) if(tree[i]==-1) cout <<" "; else cout <<tree[i]; return ; } int search1(int s,int ch) { if(t==1) { cout <<"no element in tree"; return -1; } if(tree[s]==-1) return s/2; if(tree[s] > ch) search1(2*s,ch); else search1(2*s+1,ch); } 10. Write a C++ Program to perform heap sort. #include<iostream.h> #include<stdlib.h> #include<conio.h> class sorting { private: int n,size; double *minheap; public: void insert_minheap(double); double delete_one_minheap(); void input(); void output(); }; void sorting::insert_minheap(double n)

{ if(size>=9) { cout<<array overflow ; exit(0); } minheap[++size]=n; //Reorder the heap int k=size; while(k>1) //k has a parent { if(minheap[k]<minheap[k/2]) { double t=minheap[k]; minheap[k]=minheap[k/2]; minheap[k/2]=t; k/=2; } else break; } } double sorting::delete_one_minheap() { if(size<1) return -1; double val; val=minheap[1]; minheap[1]=minheap[size]; size; //Reorder the heap by moving down int k=1; int newk; while(2*k<=size) //k has atleast one chaild { //Set newk to the index of the smallest chaild of k if(2*k==size) //if k has only left chaid { newk=2*k; } else //k has two chailds { if(minheap[2*k]<minheap[2*k+1]) newk=2*k; else newk=2*k+1; }
13

if(minheap[k]<minheap[newk]) break; else { double t; t=minheap[k]; minheap[k]=minheap[newk]; minheap[newk]=t; k=newk; } } return val; } void sorting::input() { cout<<Enter how many numbers you are going to enter for sorting :; cin>>n; minheap=new double[n+1]; /********** Construct a heap with the input elements *******/ size=0; cout<<Now enter the elements\n; double number; for(int i=1;i<=n;i++) { cin>>number; insert_minheap(number); } } void sorting::output() { cout<<The sorted numbers are ::\n; for(int i=1;i<=n;i++) cout<<delete_one_minheap()<<\t; cout<<endl; } int main() { clrscr(); sorting obj; obj.input(); obj.output(); getch(); return 0; } 11. C++ Program to perform quick sort.

#include<iostream.h> #include<conio.h> class QuiSort { int i,j,pivot; public: int n,a[20]; void quick(int a[],int left,int right); void swap(int a[],int i,int j); }; void QuiSort :: quick(int a[],int first,int last) { if(first<last) { pivot=a[first]; i=first; j=last; while(i<j) { while(a[i]<=pivot&&i<last) i++; while(a[j]>=pivot&&j>first) j--; if(i<j) swap(a,i,j); } swap(a,first,j); quick(a,first,j-1); quick(a,j+1,last); } } void QuiSort :: swap(int a[],int i,int j) { int temp; temp=a[i]; a[i]=a[j]; a[j]=temp; } void main() { QuiSort obj; clrscr(); cout<<"\n\nQUICK SORT"; cout<<"\n\nEnter the limit : "; cin>>obj.n; clrscr(); cout<<"\n\nEnter the element\n\n"; for(int i=0;i<obj.n;i++)
14

cin>>obj.a[i]; obj.quick(obj.a,0,obj.n-1); cout<<"\n\nThe sorted list is \n\n"; for(i=0;i<obj.n;i++) cout<<obj.a[i]<<" "; getch(); }

15

Potrebbero piacerti anche