Sei sulla pagina 1di 83

Geethanjali College of Engineering and Technology

Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.1 Sheet

1) Program to demonstrate abstract classes #include<iostream.h> #include<conio.h> class item { private: int a,b,c; public: void get(); void sum(); void multiply(); }; void item::get() { cout<<"Enter a and b:"; cin>>a>>b; } void item::sum() { c=a+b; cout<<"\nThe sum of a and b is:"<<c; } void item::multiply() { c=a*b; cout<<"\nThe multiplication of a and b is:"<<c; }

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.2 Sheet

void main() { clrscr(); item i; i.get(); i.sum(); i.multiply(); getch(); }

Output:
Enter a and b : 10 20 The sum of a and b is : 30 The multiplication of a and b is : 200

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.3 Sheet

2) Program to demonstrate concrete classes


#include<iostream.h> #include<conio.h> class item { private: int a,b,c,d; public: void get() { cout<<"Enter a and b:"; cin>>a>>b; } void display() { c=a+b; cout<<"\nSum is:"<<c; d=a-b; cout<<"\nSubtraction is:"<<d; } }; void main() { clrscr(); item i; i.get(); i.display();

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.4 Sheet

getch(); }

Output:
Enter a and b : 10 20 Sum is : 30 Subtraction is : -10

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.5 Sheet

3) Program to demonstrate templates using functions


#include<iostream.h> #include<conio.h> template <class T> T min(T a,T b) { return(a<b)?a:b; } void main() { int a=10,b=20; clrscr(); cout<<"The minimum is:"<<min(a,b); getch(); }

Output : The minimum is :10

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.6 Sheet

4) Program to demonstrate scope resolution operator


#include<iostream.h> #include<conio.h> class item { private: int a,b,c; public: void sum() { cout<<"Enter the values of a and b:"; cin>>a>>b; c=a+b; cout<<"The sum of a and b is:"<<c; } void multiply(); }; void item::multiply() { cout<<"\nEnter the values of a and b:";

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.7 Sheet

cin>>a>>b; c=a*b; cout<<"The multiplication of a and b is:"<<c; } void main() { clrscr(); item i; i.sum(); i.multiply(); getch(); }

Output:
Enter the values of a and b : 10 20 The sum of a and b is : 30 Enter the values of a and b : 10 20 The multiplication of a and b is : 200

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.8 Sheet

5) Program to insert,delete and display elements in an array


#include<iostream.h> #include<conio.h> const int max=5; class array { private: int arr[max]; public: void insert(int pos,int num); void del(int pos); void display(); }; void array::insert(int pos,int num) { for(int i=max-1;i>=pos;i--)

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.9 Sheet

arr[i]=arr[i-1]; arr[i]=num; } void array::del(int pos) { for(int i=pos;i<max;i++) arr[i-1]=arr[i]; arr[i-1]=0; } void array::display() { cout<<endl; for(int i=0;i<max;i++) cout<<" "<<arr[i]; }

void main() { clrscr(); array a; a.insert(1,11); a.insert(2,12); a.insert(3,13); a.insert(4,14); a.insert(5,15); cout<<"\n Elements of array:"; a.display(); a.del(5); a.del(2); cout<<"\nAfter deletion elements in array are:"; a.display();

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.10 Sheet

a.insert(2,222); a.insert(5,555); cout<<"\nAfter insertion elements in array are:"; a.display(); getch(); }

Output:
Elements of array: 11 12 13 14 15 After deletion elements in array are: 11 13 14 0 0 After insertion elements in array are: 11 222 13 14 555

6) Program for reversing the elements in an array and display

the position of given element in the array


#include<iostream.h> #include<conio.h> const int max=5; class array { private: int arr[max]; public: void insert(int pos,int num);

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.11 Sheet

void reverse(); void display(); void search(int num); }; void array::insert(int pos,int num) { for(int i=max-1;i>=pos;i--) arr[i]=arr[i-1]; arr[i]=num; } void array::reverse() { for(int i=0;i<max/2;i++) { int temp=arr[i]; arr[i]=arr[max-1-i]; arr[max-1-i]=temp; } }

void array::search(int num) { for(int i=0;i<max;i++) { if(arr[i]==num); { cout<<"\nThe element"<<num<<"is present at"<<i+1<<"position"; return; } }

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.12 Sheet

if(i==max) { cout<<"\nThe element"<<num<<"is not present in given array"; } } void array::display() { cout<<endl; for(int i=0;i<max;i++) cout<<" "<<arr[i]; } void main() { clrscr(); array a; a.insert(1,11); a.insert(2,12); a.insert(3,13); a.insert(4,14); a.insert(5,15); cout<<"\nElements of array:"; a.display(); a.insert(2,222); a.insert(5,555); cout<<"\nAfter insertion elements in array are:"; a.display(); a.reverse(); cout<<"\nAfter reversing elements in array are:"; a.display(); a.search(222); a.search(666);

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.13 Sheet

getch(); }

Output:
Elements of array : 11 12 13 14 15 After insertion elements in array are : 11 222 12 13 555 After reversing elements in array are : 555 13 12 222 11 The element 222 is present at 4 position The element 666 is not present in given array

7)

Program to add new node at the given position in the single linkedlist
#include<iostream.h> #include<conio.h> const null=0;

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.14 Sheet

class linklist { struct node { int data; node*link; }*p; public: linklist(); void append(int num); void addatbeg(int num); void addatafter(int loc,int num); void display(); }; linklist::linklist() { p=null; } void linklist::append(int num) { node*temp,*r; if(p==null) { temp=new node; temp->data=num; temp->link=null; p=temp; } else { temp=p;

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.15 Sheet

while(temp->link!=null) temp=temp->link; r=new node; r->data=num; r->link=null; temp->link=r; } } void linklist::addatbeg(int num) { node*temp; temp=new node; temp->data=num; temp->link=p; p=temp; } void linklist::addatafter(int loc,int num) { node*temp,*r; temp=p; for(int i=0;i<loc;i++) { temp=temp->link; if(temp==null) { cout<<"\nThere are less than"<<loc<<"elements in the list"<<endl; return; } } r=new node; r->data=num;

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.16 Sheet

r->link=temp->link; temp->link=r; } void linklist::display() { node *temp=p; cout<<endl; while(temp!=null) { cout<<temp->data<<" "; temp=temp->link; } } void main() { clrscr(); linklist l; l.append(14); l.append(30); l.append(25); l.append(42); l.append(17); cout<<"\nThe elements in the linklist are:"; l.display(); l.addatbeg(999); l.addatbeg(888); l.addatbeg(777); cout<<"\nElements in the linklist after adding at the beginning are:"; l.display(); l.addatafter(7,0); l.addatafter(2,1);

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.17 Sheet

l.addatafter(5,99); cout<<"\nElements in the linklist after addition at the given position are :; l.display(); getch(); }

Output :
The elements in the linklist are : 14 30 25 42 17 Elements in the linklist after adding at the beginning are : 777 888 999 14 30 25 42 17 Elements in the linklist after addition at the given position are : 777 888 999 1 14 30 99 25 42 17 0

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.18 Sheet

8) Program to add new node at the given position in the double linklist
#include<iostream.h> #include<conio.h> class linklist { private: struct dnode { dnode *prev; int data; dnode*next; }*p; public: linklist(); void d_append(int num); void d_addatbeg(int num); void d_addatafter(int loc,int num); void d_display(); ~linklist(); }; linklist::linklist() { p=NULL; } void linklist::d_append(int num) { dnode*r,*q;

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.19 Sheet

q=p; if(q==NULL) { q=new dnode; q->prev=NULL; q->data=num; q->next=NULL; p=q; } else { while(q->next!=NULL) q=q->next; r=new dnode; r->data=num; r->next=NULL; r->prev=q; q->next=r; } } void linklist::d_addatbeg(int num) { dnode*q; q=new dnode; q->prev=NULL; q->data=num; q->next=p; p->prev=q; p=q; } void linklist::d_addatafter(int loc,int num) {

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.20 Sheet

dnode *q; q=p; for(int i=0;i<loc;i++) { q=q->next; if(q==NULL) { cout<<"\nThere are less than"<<loc<<"elements"; return; } } q=q->prev; dnode*temp=new dnode; temp->data=num; temp->prev=q; temp->next=q->next; temp->next->prev=temp; q->next=temp; } void linklist::d_display() { dnode*temp=p; cout<<endl; while(temp!=NULL) { cout<<temp->data<<" "; temp=temp->next; } } linklist::~linklist() { p=NULL;

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.21 Sheet

} void main() { clrscr(); linklist l; l.d_append(1); l.d_append(2); l.d_append(14); l.d_append(17); l.d_append(99); cout<<"\nElements in double link list are:"; l.d_display(); l.d_addatbeg(1); l.d_addatbeg(99); cout<<"\nElements in linklist after addition at the beginning are :; l.d_display(); l.d_addatafter(4,56); cout<<"\nElements in linklist after adding at given position are :; l.d_display(); getch(); }

Ouput :
Elements in double link list are : 1 2 14 17 99 Elements in linklist after addition at the beginning are : 99 1 1 2 14 17 99 Elements in linklist after adding at given position are :

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.22 Sheet

99 1 1 2 56 14 17 99

9) Program to delete node using double link list


#include<iostream.h> #include<conio.h> class linklist { private: struct dnode { dnode*prev; int data; dnode*next; }*p; public: linklist(); void d_append(int num); void d_delete(int num); void d_display(); }; linklist::linklist()

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.23 Sheet

{ p=NULL; } void linklist::d_append(int num) { dnode*q,*r; q=p; if(q==NULL) { q=new dnode; q->prev=NULL; q->data=num; q->next=NULL; p=q; } else { while(q->next!=NULL) q=q->next; r=new dnode; r->data=num; r->next=NULL; r->prev=q; q->next=r; } } void linklist::d_delete(int num) { dnode*q=p; while(q!=NULL) { if(q->data==num)

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.24 Sheet

{ if(q==p) { p=p->next; p->prev=NULL; } else { if(q->next==NULL) q->prev->next=NULL; else { q->prev->next=q->next; p->next->prev=q->prev; } delete q; } return; } q=q->next; } cout<<"\n"<<num<<"not found"; } void linklist::d_display() { dnode*temp=p; cout<<endl; while(temp!=NULL) { cout<<temp->data<<" "; temp=temp->next; }

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.25 Sheet

} void main() { clrscr(); linklist l; l.d_append(1); l.d_append(2); l.d_append(14); l.d_append(17); l.d_append(99); cout<<"\nElements in double linklist:"; l.d_display(); l.d_delete(55); l.d_delete(2); l.d_delete(99); cout<<"\nElements in double linklist after deletion are:"; l.d_display(); getch(); }

Output :
Elements in double linklist : 1 2 14 17 99 55 not found Elements in double linklist after deletion are : 1 14 17

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.26 Sheet

10) Program to implement a stack using an array


#include<iostream.h> #include<conio.h> const int max=10; class stack { private: int arr[max]; int top; public:

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.27 Sheet

stack(); void push(int item); int pop(); }; stack::stack() { top=-1; } void stack::push(int item) { if(top==max-1) { cout<<endl<<""<<"stack is full"; return; } top++; arr[top]=item; } int stack::pop() { if(top==-1) { cout<<endl<<"stack is empty"; return NULL; } int data=arr[top]; top--; return data; } void main() { stack s;

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.28 Sheet

clrscr(); s.push(11); s.push(23); s.push(-8); s.push(16); s.push(27); s.push(14); s.push(20); s.push(39); s.push(2); s.push(7); s.push(88); int i=s.pop(); cout<<"\nitem poped:"<<i; i=s.pop(); cout<<"\nitem poped:"<<i; i=s.pop(); cout<<"\nitem poped:"<<i; i=s.pop(); cout<<"\nitem poped:"<<i; i=s.pop(); cout<<"\nitem poped:"<<i; getch(); }

Output :
stack is full item poped:7 item poped:2 item poped:39 item poped:20

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.29 Sheet

item poped:14

11) Program to implement stack as a linked list #include<iostream.h> #include<conio.h>

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.30 Sheet

class stack { private: //structure containing data part and link part struct node { int data; node*link; }*top; public: stack(); void push(int item); int pop(); ~stack(); }; //initialises data members stack::stack() { top=NULL; } //adds a new node to the stack as linked list void stack::push(int item) { node*temp; temp=new node; if(temp==NULL) cout<<endl<<"Stack is full"; temp->data=item; temp->link=top; top=temp; } int stack::pop()

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.31 Sheet

{ if(top==NULL) { cout<<endl<<"Stack is empty"; return NULL; } node *temp; int item; temp=top; item=temp->data; top=top->link; delete temp; return item; } //deallocates memory stack::~stack() { if(top==NULL) return; node*temp; while(top!=NULL) { temp=top; top=top->link; delete temp; } } void main() { stack s; clrscr(); s.push(14);

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.32 Sheet

s.push(3); s.push(18); s.push(29); s.push(31); s.push(16); int i=s.pop(); cout<<"\nItem poped:"<<i; i=s.pop(); cout<<"\nItem poped:"<<i; i=s.pop(); cout<<"\nItem poped:"<<i; getch(); }

Output :
Item poped:16 Item poped:31 Item poped:29

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.33 Sheet

12) Program to implement Recursive function #include<iostream.h> #include<conio.h> int fact(int); void main() { int n; clrscr(); cout<<"Enter a number:"; cin>>n; cout<<"Factorial of"<<n<<"is:"<<fact(n); getch(); } int fact(int z) { if(z==1) return(1); else return(z*fact(z-1)); } Output: Enter a number:5 Factorial of5is:120

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.34 Sheet

13) Program to implement queue using an array #include<iostream.h> #include<conio.h> const int max=10; class queue { private: int arr[max]; int front,rear,null; public: queue(); void addq(int item); int delq(); }; queue::queue() { front=-1; rear=-1; } void queue::addq(int item) { if(rear==max-1) { cout<<"\nQueue is full"; return; } rear++;

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.35 Sheet

arr[rear]=item; if(front==-1) front=0; }

int queue() { int data; if(front==-1) { cout<<"\nQueue is empty"; return null; } data=arr[front]; arr[front]=0; if(front==rear) front=rear=-1; else front++; return data; } void main() { queue q; clrscr(); q.addq(23); q.addq(19); q.addq(11); q.addq(25); q.addq(-10); q.addq(16);

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.36 Sheet

q.addq(17); q.addq(22); q.addq(19); q.addq(30); q.addq(32); int i=q.delq(); cout<<"\n Item deleted:"<<i; i=q.delq(); cout<<"\n Item deleted:"<<i; i=q.delq(); cout<<"\n Item deleted:"<<i; getch(); }

Output :
Queue is full Item deleted:23 Item deleted:19 Item deleted:11

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.37 Sheet

14) Program to implement queue as linked list #include<iostream.h> #include<conio.h> class queue { private: struct node { int data; node *link; }*front,*rear; public: queue(); void addq(int item); int delq(); ~queue(); }; // initialises data members queue::queue() {

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.38 Sheet

front=rear=NULL; } // adds an element to the queue void queue:: addq(int item) { node *temp; temp=new node; if(temp==NULL) cout<<"\n queue is full"; temp-> data=item; temp-> link=NULL; if(front==NULL) { rear=front=temp; return; } rear->link=temp; rear=rear->link; } //removes an element from the queue int queue:: delq() { if(front==NULL) { cout<<"\n queue is empty"; return NULL; } node *temp; int item; item= front->data; temp=front;

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.39 Sheet

front=front->link; delete temp; return item; } // deallocate memory queue::~queue() { if(front==NULL) return; node *temp; while(front!=NULL) { temp=front; front=front->link; delete temp; } } void main() { clrscr(); queue q; q.addq(11); q.addq(12); q.addq(23); q.addq(19); q.addq(15); q.addq(16); q.addq(28); int i=q.delq(); cout<<"\n item extracted:"<<i; i=q.delq(); cout<<"\n item extracted:"<<i; i=q.delq();

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.40 Sheet

cout<<"\n item extracted:"<<i; getch(); }

Ouput:
Item extracted:11 Item extracted:12 Item extracted:23

15) Program on implementation of deque #include<iostream.h> #include<conio.h> const int MAX=5; class dque { private: int arr[MAX]; int front,rear; public: dque(); void addqatbeg(int item); void addqatend(int item);

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.41 Sheet

int delqatbeg(); int delqatend(); void display(); int count(); }; //Initialises data members dque::dque() { front=rear=-1; for(int i=0;i<MAX;i++) arr[i]=0; } //Adds an element at the beginning of a dque void dque::addqatbeg(int item) { if(front==0&&rear==MAX-1) { cout<<"\nDeque is full"<<endl; return; } if(front==-1) { front=rear=0; arr[front]=item; return; } if(rear!=MAX-1) { int c=count(); int k=rear+1; for(int i=1;i<=c;i++) {

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.42 Sheet

arr[k]=arr[k-1]; k--; } arr[k]=item; front=k; rear++; } else { front--; arr[front]=item; } } //Adds an element at the end of a deque void dque::addqatend(int item) { if(front==0&&rear==MAX-1) { cout<<"\nDeque is full"<<endl; return; } if(front==-1) { front=rear=0; arr[rear]=item; return; } if(rear==MAX-1) { int k=front-1; for(int i=front-1;i<rear;i++) {

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.43 Sheet

k=i; if(k==MAX-1) arr[k]=0; else arr[i+1]; } rear--; front--; } rear++; arr[rear]=item; } //Removes an element from the end of deque int dque::delqatbeg() { if(front==-1) { cout<<"\nDequeis empty"<<endl; return 0; } int item=arr[front]; arr[front]=0; if(front==rear) front=rear=-1; else front++; return item; } //Removes an elelment from the rear end of the deque int dque::delqatend() { if(front==-1) {

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.44 Sheet

cout<<"\n Deque is empty"<<endl; return 0; } int item=arr[rear]; arr[rear]=0; rear--; if(rear==-1) front=-1; return item; } //Display elements of a deque void dque::display() { cout<<endl<<"front->"; for(int i=0;i<MAX;i++) cout<<" "<<arr[i]; cout<<"<-rear"; } //Counts the total number of elements in deque int dque::count() { int c=0; for(int i=0;i<MAX;i++) { if(arr[i]!=0) c++; } return c; } void main() { clrscr();

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.45 Sheet

dque q; q.addqatend(5); q.addqatend(9); q.addqatend(3); q.addqatbeg(25); q.addqatbeg(14); cout<<"\nElements in a deque"; q.display(); q.addqatbeg(45); int n=q.count(); cout<<"\nTotal number of elements in deque:"<<n; int i=q.delqatbeg(); cout<<"\nItem extracted:"<<i; cout<<"\nElements in deque after deletion at beginning:"; q.display(); i=q.delqatend(); cout<<"\nItem extracted:"<<i; cout<<"\nElelments in a deque after deletion at the end:"; q.display(); n=q.count(); cout<<"\nTotal number of elements in deque:"<<n; getch(); }

Output :

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.46 Sheet

Elements in a deque front-> 14 25 5 9 3<-rear Deque is full Total number of elements in deque:5 Item extracted:14 Elements in deque after deletion at beginning: front-> 0 25 5 9 3<-rear Item extracted:3 Elelments in a deque after deletion at the end: front-> 0 25 5 9 0<-rear Total number of elements in deque:3

16) Program to implement binary tree

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.47 Sheet

#include<iostream.h> #include<conio.h> class btree { private: struct node { node*left; char data; node*right; }*root; char*a; public: btree(char*ar,int size); void insert(int index); static node*builttree(char*arr,int index); void display(); static void inorder(node*sr); ~btree(); static void del(node*sr); }; btree::btree(char*ar,int size) { root=NULL; a=new char[size]; for(int i=0;i<size;i++) *(a+i)=ar[i]; } void btree::insert(int index) { root=builttree(a,index); }

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.48 Sheet

node*btree::builttree(char*arr,int index) { node*temp=NULL; if(*(arr+index)!='\0') { temp=new node; temp->left=builttree(arr,2*index+1); temp->data=*(arr+index); temp->right=builttree(arr,2*index+2); } return temp; } void btree::display() { inorder(root); } void btree::inorder(node*sr) { if(sr!=NULL) { inorder(sr->left); cout<<sr->data<<"\t"; inorder(sr->right); } } btree::~btree() { delete a; del(root); } void btree::del(node*sr) {

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.49 Sheet

if(sr!=NULL) { del(sr->left); del(sr->right); } delete sr; } void main() { clrscr(); char arr[]={'A','B','C','D','E','F','G','\0','\0','\H','\0', '\0','\0','\0','\0','\0','\0','\0','\0','\0','\0'}; int sz=sizeof(arr); btree bt(arr,sz); bt.insert(0); cout<<"\n Inorder traversal"<<endl; bt.display(); getch(); }

Output :
Inorder traversal D B H E A F C G

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.50 Sheet

17) Program to implement inorder,preorder and postorder

traversals of Binary search tree


#include<iostream.h> #include<conio.h> class btree { private: struct btreenode { btreenode*leftchild; int data; btreenode*rightchild; }*root; public: btree(); void builtbtree(int num); void insert(btreenode**sr,int num); void traverse(); void inorder(btreenode*sr); void preorder(btreenode*sr); void postorder(btreenode*sr); ~btree(); void del(btreenode*sr); }; btree::btree() {

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.51 Sheet

root=NULL; } void btree::builtbtree(int num) { insert(&root,num); } void btree::insert(btreenode**sr,int num) { if(*sr==NULL) { *sr=new btreenode; (*sr)->leftchild=NULL; (*sr)->data=num; (*sr)->rightchild=NULL; return; } else { if(num<(*sr)->data) insert(&((*sr)->leftchild),num); else insert(&((*sr)->rightchild),num); } return; } void btree::traverse() { cout<<"Inorder traversal"; inorder(root); cout<<"\nPreorder traversal"; preorder(root); cout<<"\nPostorder traversal";

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.52 Sheet

postorder(root); } void btree::inorder(btreenode*sr) { if(sr!=NULL) { inorder(sr->leftchild); cout<<"\t"<<sr->data; inorder(sr->rightchild); } else return; } void btree::preorder(btreenode*sr) { if(sr!=NULL) { cout<<"\t"<<sr->data; preorder(sr->leftchild); preorder(sr->rightchild); } else return; } void btree::postorder(btreenode*sr) { if(sr!=NULL) { postorder(sr->leftchild); postorder(sr->rightchild); cout<<"\t"<<sr->data; }

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.53 Sheet

else return; } btree::~btree() { del(root); } void btree::del(btreenode*sr) { if(sr!=NULL) { del(sr->leftchild); del(sr->rightchild); } delete sr; } void main() { clrscr(); btree bt; int req,i=1,num; cout<<"Specify the number of items to be inserted:"; cin>>req; while(i++<=req) { cout<<"\nEnter the data"; cin>>num; bt.builtbtree(num); } bt.traverse(); getch(); }

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.54 Sheet

Output :
Specify the number of items to be inserted : 4 Enter the data : 2 Enter the data : 5 Enter the data : 4 Enter the data : 6 Inorder traversal 2 Preorder traversal 2 Postorder traversal 4 4 5 6 5 4 5 6 6 2

18) Program to implement linear search method for an unsorted

array
#include<iostream.h> #include<conio.h> const int MAX=10; class array { private: int arr[MAX]; int count; public: array(); void add(int item); int search(int num); }; //Initialise data members array::array()

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.55 Sheet

{ count=0; for(int i=0;i<MAX;i++) arr[i]=0; } //Adds a new elemwnt into the array void array::add(int item) { if(count<MAX) { arr[count]=item; count++; } else cout<<"\nArray is full"; } //Finds for given element in an array int array::search(int num) { for(int i=0;i<count;i++) { if(arr[i]==num) break; } if(i==count) return -1; else return i; } void main() { clrscr(); array a;

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.56 Sheet

a.add(11); a.add(2); a.add(9); a.add(13); a.add(57); a.add(25); a.add(17); a.add(1); a.add(90); a.add(3); int num; cout<<"\nEnter number to search:"; cin>>num; int i=a.search(num); if(i==-1) cout<<"\nNumber is not present in the array"; else cout<<"\nThe number is at position"<<i<<"in the array"; getch(); }

Output :
Enter number to search : 90 The number is at position 8 in the array Enter number to search : 5 Number is not present in the array

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.57 Sheet

19) Program to implement binary search algorithm for an sorted

array
#include<iostream.h> #include<conio.h> const int MAX=10; class array { private: int arr[MAX]; int count; public:

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.58 Sheet

array(); void add(int num); void search(int num); }; //Initialised data members array::array() { count=0; for(int i=0;i<MAX;i++) arr[i]=0; } //Adds a new element to the array void array::add(int item) { if(count<MAX) { arr[count]=item; count++; } else cout<<"\nArray is full"<<endl; } //Finds for given element in the array void array::search(int num) { int mid,lower=0,upper=count-1,flag=1; for(mid=(lower+upper)/2;lower<=upper;mid=(lower+upper)/2) { if(arr[mid]==num) { cout<<"\n The number is at position"<<mid<<"in the array; flag=0;

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.59 Sheet

break; } if(arr[mid]>num) upper=mid-1; else lower=mid+1; } if(flag) cout<<"\nElement is not present in the array"; } void main() { clrscr(); array a; a.add(1); a.add(2); a.add(3); a.add(9); a.add(11); a.add(13); a.add(17); a.add(25); a.add(57); int num; cout<<"\nEnterthe number to search:"; cin>>num; a.search(num); getch(); }

Output :
Enter the number to search :17

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.60 Sheet

The number is at position 6 in the array Enter the number to search : 4 Element is not present in the array

20) Program to implement bubble sort #include<iostream.h> #include<conio.h> const int max=10; class array

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.61 Sheet

{ private: int arr[max],i,j; int count; public: array(); void add(int item); void sort(); void display(); }; array::array() { count=0; for(int i=0;i<max;i++) arr[i]=0; } void array::add(int item) { if(count<=max) { arr[count]=item; count++; } else cout<<"\nArray is full"; } void array::sort() { int temp; for(int i=0;i<count-1;i++) {

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.62 Sheet

for(int j=0;j<count-1;j++) { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } } void array::display() { for(int i=0;i<count;i++) cout<<arr[i]<<"\t"; cout<<endl; } void main() { clrscr(); array a; a.add(25); a.add(17); a.add(31); a.add(9); a.add(30); a.add(2); cout<<"\Bubble Sort"; cout<<"\nBefore sorting:\n"; cout<<endl; a.display(); a.sort();

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.63 Sheet

cout<<"\n"; cout<<"After the sorting:\n"; cout<<endl; a.display(); getch(); }

Output :
Bubble Sort Before sorting: 25 17 31 9 After the sorting: 2 9 17 25

30 30

2 31

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.64 Sheet

21) Program to implement quicksort #include<iostream.h> #include<conio.h> const int max=10; class array { private: int arr[max]; int count; public: array(); void add(int item); void quicksort(int lower,int upper); int getcount(); static int split(int *,int,int); void display(); }; array::array() { count=0; for(int i=0;i<max;i++) arr[i]=0; } void array::add(int item) { if(count<max) { arr[count]=item; count++; } else

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.65 Sheet

cout<<"\nArray is full"<<endl; } int array::getcount() { return count; } void array::quicksort(int lower,int upper) { if(upper>lower) { int i=split(arr,lower,upper); quicksort(lower,i-1); quicksort(i+1,upper); } } int array::split(int *a,int lower,int upper) { int i,p,q,t; p=lower+1; q=upper; i=a[lower]; while(q>=p) { while(a[p]<i) p++; while(a[q]>i) q--; if(q>p) { t=a[p]; a[p]=a[q]; a[q]=t;

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.66 Sheet

} } t=a[lower]; a[lower]=a[q]; a[q]=t; return q; } void array::display() { for(int i=0;i<count;i++) cout<<arr[i]<<"\t"; cout<<endl; } void main() { clrscr(); array a; a.add(1); a.add(2); a.add(5); a.add(9); a.add(6); a.add(4); a.add(10); a.add(7); a.add(17); a.add(12); cout<<"Quicksort\n"; cout<<"Array before sorting:"<<endl; a.display(); int c=a.getcount(); a.quicksort(0,c-1);

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.67 Sheet

cout<<"Array after sorting:"<<endl; a.display(); getch(); }

Output :
Quicksort Array before sorting: 1 2 5 9 6 Array after sorting: 1 2 4 5 6

4 7

10 9

7 10

17 12

12 17

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.68 Sheet

22) Program to implement selection sort #include<iostream.h> #include<conio.h> int const MAX=10; class array { private: int arr[MAX]; int count; public: array(); void add(int item); void sort(); void display(); }; array::array() { count=0; for(int i=0;i<MAX;i++) arr[i]=0; }

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.69 Sheet

void array::add(int item) { if(count<MAX) { arr[count]=item; count++; } else cout<<"\nArray is full"; } void array::sort() { int temp; for(int i=0;i<=count-2;i++) { for(int j=i+1;j<=count-1;j++) { if(arr[i]>arr[j]) { temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } } void array::display() { for(int i=0;i<count;i++) cout<<arr[i]<<"\t"; cout<<endl; }

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.70 Sheet

void main() { clrscr(); array a; a.add(25); a.add(17); a.add(31); a.add(13); a.add(2); cout<<"\nSelection sort:"; cout<<"\n Array before sorting:"<<endl; a.display(); a.sort(); cout<<"\n Array after sorting:"<<endl; a.display(); getch(); }

Output :
Selection sort: Array before sorting: 25 17 31 13 Array after sorting: 2 13 17 25

2 31

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.71 Sheet

23) Program to convert infix expression to postfix expression using

stack
#include<iostream.h> #include<string.h> #include<ctype.h> const int MAX=50; class infix { private: char target[MAX],stack[MAX]; char *s,*t; int top;

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.72 Sheet

public: infix(); void setexpr(char*str); void push(char c); char pop(); void convert(); int priority(char c); void show(); }; infix::infix() { top=-1; strcpy(target,""); strcpy(stack,""); t=target; s=""; } void infix::setexpr(char*str) { s=str; } void infix::push(char c) { if(top==MAX) cout<<"Stack is full\n"; else { top++; stack[top]=c; } } char infix::pop() {

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.73 Sheet

if(top==-1) { cout<<"Stack is empty\n"; return-1; } else { char item=stack[top]; top--; return item; } } void infix::convert() { while(*s) { if(*s==' '||*s=='\t') { s++; continue; } if(isdigit(*s)||isalpha(*s)) { while(isdigit(*s)||isalpha(*s)) { *t=*s; s++; t++; } } if(*s=='(') {

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.74 Sheet

push(*s); s++; } char opr; if(*s=='*'||*s=='+'||*s=='/'||*s=='%'||*s=='-'||*s=='$') { if(top!=-1) { opr=pop(); while(priority(opr)>=priority(*s)) { *t=opr; t++; opr=pop(); } push(opr); push(*s); } else push(*s); s++; } if(*s==')') { opr=pop(); while((opr)!='(') { *t=opr; t++; opr=pop(); } s++; }

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.75 Sheet

} while(top!=-1) { char opr=pop(); *t=opr; t++; } *t='\0'; } int infix::priority(char c) { if(c=='$') return 3; if(c=='*'||c=='/'||c=='%') return 2; else { if(c=='+'||c=='-') return 1; else return 0; } } void infix::show() { cout<<target; } void main() { char expr[MAX]; infix q; cout<<"\nEnter an expression in infix form:"; cin.getline(expr,MAX);

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.76 Sheet

q.setexpr(expr); q.convert(); cout<<"The postfix expression is:"; q.show(); }

Output:
Enter an expression in infix form : 4$2*3-3+8/4/(1+1) Stack is empty The postfix expression is : 42$3*3-84/11+/+

24) Program to implement pattern matching algorithm


#include<iostream.h>

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.77 Sheet

#include<string.h> #include<conio.h> const int MAX=20; class string { private: char str[MAX]; public: string(); string(char*s); static int xstrsearch(string&s1,string&s2); void show(); }; //initialises data string::string() { } //initialises data string::string(char*s) { strcpy(str,s); } //searches for the given pattern s2 into the string s1 int string::xstrsearch(string&s1,string&s2) { char*src=s1.str; char*trg=s2.str; int l1=strlen(src); int l2=strlen(trg); for(int i=0,j,k;i<=l1-l2;i++) { j=0; k=i;

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.78 Sheet

while((src[k]==trg[j])&&(j<l2)) { k++; j++; } if(j==l2) return i; } return -1; } //Displays the string void string::show() { cout<<str<<endl; } void main() { //create object and display string string s1("NagpurKicit"); clrscr(); cout<<"string s1:"; s1.show(); string s2("Kicit"); cout<<"String s2:"; s2.show(); //search if s2 is present in s1 int pos=string::xstrsearch(s1,s2); cout<<"\nThe pattern string is found at position:"<<pos<<endl; }

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.79 Sheet

Output:
string s1:NagpurKicit String s2:Kicit The pattern string is found at position:6

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.80 Sheet

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.81 Sheet

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.82 Sheet

Geethanjali College of Engineering and Technology


Cheeryal (V), Keesara (M), Ranga Reddy District. Roll No. 10R11F0004 No.83 Sheet

Potrebbero piacerti anche