Sei sulla pagina 1di 38

FUNCTION OVERLOADING

PROGRAM:

#include<iostream.h> #include<conio.h> void add(int,int); void add(float,float); void add(char,char); void main() { int a=10,b=20; float c=123,d=1.23; char e='0',f='2'; clrscr(); add (a,b); add (c,d); add (e,f); getch(); } void add(int a,int b) { int z; z=a+b; cout<<"\n The result of integer addition is"<<z; } void add(float a,float b) { float z; z=a+b; cout<<"\n The result of float addition is ="<<z; } void add(char a,char b) { char z; z=a+b; cout<<"\n The result of char addition is" =<<z; };
OUTPUT:

The result of integer addition is = 30 The result of float addition is = 124.23003 The result of char addition is b

CONSTRUCTOR PROGRAM:

#include<iostream.h> #include<conio.h> class integer { int m,n; public: integer (int,int); void display(void) { cout<<"m="<<m<<"\n"; cout<<"n="<<n<<"\n"; } }; integer::integer(int x,int y) { m=x;n=y; } int main() { clrscr(); integer int1(0,100); integer int2=integer(25,75); cout<<"\nOBJECT1"<<"\n"; int1.display(); cout<<"\nOBJECT2"<<"\n"; int2.display(); getch(); return 0; }
OUTPUT:

OBJECT 1 m=0 n=100 OBJECT 2 m=25 n=75

INLINE FUNCTION

PROGRAM:

#include<iostream.h> #include<conio.h> inline float mul(float x,float y) { return(x*y); } inline double div(float p,float q) { return(p/q); } main() { float a,b; clrscr(); cout<<"Enter the value of a and b;"; cin>>a>>b; cout<<"Result of multiplication operation="<<mul(a,b)<<endl; cout<<"Result of division operation="<<div(a,b)<<endl; getch(); return 0; }

OUTPUT:

Enter the value of a and b: 4 5 Result of multiplication operation = 20 Result of division operation = 0.8

INHERITANCE

PROGRAM:

#include<iostream.h> #include<conio.h> class data { public: char name[10]; int sno; void getno(void); }; class mark { public: int m1,m2,m3,m4,m5; void getmark(void); }; class result : public data,public mark { public: int avg,total; void getresult(void); }; void data::getno(void) { cout<<"enter the name: "; cin>>name; cout<<"enter the roll no: "; cin>>sno; } void mark::getmark(void) { cout<<"enter the 5 subjects mark: \n"; cin>>m1>>m2>>m3>>m4>>m5; } void result :: getresult(void) { total=m1+m2+m3+m4+m5; avg=total/5; cout<<"\n\n************RESULT****************\n";

cout<<"name="<<name<<"\n "; cout<<"roll no="<<sno<<"\n"; cout<<"total="<<total<<"\n"; cout<<"average="<<avg<<"\n"; } main() { result f1; clrscr(); f1.getno(); f1.getmark(); f1.getresult(); getch(); return 0; }
OUTPUT:

Enter the name: cbi.saravanan Enter the roll no:6040 Enter the 5 subjects marks: 80 87 90 95 99 ***************RESULT*********** Name:cbi.saravanan Roll no:6040 Total:451 Average:90

VIRTUAL FUNCTION

PROGRAM:

#include<iostream.h> #include<conio.h> class graphics { public: virtual void draw() { cout<<"point"<<endl; } }; class line:public graphics { public: void draw() { cout<<"line"<<endl; } }; class triangle:public graphics { public: void draw() { cout<<"triangle"<<endl; } }; class rectangle:public graphics { public: void draw() { cout<<"rectangle"<<endl; } }; class circle:public graphics { public: void draw() { cout<<"circle"<<endl; }

}; int main() { clrscr(); graphics point_obj; line line_obj; triangle tri_obj; rectangle rect_obj; circle circle_obj; graphics*basep[]= { &point_obj,&line_obj,&tri_obj,&rect_obj,&circle_obj }; for(int i=0;i<5;i++) { basep[i]->draw(); } getch(); return(0); }

OUTPUT:

point line triangle rectangle circle

STACK USING ARRAY

PROGRAM:

#include<iostream.h> #include<conio.h> #define MAX 10 class stack { private: int arr[MAX]; int top; public: stack() { top=-1; } void push(int a) { top++; if(top<MAX) { arr[top]=a; } else { cout<<"STACK IS FULL!!"<<endl; top--; } } int pop() { if(top==-1) { cout<<"STACK IS EMPTY!!!"<<endl; return NULL; } else { int data=arr[top]; arr[top]=NULL;

top--; return data; } } }; int main() { clrscr(); stack a; a.push(3); cout<<"3 is pushed\n"; a.push(10); cout<<"10 is pushed\n"; a.push(1); cout<<"1 is pushed\n"; cout<<a.pop()<<" is popped\n"; cout<<a.pop()<<" is popped\n"; cout<<a.pop()<<" is popped\n"; getch(); return 0; }
OUTPUT:

3 is pushed 10 is pushed 1 is pushed 1 is popped 10 is popped 3 is popped

QUEUE ADT USING ARRAY

PROGRAM:

#include<iostream.h> #include<conio.h> #define MAX 5 class queue { private: int t[MAX]; int al; int dl; public: queue() { dl=-1; al=-1; } void del() { int tmp; if(dl==-1) { cout<<"queue is empty"; } else { for(int j=0;j<=al;j++) { if((j+1)<=al) { tmp=t[j+1]; t[j]=tmp; } else { al--; if(al==-1) dl=-1; else

10

dl=0; } } } } void add(int item) { if(dl==-1&&al==-1) { dl++; al++; } else { al++; if(al==MAX) { cout<<"queue is full\n"; al--; return; } } t[al]=item; } void display() { if(dl!=-1) { for(int iter=0;iter<=al;iter++) cout<<t[iter]<<" "; } else cout<<"EMPTY"; } }; int main() { clrscr(); queue a; int data[5]={32,23,45,99,24}; cout<<"queue before adding element"; a.display(); cout<<endl<<endl;

11

for(int iter=0;iter<5;iter++) { a.add(data[iter]); cout<<"Addition number:"<<(iter+1)<<":"; a.display(); cout<<endl; } cout<<endl<<endl; for(iter=0;iter<5;iter++) { a.del(); cout<<"Deletion number:"<<(iter+1)<<" : "; a.display(); cout<<endl; } getch(); return 0; }

OUTPUT:

Queue before addition element EMPTY Addition number :1:32 Addition number :2:32 23 Addition number :3:32 23 45 Addition number :4:32 23 45 99 Addition number :5:32 23 45 99 24 Queue After addition element 33 23 45 99 24 Deletion number:1:23 45 99 24 Deletion number:2:45 99 24 Deletion number:3:99 24 Deletion number:4:24

12

QUEUE ADT USING LINKED LIST PROGRAM:

#include<iostream.h> #include<conio.h> struct node { int data; node*link; }; class lqueue { private: node*front,*rear; public: lqueue() { front=NULL; rear=NULL; } void add(int n) { node*tmp; tmp=new node; if(tmp==NULL) cout<<"\n queue full"; tmp->data=n; tmp-> link=NULL; if(front==NULL) { rear=front=tmp; return; } rear->link=tmp; rear=rear->link; } int del() { if(front==NULL) {cout<<"\n queue empty"; return NULL; }

13

node*tmp; int n; n=front->data; tmp=front; front=front->link; delete tmp; return n; } ~lqueue() { if(front==NULL) return; node*tmp; while(front!=NULL) { tmp=front; front=front->link; delete tmp; } } }; int main() { clrscr(); lqueue q; q.add(11); q.add(22); q.add(33); q.add(44); q.add(55); cout<<"\n item deleted ="<<q.del(); cout<<"\n item deleted ="<<q.del(); cout<<"\n item deleted ="<<q.del(); getch(); return 0; }
OUTPUT:

Item deleted =11 Item deleted =22 Item deleted =33

14

STACK ADT USING LINKED LIST PROGRAM:

#include<iostream.h> #include<conio.h> struct node { int data; node*link; }; class lstack { private: node*top; public: lstack() { top=NULL; } void push(int n) { node*tmp; tmp=new node; if(tmp==NULL) cout<<"\n STACK FULL"; tmp->data=n; tmp->link=top; top=tmp; } int pop() { if(top==NULL) { cout<<"\nSTACK EMPTY"; return NULL; } node*tmp; int n; tmp=top; n=tmp->data; top=top->link;

15

delete tmp; return n; } ~lstack() { if(top==NULL) return; node*tmp; while(top!=NULL) { tmp=top; top=top->link; delete tmp; } } }; int main() { clrscr(); lstack s; s.push(11); s.push(101); s.push(99); s.push(78); cout<<"Item popped="<<s.pop()<<endl; cout<<"Item popped="<<s.pop()<<endl; cout<<"Item popped="<<s.pop()<<endl; cout<<"Item popped="<<s.pop()<<endl; getch(); return 0; }
OUTPUT:

Item popped=78 Item popped=99 Item popped=101 Item popped=11

16

LINKED LIST PROGRAM:

#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 { q=p; while(q->link!=NULL) q=q->link; t=new node; t->data=x;

17

t->link=NULL; q->link=t; } cout<<"inserted successfully at the end..\n"; disp(); } void list::insbeg(int x) { node *q; q=p; p=new node; p->data=x; p->link=q; cout<<"inserted successfully at the begining..\n"; 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<<"element you entered"<<x<<"is not found..\n"; } void list::delbeg() { cout<<"the list before deletion:\n";

18

disp(); node *q; q=p; if(q==NULL) { cout<<"no data is present...\n"; return; } p=q->link; delete q; return; } void list::dellast() { cout<<"the list before deletion:\n"; disp(); node *q,*t; q=p; if(q==NULL) { cout<<"there is no data in the list...\n"; 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;

19

} void list::disp() { node *q; q=p; if(q==NULL) { cout<<"NO DATA IS IN THE LIST\n"; return; } cout<<"THE ITEMS PRESENT IN THE LIST ARE\n"; while(q!=NULL) { cout<<" " <<q->data; q=q->link; } } 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=0;((i<position)&&(temp->link!=NULL));i++) { if(i==(position-1)) { temp1=new node; temp1->data=value; temp->link=temp->link; temp1->link=temp1; } temp=temp->link; } cout<<"inserted successfully at the position.."<<position; disp(); }

20

int list::seek(int value) { node *temp; temp=p; int position=0; while(temp!=NULL) { if(temp->data==value) return position+1; cout<<"element"<<value<<"not found\n"; } return 0; } void main() { list l; int ch,v,p,ps; do { clrscr(); cout<<"operation on list..\n"; cout<<"1.inseration\n2.deletion\n3.display\n4.seek\n5.exit\n"; cout<<"enter ur choice:\n"; cin>>ch; switch(ch) { case 1: cout<<"1.inseration at begining\n 2.insertion at the end\n"; cout<<"3.insertion after the mentioned position\n"; cout<<"enter ur choice:\n"; cin>>ps; cout<<"enter the value to insert:\n"; cin>>v; switch(ps) { case 1: l.insbeg(v); break; case 2: l.inslast(v); break; case 3: cout<<"enter the position to insert the value:\n";

21

cin>>p; l.insnext(v,p); break; default: cout<<"the choice is invalid\n"; return; } break; case 2: cout<<"1.delete the first element\n2.delete the last element\n"; cout<<"3.enter the element to delete from the list\n"; cout<<"enter ur choice:\n"; cin>>ps; switch(ps) { case2: l.delbeg(); cout<<"the list after deletion:\n"; l.disp(); break; case 2: l.dellast(); cout<<"the list after deletion:\n"; l.disp(); break; case 3: l.disp(); cout<<"enter the element to delete:\n"; cin>>v; l.delelement(v); cout<<"the list after deletion:\n"; break; } break; case 3: l.disp(); break; case 4: l.disp(); cout<<"enter the element to search:\n"; cin>>v; cout<<"\n the position of the element"<<v<<"is"<<l.seek(v); getch();

22

break; case 5: exit(1); default: cout<<"the option is invalid...\n"; return; } getch(); }while(ch!=5); getch(); return; }

OUTPUT:

operation on list.. 1.Insertion 2.Deletion 3.Display 4.Seek 5.Exit Enter ur choice: 1.Insertion at begining 2.Insertion at the end 3.Insertion after the mentioned position Enter ur choice: 2 Enter the value to insert: 12 Inserted successfully at the end.. The items present in the list are 12

23

CURSOR

PROGRAM:

#include<iostream.h> #include<conio.h> struct node { char name[20]; int age; float height; node*nxt; }; node*start_ptr=NULL; node*current; int option=0; void add_node_at_end() {node*temp,*temp2; temp=new node; cout<<"\n Please enter the name of the person:"; cin>>temp->name; cout<<"\n Please enter the age of the person:"; cin>>temp->age; cout<<"\n Please enter the height of the person:"; cin>>temp->height; temp->nxt=NULL; if(start_ptr==NULL) { start_ptr=temp; current=start_ptr; } else { temp2=start_ptr; while(temp2->nxt!=NULL) { temp2=temp2->nxt; } temp2->nxt=temp; } }

24

void display_list() { node*temp; temp=start_ptr; if(temp==NULL) cout<<"\n The list is empty!"; else { while(temp!=NULL) { cout<<"\n name:"<<temp->name<<" "; cout<<"\n age:"<<temp->age<<" "; cout<<"\nheight:"<<temp->height; if(temp==current) cout<<"\n <-current node"; temp=temp->nxt; } cout<<"\n end of the list!"; } } void delete_start_node() { node*temp; temp=start_ptr; start_ptr=start_ptr->nxt; delete temp; } void delete_end_node()

{
node*temp1,*temp2; if(start_ptr==NULL) cout<<"\n the list is empty!"; else { temp1=start_ptr; if(temp1->nxt==NULL) { delete temp1; start_ptr=NULL; } else

25

{ while(temp1->nxt!=NULL) { temp2=temp1; temp1=temp1->nxt; } delete temp1; temp2->nxt=NULL; } } } void move_current_on() { if(current->nxt==NULL) cout<<"\n you are at the end of the list."; else current=current->nxt; } void move_current_back() { if(current==start_ptr) cout<<"\n you are at the start of the list"; else { node*previous; previous=start_ptr; while(previous->nxt!=current) { previous=previous->nxt; } current=previous; } } void main() { clrscr(); start_ptr=NULL; do { display_list(); cout<<"\n Please select an option:"; cout<<"\n 0.Exit the program."; cout<<"\n 1.Add a node to the end of the list.";

26

cout<<"\n 2.Delete the start node from the list."; cout<<"\n 3.Delete the end node from the list."; cout<<"\n 4.Move the current pointer on one node."; cout<<"\n 5.Move the current pointer back one node. \n"; cin>>option; switch(option) { case 1: add_node_at_end(); break; case 2: delete_start_node(); break; case 3: delete_end_node(); break; case 4: move_current_on(); break; case 5: move_current_back(); } } while(option!=0); }

OUTPUT:

The list is empty! Please select an option: 0.Exit the program. 1.Add a node to the end of the list. 2.Delete the start node from the list. 3.Delete the end node from the list. 4. Move the current pointer on one node. 5.Move the current pointer back one node.

27

BINARY SEARCH TREE

PROGRAM:

#include<iostream.h> #include<conio.h> #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; int main() { int ch,y; for(i=1;i<40;i++) tree[i]=-1; while(1) { cout<<"1.Insert \n 2.Delete \n 3.Display \n 4.Search \n 5.Exit \n Enter your choice:"; cin>>ch; switch(ch) { case 1: cout<<"enter the elements 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";

28

break; case 3: display(1); cout<<"\n"; for(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) {

29

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) search(2*s); else if(tree[s]<x) search(2*s+1); else return s;l } 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];

30

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);
OUTPUT:

1.Insert 2.Delete 3.Display 4.Search 5.Exit Enter your choice:1 Enter the elements to insert 123 1.INSERT 2.DELETE 3.DISPLAY SEARCH 4.SEARCH 5.EXIT Enter the choice:2 Enter the element to delete 123 Element deleted

31

HEAP SORT PROGRAM:

#include<iostream.h> #include<conio.h> #define size 10 int a[size]; void main() { int i,n,val; int getdata(); void heap(int,int); void sort(int); clrscr(); cout<<"\n\t\t Program for heap sort"; n=getdata(); for(i=1;i<n;i++) { val=a[i]; heap(i,val); } cout<<"\n heap is created as"; for(i=0;i<n;i++) cout<<a[i]; sort(n); cout<<"\n after sorting by heap sort"; for(i=0;i<n;i++) cout<<a[i]; getch(); } int getdata() { int i,n; cout<<"\n How many elements you want to sort"; cin>>n; if(n>size) { cout<<"\n More elements than array size"; return n;

32

} cout<<"enter the elements"; for(i=0;i<n;i++) cin>>a[i]; return n; } void heap(int i,int val) { int s,parent; s=i; parent=(s-1)/2; while(s>0&&a[parent]<val) { a[s]=a[parent]; s=parent; parent=(parent-1)/2; } a[s]=val; } void sort(int n) { int i,node,parent,pos; for(i=n-1;i>0;i--) { pos=a[i]; a[i]=a[0]; parent=0; if(i==1) node=-1; else node=1; if(1>2&&a[2]>a[1]) node=2; while(node>=0&&pos<a[node]) { a[parent]=a[node]; parent=node; node=2*parent+1; if(node+1<=i-1&&a[node]<a[node+1]) node++; if(node>i-1) node=-1; }

33

a[parent]=pos; }}

OUTPUT:

Program for heap sort How many elements you want to sort:2 Enter the elements 2 5 heap is created as 5 2 after sorting by heap sort 2 5

34

QUICK SORT

PROGRAM:

include<process.h> #include<iostream.h> #include<conio.h> #include<stdlib.h> int partition(int low,int high,int arr[]); void quick_sort(int low,int high ,int arr[]); void main() { int*a,n,low,high,i; clrscr(); cout<<"/**********quick sort algorithm implementation**********/"; cout<<"enter number of elements:"; cin>>n; a=new int[n]; /*cout<<"enter the elements:"; for(i=0;i<n;i++) cin>>a;*/ for(i=0;i<n;i++) a[i]=rand()%100; clrscr(); cout<<"initial order of elements "; for(i=0;i<n;i++) cout<<a[i]<<" "; cout<<" "; high=n-1; low=0; quick_sort(low,high,a); cout<<"final array after sorting:"; for(i=0;i<n;i++) cout<<a[i]<<" "; getch(); } /*function of partitioning the array*/ int partition(int low,int high,int arr[]) {

35

int i,high_vac,low_vac,pivot/*,itr*/; pivot=arr[low]; while(high>low) { high_vac=arr[high]; while(pivot<high_vac) { if(high<=low)break; high--; high_vac=arr[high]; } arr[low]=high_vac; low_vac=arr[low]; while(pivot>low_vac) { if(high<=low)break; low++; low_vac=arr[low]; } arr[high]=low_vac; } arr[low]=pivot; return low; } void quick_sort(int low,int high,int arr[]) { int piv_index,i; if(low<high) { piv_index=partition(low,high,arr); quick_sort(low,piv_index-1,arr); quick_sort(piv_index+1,high,arr); } }
OUTPUT: /**********quick sort algorithm implementation**********/ Enter number of elements:2 Initial order of elements 46 30

36

Final array after sorting 30 46

37

EC 2209 DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING LAB 1. 2. 3. 4. 5. Basic Programs for C++ Concepts Array implementation of List Abstract Data Type (ADT) Linked list implementation of List ADT Cursor implementation of List ADT Stack ADT - Array and linked list implementations

0 0 3 2

The next two exercises are to be done by implementing the following source files (a) Program source files for Stack Application 1 (b) Array implementation of Stack ADT (c) Linked list implementation of Stack ADT (d) Program source files for Stack Application 2 An appropriate header file for the Stack ADT should be #included in (a) and (d) Implement any Stack Application using array implementation of Stack ADT (by implementing files (a) and (b) given above) and then using linked list implementation of Stack ADT (by using files (a) and implementing file (c)) 7. Queue ADT Array and linked list implementations 8. Search Tree ADT - Binary Search Tree 9. Heap Sort 10. Quick Sort LIST OF EQUIPMENTS AND COMPONENTS FOR A BATCH OF 30 STUDENTS ( 1 per Batch) S.No Name of the equipments / Components Quzntity Required Remarks 1 P IV Computer Variable DC Power Supply 30 Nos 2 C and C++ Compiler 30 Users Consumables (Minimum of 25 Nos. each) Nil 6.

38

Potrebbero piacerti anche