Anda di halaman 1dari 62

// WAP IMPLEMENT FUNCTION OVERLOADING TO CALCULATE A^B

#include<iostream.h>
#include<math.h>
#include<conio.h>
int calc(int a, int b)
{
return pow(a,b);
}
float calc(float a, float b)
{
return pow(a,b);
}
float calc(int a, float b)
{
return pow(a,b);
}
float calc(float a, int b)
{
return pow(a,b);
}
void main()
{
clrscr();
int P,Q;
float R,S;
cout<<"Enter 1st int number : "; cin>>P;
cout<<"Enter 2nd number : "; cin>>Q;
cout<<"Enter 1st real number : "; cin>>R;
cout<<"Enter 2nd real number : "; cin>>S;
cout<<P<<"^"<<Q<<" = "<<pow(P,Q)<<"\n\n";
cout<<P<<"^"<<R<<" = "<<pow(P,R)<<"\n\n";
cout<<R<<"^"<<Q<<" = "<<pow(R,Q)<<"\n\n";
cout<<R<<"^"<<S<<" = "<<pow(R,S);
getch();
}

OUTPUT:

// WAP USING FUNCTION OVERLOADING TO CALCULATE AREA OF CIRCLE, SQUARE AND RECTANGLE.
#include<iostream.h>
#include<math.h>
#include<conio.h>
float area(float p)
{
return 3.14*p*p;
}
float area(int p)
{
return p*p;
}
float area(float p,float q)
{
return p*q;
}
void main()
{
clrscr();
int p,q,ch;
float a,b,r;
char choice;
do{
cout<<"\n\nChoose from the following : ";cout<<"\n\n1. Area of square ";cout<<"\n\n2. Area of circle ";
cout<<"\n\n3. Area of rectangle ";cout<<"\n\nEnter your choice : "; cin>>ch;
switch(ch)
{
case 1:
cout<<"\n\nEnter side of square : ";
cin>>p;
cout<<"\n\narea of square is : "<<area(p);
break;
case 2:

cout<<"\n\nEnter radius of circle : ";


cin>>a;
cout<<"\n\narea of circle is : "<<area(a);
break;

case 3:

cout<<"\n\nEnter length of rectangle : ";


cin>>a;
cout<<"\n\nEnter breadth of rectangle : ";
cin>>b;
cout<<"\n\narea of rectangle is : "<<area(a,b);
break;

}
cout<<"\n\nWant to choose again : ";
cin>>choice;
}
while(choice=='y'||choice=='Y');
getch();
}

OUTPUT

/* Program to define the classes PERSON, GAME and STUDENT & to access the essential data using multiple
inheritance.*/
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class person
{
char name[21];
int age;
public:
void indata()
{
cout<<"\n\nEnter the name of Student: " ;
gets(name);
cout<<"\n\nEnter the age : ";
cin>>age;
}
void outdata();
};
void person::outdata() // since the function contains loop so it is not made inline
{
cout<<"\n\n";
for(int i=0; i<79; i++)
cout<<"-";
cout<<"\n\nName of the student is: "<<name;
cout<<"\n\nAge of the student is : "<<age;
}
class game
{
char game_name[20];
public:
void input()
{
cout<<"\n\nEnter the game name : ";
cin.get();cin.getline(game_name,20);
}
void output()
{
cout<<"\n\nGame opted by the student is : "<<game_name;
}
};
class student: public person, public game
{
float Tmarks;
int rollno;
public:
char calgrade()
{
if(Tmarks>90)
return 'A';
else if(Tmarks>80&&Tmarks<=90)
return 'B';

else if(Tmarks>70&&Tmarks<=80)
return 'C';
else if(Tmarks>60&&Tmarks<=70)
return 'D';
else
return 'E';
}
void enter()
{
indata();
// indata() of class person called here
cout<<"\n\nEnter the roll number: ";
cin>>rollno;
input();
// input() of class game called here
cout<<"\n\nEnter total marks (out of 100) : ";
cin>>Tmarks;
}
void display()
{
outdata();
cout<<"\n\nRoll number : "<<rollno;
output();
cout<<"\n\nTotal marks are : "<<Tmarks;
cout<<"\n\nGrade = "<<calgrade();
}
};
void main()
{
clrscr();
student A;
A.enter();
A.display();
getch();
}

OUTPUT:

// WAP TO CREATE A MENU DRIVEN PROGRAM TO IMPLEMENT THE FOLLOWING METHODS OF SEARCHING.
A)LINEAR SEARCH
B) BINARY SEARCH
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int Linear(int [],int,int);
int Binary(int [],int,int);
int main()
{
clrscr();
int a[50], key,size,pos,i,j;
char ch;
cout<<" Enter the desired size of array(max.50)";
cin>>size;
do
{
cout<<"......MENU......"<<endl;
cout<<"1: LINEAR SEARCH<<endl;
cout<<"2: BINARY SEARCH"<<endl;
cout<<"3: EXIT"<<endl;
cout<<".Enter Your Choice."<<endl;
cin>>i;
switch(i)
{
case 1:
clrscr();
cout<<"......LINEAR SEARCH......"<<endl;
cout<<"Enter "<<size<<" elements"<<endl;
for(j=0;j<size;j++)
{
cin>>a[j];
}
cout<<"Enter Element to be searched"<<endl;
cin>>key;
pos=Linear(a,size,key);
if(pos==-1)
cout<<"\n Sorry!! Element is not present is array"<<endl;
else
cout<<"\n Element found at index:"<<pos<<" position "<<pos+1<<endl;
break;
case 2:
clrscr();
cout<<"......BINARY SEARCH......"<<endl;
cout<<"Enter "<<size<<" elements"<<endl;
for(j=0;j<size;j++)
{
cin>>a[j];
}
cout<<"Enter Element to be searched"<<endl;
cin>>key;
pos=Binary(a,size,key);
if(pos==-1)
cout<<"\n Sorry!! Element is not present is array"<<endl;
else

cout<<"\n Element found at index:"<<pos<<" position "<<pos+1<<endl;


break;
case 3: exit(0);
break;
default: cout<<"Wrong Choice"<<endl;
break;
}
cout<<"do you want to continue"<<endl;
cin>>ch;
} while(ch=='Y'||ch=='y');
return 0;
}
int Binary(int a[],int size,int item)
{
int beg,last,mid;
beg=0;
last=size-1;
while(beg<=last)
{
mid=(beg+last)/2;
if(item==a[mid])
return mid;
else if (item> a[mid])
beg=mid+1;
else
last=mid-1;
}
return -1;
}
int Linear(int a[],int size,int item)
{
for(int i=0;i<size;i++)
{
if(a[i]==item])
return i;
}
return -1;
}

OUTPUT

// WAP TO CREATE A MENU DRIVEN PROGRAM TO IMPLEMENT THE FOLLOWING METHODS OF SORTING.
//A)INSERTION SORT
B) SELECTION SORT
C) BUBBLE SORT
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
void Selection(int [],int);
void Bubble(int [],int);
void insertion(int [],int);
int main()
{
int a[50], key,size,pos,i,j;
char ch;
do
{
cout<<"......MENU......"<<endl;
cout<<"1: INSERTION SORT"<<endl;
cout<<"2: BUBBLE SORT"<<endl;
cout<<"3: SELECTION SORT"<<endl;
cout<<"4: EXIT"<<endl;
cout<<".Enter Your Choice."<<endl;
cin>>i;
switch(i)
{
case 1: system("cls");
cout<<"......INSERTION SORT......"<<endl;
cout<<" Enter the desired size of array(max.50)";
cin>>size;
cout<<"Enter "<<size<<" elements"<<endl;
for(j=1;j<=size;j++)
{
cin>>a[j];
}
insertion(a,size);
cout<<"\n\n The Sorted Array is: \n\n";
for(j=1;j<=size;j++)
cout<<a[j]<<" ";
cout<<endl;
break;
case 2:system("cls");
cout<<"......BUBBLE SORT......"<<endl;
cout<<" Enter the desired size of array(max.50)";
cin>>size;
cout<<"Enter "<<size<<" elements"<<endl;
for(j=0;j<size;j++)
{
cin>>a[j];
}
Bubble(a,size);
cout<<"The Sorted Array is"<<endl;
for(j=0;j<size;j++)
cout<<a[j]<<" ";
cout<<endl;
break;

case 3: system("cls");
cout<<"......SELECTION SORT......"<<endl;
cout<<" Enter the desired size of array(max.50)";
cin>>size;
cout<<"Enter "<<size<<" elements"<<endl;
for(j=0;j<size;j++)
{
cin>>a[j];
}
Selection(a,size);
for(j=0;j<size;j++)
cout<<a[j]<<" ";
cout<<endl;
break;
case 4: exit(0);
break;
default: cout<<"Wrong Choice"<<endl;
break;
}
cout<<"do you want to continue"<<endl;
cin>>ch;
} while(ch=='Y'||ch=='y');
return 0;
}
void Bubble(int a[],int size)
{
int tmp,ctr=0;
for(int i=0;i<size;i++)
{
for(int j=0;j<(size-1)-i;j++)
{
if(a[j]>a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
cout<<"Array after iteration:: "<<++ctr<<" --is:"<<endl;
for(int k=0;k<size;k++)
cout<<a[k]<<" ";
cout<<endl;
}
}
void Selection(int a[],int size)
{
int small,pos,tmp;
for(int i=0;i<size;i++)
{
small=a[i];
pos=i;
for(int j=i+1;j<size;j++)
{
if(a[j]<small)
{
small=a[j];
pos=j;

}
}
tmp=a[i];
a[i]=a[pos];
a[pos]=tmp;
cout<<"\n Array after pass: "<<i+1<<" is::";
for(int k=0;k<size;k++)
{
cout<<a[k]<<" ";
}
cout<<endl;
}
}
void insertion(int a[],int size)
{
int tmp,j;
a[0]=-1;
for(int i=1;i<=size;i++)
{
tmp=a[i];
j=i-1;
while(tmp<a[j])
{
a[j+1]=a[j];
j--;
}
a[j+1]=tmp;
cout<<"Array after pass::"<<i<<"::is";
for(int k=1;k<=size;k++)
cout<<a[k]<<" ";
cout<<endl;
}
}

OUTPUT

/*Write menu driven program to show following operations in a 2-d array (using user defined function)Menu
1. Adding two 2-d arrays
2. Subtracting two 2-d arrays
3. Multiplying two 2-d arrays
4. Check whether two 2-d arrays are equivalent or not
5. Display upper triangular matrix
6. Display lower triangular matrix
7. Display and find sum of diagonal elements of a 2-d array
8. Display and find the row-wise sum of a 2-d array
9. Display and find the column-wise sum of a 2-d array
10. Quit */

#include<iostream.h>
#include<process.h>
#include<conio.h>
int a[80][80],b[80][80];
// 2-D matrices
int i=0, j=0;
int m, n, p, q;
// Matrix 1 : m= no. of Rows , n=no. of Columns Matrix 2 : p= no. of Rows , q=no. of Columns
int mc, oc, sum=0;
// Function Declarations
void add(int a[80][80],int b[80][80]);
void subtract(int a[80][80],int b[80][80]);
void multiply(int a[80][80],int b[80][80]);
void multiply1(int b[80][80],int a[80][80]);
void insert_mat(int a[80][80],int b[80][80]);
void main() // main body
{
clrscr();
int ch,op; char choice;
do{
cout<<"\n\nChoose from the following menu : ";
cout<<"\n\n1. Add two 2-D arrays ";
cout<<"\n\n2. Subtract two 2-D arrays ";
cout<<"\n\n3. Multiply two 2-D arrays " ;
cout<<"\n\n4. Exit ";
cout<<"\n\n\n\nEnter your choice : ";
cin>>ch;
switch(ch)
{
case 1 :
insert_mat(a,b);
if(m==p)
if(n==q)
add(a,b);
else
{
cout<<"\n\nNumber of columns of matrices are not same;
}
else
cout<<"\n\nNumber of rows of matrices are not same. ";
break;
case 2 :
insert_mat(a,b);
if(m==p)
if(n==q)
{

cout<<"\n\n\t1. Subtract Matrix 2 from Matrix 1 ";


cout<<"\n\n\t1. Subtract Matrix 1 from Matrix 2 ";
cout<<"\n\n\tEnter your choice : ";cin>>mc;
switch(mc)
{
case 1 : subtract(a,b);break;
case 2 : subtract(b,a);break;
default: cout<<"wrong choice";
}
}
else
{
cout<<"\n\nNumber of columns of matrices are not same";
}
else
{
cout<<"\n\nNumber of rows of matrices are not same.";
}
break;
case 3 : insert_mat(a,b);
cout<<"\n\n\t(i). Multiply matrix 1 with matrix 2 ";
cout<<"\n\n\t(ii). Multiply matrix 2 with matrix 1 ";
cout<<"\n\n\tEnter your choice : ";
cin>>op;
switch(op)
{
case 1 : if(n==p)
multiply(a,b);
else
cout<<"\n\n no. of columns of first matrix != no. of rows of second matrix. ";
break;
case 2 : if(m==q)
multiply(b,a);
else
{
cout<<"\n\n no. of columns of first matrix != no. of rows of second matrix. ";
}
break;
}
break;

case 4 : exit(0);
}
cout<<"\n\n\nWant to choose again => "; cin>>choice;
}while(choice=='y'||choice=='Y');
getch();
}

//----------------------------------------------------------- Function Definitions ---------------------------------------------------------

void add(int a[80][80],int b[80][80])


{
int c[80][80];
for(i=0; i<m; i++)
for(j=0; j<n; j++)
c[i][j]=a[i][j]+b[i][j];
cout<<"\n\n\nResultant matrix by addition is : \n\n";
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
cout<<c[i][j]<<" ";
}
cout<<"\n";
}
}
void subtract(int a[80][80],int b[80][80])
{int l,u;
if (mc==1)
{
l=m;
u=n;
}
else
l=p;
u=q;
int c[80][80];
for(i=0; i<l; i++)
for(j=0; j<u; j++)
c[i][j]=a[i][j]-b[i][j];
cout<<"\n\n\nResultant matrix by Subtraction is : \n\n";
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
cout<<c[i][j]<<" ";
}
cout<<"\n";
}
}
void multiply(int a[80][80],int b[80][80])
{
int c[80][80];
j=0;
int k=0;
cout<<"\n\nResultant of Matrix Multiplication : ";
for(i=0; i<m; i++)
{ cout<<"\n";
for(j=0; j<q; j++)
{
c[i][j]=0;

for(k=0; k<n; k++)


{ c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
cout<<c[i][j]<<" ";
}
}
}

void insert_mat(int a[80][80],int b[80][80])


{
cout<<"\n\nDescribe the Size of matrix 1 : ";
cout<<"\n\n\n\tEnter the number of rows : ";
cin>>m;
cout<<"\n\n\tEnter the number of columns : ";
cin>>n;
cout<<"\n\nEnter the elements of matrix 1 : \n\n";
for(i=0; i<m; i++)
for(j=0; j<n; j++)
cin>>a[i][j];
cout<<"\n\n\n\nDescribe the size of matrix 2 : ";
cout<<"\n\n\n\tEnter the number of rows : ";
cin>>p;
cout<<"\n\n\tEnter the number of columns : ";
cin>>q;
cout<<"\n\nEnter the elements of matrix 2 : \n\n";
for(i=0; i<p; i++)
for(j=0; j<q; j++)
cin>>b[i][j];
for(i=0; i<80; i++)
cout<<"-";
cout<<"\n\nMatrix 1 is : \n\n";
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n\n";
}
cout<<"\n\nMatrix 2 is : \n\n";
for(i=0; i<p; i++)
{
for(j=0; j<q; j++)
{
cout<<b[i][j]<<" ";
}
cout<<"\n\n";
}
}

/* WAP in C++ which accepts an integer array and its size and assigns the elements into a twodimensional array of
integers in the following format: */

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[20],b[20][20],len,i,j,k;
cout<<"Enter the length of the array";
cin>>len;
cout<<"Enter"<<len<<"elements";
for(i=0;i<len;i++)
cin>>a[i];
for( k=0;k<=len;k++)
for( j=0;j<len-k;j++)
b[k][j]=a[j];
cout<<endl<<"Array you entered is:";
for(i=0;i<len;i++)
cout<<a[i]<<" ";
cout<<endl<<"two dimensional array is:"<<endl;
for(i=0;i<len;i++)
{
for(j=0;j<len;j++)
cout<<b[i][j]<<" ";
cout<<endl;
}
getch();
}

/* Write menu driven program to show following operations in a 1-d array (using user defined function)1. Inserting an
element at ith position 2. Deleting an element from an array 3. Quit*/
#include<iostream.h>
#include<limits.h>
#include<process.h>
#include<conio.h>
/*--------------------------------------------------------------Function declaration -------------------------------------------------------*/

void insert_element(int A[], int n);


void delete_element(int A[], int n);

int A[50],n,mg=0; // global variable

/*--------------------------------------------------------------- Main's body ----------------------------------------------------------------------*/


void main()
{clrscr();
int ch,o=0,h=0,t=0,k,m;
int B[50],p,subscript;
char choice;
cout<<"Enter the Size of array : ";
cin>>n;
cout<<"\n\nEnter the elements : \n\n";
for(int i=0; i<n; i++)
cin>>A[i];
cout<<"\n\nArray formed is : ";
for(i=0; i<n; i++)
cout<<A[i]<<" ";
do{
cout<<"\n\n1. Insert element in array ";
cout<<"\n\n2. Delete element in array ";
cout<<"\n\n3. Exit ";
cout<<"\n\n\nEnter your choice : ";
cin>>ch;
switch(ch)
{

case 1: insert_element(A,n);
break;
case 2: delete_element(A,n);break;
case 3: exit(0); break;
default : cout<<"\n\nPlease enter desired keyword : ";

}
cout<<"\n\nWant to choose from menu again : ";
cin>>choice;
}while(choice=='y'||choice=='Y');
getch();
} // end of main

/*----------------------------------------------------------- Function Definitions --------------------------------------------------------------*/

void insert_element(int A[], int n)


{ int p,pos;char choice;
do
{if(n>=50||n<1)
cout<<"\n\nArray Overflow.";
else
cout<<"\n\nEnter element along with its position : \n\n";
cout<<"Element = "; cin>>p;
cout<<"\n\nPosition = "; cin>>pos;
for(int i=n; i>=pos; i--)
{
A[i]=A[i-1];
}
n++;
A[n]=0;
A[pos-1]=p;
cout<<"\n\nArray formed : ";
for(i=0; i<n; i++)
cout<<A[i]<<" ";
cout<<"\n\nWant to insert again : "; cin>>choice;
}while(choice=='y'||choice=='Y');
}
void delete_element(int A[], int n)
{ int pos,flag=1;
char choice;
do{
l:
cout<<"\n\n\nEnter position of element to delete it : ";
cin>>pos;
if(pos-1>n-1)
{
cout<<"\n\nInvalid ";
}
else
for(int i=0; i<n; i++)
{
if(pos-1 ==i)
{
flag=0;
for(int j=pos-1; j<n; j++)
{
A[j]=A[j+1];
}

n--;
}
}

cout<<"\n\n\nArray formed : ";


for( i=0; i<n; i++)
cout<<A[i]<<" ";
cout<<"\n\n\nWant to delete again : ";
cin>>choice;
}while(choice=='y'||choice=='Y');
}
OUTPUT

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct student
{
int rollno;
float marks[3];
char name[25];
}
stu[5],temp;
void bubble(student []);
void main()
{
int i;
for(i=0;i<5;i++)
{
cout<<endl<<"Enter name of the student"<<i;
gets(stu[i].name);
cout<<"Enter marks for three subject";
for(int j=0;j<3;j++)
cin>>stu[i].marks[j];
cout<<endl<<"Enter the roll number of";puts(stu[i].name);
cin>>stu[i].rollno;
}
bubble(stu);
cout<<"SORTED DETAILS ARE";
for(i=0;i<5;i++)
{
cout<<endl<<"Name of the student"<<i;
puts(stu[i].name);
cout<<"marks for three subject";
for(int j=0;j<3;j++)
cout<<stu[i].marks[j];
cout<<endl<<"Enter the roll number of";puts(stu[i].name);
cout<<stu[i].rollno;
}
getch();
}
void bubble(student a[])
{
int j;
for(int i=0;i<5;i++)
{
for(j=0;j<4-i;j++)
{
if(a[j].rollno>a[j+1].rollno)
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

OUTPUT

/* Create a array storing these details of 5 employees .print the array. Create a menu that displays the following options
1. Query
2. Addname
Option 1 displays the total salary of the employee after taking the employee number from the user .(basic+hra)
option 2 adds the second name of all employees at the end of first name. Print the new array also.*/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class employee
{
int empno;
char name[25];
float hra,basicsal;
public:
void get();
void display();
void query();
void addname();
int eno()
{
return empno;
}
}ob[5];
int i=0;
void employee::query()
{
cout<<"Total Salary of";
puts(name);cout<<"is"<<basicsal+hra<<endl;
}
void employee::addname()
{
char n[50];
cout<<"Enter Second Name of"; puts(name);
gets(n);
strcat(name,n);
cout<<"Changed Name of employee "<<empno<<" is";puts(name);
}
void employee::get()
{
cout<<"\tEMPLOYEE"<<++i<<endl;
cout<<"\Enter the employee no:"<<endl;
cin>>empno;
cout<<"Enter name:"<<endl;
gets(name);
cout<<"Enter basic salary"<<endl;
cin>>basicsal;
cout<<"Enter hra:"<<endl;
cin>>hra;
}
void main()
{
clrscr();
char ans;
for(int i=0;i<5;i++)
ob[i].get();
int ch;
do{

cout<<"MENU"<<endl;
cout<<"\n1:QUERY \n 2: ADDNAME";
cin>>ch;
int k=0,pos=0;
cout<<"Enter the employee number where you want to make changes";
cin>>k;
for(i=1;i<=5;i++)
if(ob[i].eno()==k)
pos=i;
switch(ch)
{
case 1: ob[pos].query();
break;
case 2: ob[pos].addname();
break;
default: cout<<"Wrong choice";
}
cout<<"Do you want to continue(Y/N)";
cin>>ans;
}
while(ans=='y'||ans=='Y');
getch();
}

/* Write functions which perform the following task on the string (without using standard library functions): A) string
length b) comparison of two strings c) concat of two strings*/

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void findlength()
{
char str[30];
int l=0;
cout<<"\n Enter the string (size<=30) ";
gets(str);
while(str[l]!='\0')
{
l++;
}
cout<<"\n Length Of the given String is: "<<l<<endl;
}
void compare()
{
char str1[30], str2[30];
int l1=0,l2=0,i=0,flag=0;
cout<<"\n Enter the string1 (size<=30) ";
gets(str1);
while(str1[l1]!='\0')
{
l1++;
}
cout<<"\n Enter the string2 (size<=30) ";
gets(str2);
while(str2[l2]!='\0')
{
l2++;
}
if(l2!=l1)
{
cout<<"\n Strings are not Equal ";
}
else
{
for(i=0;i<l1;i++)
{
if(str1[i]!=str2[i])
{
flag=1;
break;
}
}
if(flag==1)
{
cout<<"\n Strings are not Equal ";
}
else
{
cout<<"\n Strings are Equal ";
}
}

}
void concat()
{
char str1[30], str2[30];
int l1=0,l2=0,i=0,flag=0;
cout<<"\n Enter the string1 (size<=30) ";
gets(str1);
while(str1[l1]!='\0')
{
l1++;
}
cout<<"\n Enter the string2 (size<=30) ";
gets(str2);
while(str2[l2]!='\0')
{
l2++;
}
for(i=0;i<l2;i++)
{
str1[l1+i]=str2[i];
}
str1[l1+l2]='\0';
cout<<"\n The concatenated String is: ";
puts(str1);
}
void main()
{
clrscr();
char ch,ans;
do
{
cout<<"Enter your choice \n \t1.Find length of string\n\t"
"2.Compare two Strings \n\t3.Concatenate two strings\n\t4.Exit \n";
cin>>ch;
switch(ch)
{
case '1':
findlength();
break;
case '2':
compare();
break;
case '3':
concat();
break;
default:cout<<"Wrong choice";
}
cout<<"Do You want to continue"<<endl;
cin>>ans;
}while(ans=='y'||ans=='Y');
getch();
}

OUTPUT

/* WAP using pointers to find the length of a string and print the reversed string .*/
#include<iostream.h>
#include<string.h>
#include<conio.h>
void main()
{
clrscr();
char *str= new char[256];
cout<<"\n\nEnter the string : ";
cin.getline(str,256);
cout<<"\n\nThe string length => "<<strlen(str);
cout<<"\n\nThe reverse string is : ";
char intermediate;
for(int j=strlen(str)-1,i=0; i<=strlen(str)/2; j--,i++)
{
intermediate = *(str+j);
*(str+j)=*(str+i);
*(str+i)=intermediate;
}
cout<<str;
getch();
}

/* WAP using pointers to find the smallest/ largest element in a dynamically created array.*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *array, smallest, largest, n;
cout<<"\n\nEnter the number of elements : ";
cin>>n;
array=new[n];
cout<<"\n\nEnter the elements : \n\n";
for(int i=0; i<n; i++)
cin>>array[i];
i=0;
cout<<"\n\nThe array formed is : \n\n";
while(i!=n)
{
cout<<array[i]<<" ";
i++;
}
smallest=array[0];
for(i=0; i<n; i++)
{
if(array[i]<=smallest)
smallest=array[i];
}
largest=array[0];
for(i=0; i<n; i++)
{
if(array[i]>=largest)
largest=array[i];
}
cout<<"\n\nThe smallest element is : "<<smallest<<"\n\nThe largest element is : "<<largest;
getch();
}

/* WAP using pointers to swap to integers*/


#include<iostream.h>
#include<conio.h>
void swap_using_pointers(int *, int *);
void main()
{
clrscr();
int a,b;
cout<<"\n\nEnter first integer : "; cin>>a;
cout<<"\n\nEnter second integer : "; cin>>b;
swap_using_pointers(&a,&b);
cout<<"\n\nNow value of first integer = "<<a;
cout<<"\n\nNow value of second integer = "<<b;
getch();
}
void swap_using_pointers(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}

/* WAP to count number of blank spaces in a text file*/


#include<fstream>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
using namespace std;
char f_name[30],fname[30];
void blankspace()
{
ifstream fin;
cout<<"Enter the file name"<<endl;
gets(f_name);
fin.open(f_name);
char ch;
int count=0;
while(!fin.eof())
{
fin.get(ch);
if(ch==' ')
count++;
}
cout<<"Number of blank spaces in file are "<<count;
fin.close();
}
int main()
{
cout<<"<<<<<<<<<---------------COUNT THE NUMBER OF BLANKSPACES------------>>>>>>>>>"<<endl;
blankspace();
getch();
}
OUTPUT

/* WAP to copy the content of one file to another.*/


#include<fstream>
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
int main()
{
ifstream readfile;
char name[30],name2[30];
cout<<"enter the name of file(with .txt) to be copied";
gets(name);
cout<<"Enter the file name to which content is to be copied";
gets(name2);
readfile.open(name,ios::in|ios::app);
ofstream newfile;
newfile.open(name2);
char ch;
while(!readfile.eof())
{
readfile.get(ch);
newfile<<ch;
}
readfile.close();
newfile.close();
cout<<"Completed copying the files";
getch();
return 0;
}

/*WAP to count number of alphabets in a text file.*/


#include<fstream>
#include<stdio.h>
#include<iostream>
#include<ctype.h>
using namespace std;
char f_name[30],fname[30];
void alphabets()
{
ifstream fin;
cout<<"Enter the file name"<<endl;
gets(f_name);
fin.open(f_name);
char ch;
int count=0;
while(!fin.eof())
{
fin.get(ch);
if(isalpha(ch))
count++;
}
cout<<"Number of alphabets in file are "<<count;
fin.close();
}
int main()
{
cout<<"<<<<<<<-----------------COUNT THE NUMBER OF ALPHABETS IN A FILE----------------->>>>>>>"<<endl;
alphabets();
}

/* WAP to count number of words in a text file*/


#include<fstream>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
using namespace std;
char f_name[30],fname[30];
void countwords_in_file()
{
ifstream fin;
cout<<"Enter the file name"<<endl;
gets(f_name);
fin.open(f_name);
char word[30];
int count=0;
while(!fin.eof())
{
fin>>word;
count++;
}
cout<<"Number of words in file are "<<count;
fin.close();
}
int main()
{
cout<<"<<<<<<<-----------------COUNT THE NUMBER OF WORDS IN A FILE----------------->>>>>>>"<<endl;
countwords_in_file();
}

/* WAP to count number of a word given by the user in a text file*/


#include<fstream>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
using namespace std;
char f_name[30],fname[30];
void countword_word()
{
ifstream fin;
cout<<"Enter the file name"<<endl;
gets(f_name);
fin.open(f_name);
char word[30];
char uword[30];
cout<<"Enter the word to be counted"<<endl;
gets(uword);
int count=0;
while(!fin.eof())
{
fin>>word;
if(strcmpi(word,uword)==0)
count++;
}
cout<<"Number of the word in file are "<<count;
fin.close();
}
int main()
{
cout<<"<<<<<<<-----------------COUNT THE NUMBER OF GIVEN WORDS IN A FILE----------------->>>>>>>"<<endl;
countword_word();
}
OUTPUT

/*WAP that maintains information about books(name,no,price) using classes and store it in file. The program
should provide the user with the following options.A) add a record B) search record
C) modify record*/
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
class book
{
char bname[30];
int bno;
float price;
public:
void input()
{
cout<<"\nEnter Book Name: ";
gets(bname);
cout<<"Enter BOOK No.: ";
cin>>bno;
cout<<"Enter Price";
cin>>price;
}
void setprice()
{
cout<<"\nEnter Price";
cin>>price;
}
void display()
{
cout<<"\nBook Name: "<<bname<<"\tBook No.: "<<bno<<"\tPrice: "<<price<<"\t";

}
int getbno()
{
return bno;
}
};
void main()
{
clrscr();
book b;
int n,i,j;
fstream ofile,afile;
char ch,ch1;
do
{
cout<<"\n\t1.Add records\n\t2.Search Records\n\t3.Modify Records\n\t4.Exit";
cin>>ch;
switch(ch)
{
case '1' :
ofile.open("bk.dat",ios::out|ios::binary);

case '2' :

cout<<"\nEnter no. of records to be Entered: ";


cin>>n;
for(i=0;i<n;i++)
{
b.input();
ofile.write((char*)&b,sizeof(book));
}
ofile.close();
break;
cout<<"\nEnter Book No. to be searched: ";
int bn,flag=0;
cin>>bn;
afile.open("bk.dat",ios::in);
while(afile)
{
afile.read((char *)&b,sizeof(book));
if(!afile)
break;
if (bn==b.getbno())
{
b.display();
flag=1;
break;
}
}
if(flag==0)
cout<<"\n No record Found";
afile.close();
break;

case '3' :
cout<<"\nEnter Book No. to be modified ";
int bn1,flag1=0,r=0;
cin>>bn1;
afile.open("bk.dat",ios::in|ios::out|ios::binary);
while(afile)
{
afile.read((char *)&b,sizeof(book));
if(!afile)
break;
if (bn1==b.getbno())
{
b.setprice();
afile.seekp(r*sizeof(b),ios::beg);
afile.write((char *)&b,sizeof(book));
flag1=1;
break;
}
r++;
}
if(flag1==0)
cout<<"\n No record Found";
afile.close();
break;

case '4' : exit(0);


}
cout<<"\n\t DO U want to continue ";

cin>>ch1;
}while(ch1=='Y'||ch1=='y');
getch();
}

/* WAP to maintain a telephone directory using data file(binary) which provides the user with following
options a) append record b) search record*/
# include <fstream.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
class telephone
{
char name[30];
char address[50];
double tno;
public :
void input()
{
cout<<"\n Enter the name ";
gets(name);
cout << "\n Enter address ";
gets(address);
cout<<"\n Enter the telephone number ";
cin>>tno;
}
void show()
{
cout << "\n Name "<<name;
cout << "\n Address "<<address;
}
double rt_tno()
{
return tno;
}
}tele;
// Function to append the records in file
void append()
{
ofstream tfile;
telephone tele;
tfile.open("tele.dat", ios :: app);
int n,i;
cout<< "Enter how many customers ";
cin>>n;
for (i =0; i<n ;i++)
{
tele.input();
tfile.write((char *)& tele,sizeof(tele));
}
tfile.close();
}
// Function to search a record in the file
void display()
{
ifstream tfile;

tfile.open("tele.dat",ios :: binary);
int no,flag;
flag = 0;
cout<< "\n Enter telephone number to be searched ";
cin>>no;
while(tfile)
{
tfile.read((char *)&tele , sizeof(tele));
if(!tfile)
break;
if (tele.rt_tno() == no)
{
tele.show();
flag = 1;
}
}
if (flag == 0)
cout<< "\n Record does not exist ";
}
void main()
{
clrscr();
int ch;
char ch1;
do
{
cout << "1. For append record ";
cout <<"\n2. For search ";
cout << "\n3. For exit";
cout<<"Enter your choice: ";
cin >> ch;
switch (ch)
{
case 1: append();
break;
case 2: display();
break;
case 3 : exit(0);
}
cout<<"\n\t DO U want to continue <Y/N>: ";
cin>>ch1;
}while(tolower(ch1)!='n');
getch();
}

/* WAP to maintain a record of sports(name,no,fees) in a file(binary) and provide the user with options to add,search and delete
record from file.*/
// Filename: \\PracticalList\P_16.CPP
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
class Sports{
char sname[30];
int sno;
float fees;
public:
void input()
{
cout<<"\nEnter sports Name: ";
gets(sname);
cout<<"Enter sports No.: ";
cin>>sno;
cout<<"Enter Fees: ";
cin>>fees;
}
void display()
{
cout<<"\nSports Name: "<<sname<<"\tSports No.: "<<sno<<"\tFees: "<<fees<<"\t";
}
int getsno()
{
return sno;
}
};
void main()
{
clrscr();
Sports s;
int n,i,j;
fstream ofile,afile;
char ch,ch1;
do
{
cout<<"\n\t1.Add records\n\t2.Search Records\n\t3.Delete Records\n\t4.Exit\n";
cout << "Enter your choice... ";
cin>>ch;
switch(ch)
{

case '1' :

case '2' :

ofile.open("Sport.dat",ios::out|ios::binary);
cout<<"\nEnter no. of records to be Entered: ";
cin>>n;
for(i=0;i<n;i++)
{
s.input();
ofile.write((char*)&s,sizeof(Sports));
}
ofile.close();
break;
cout<<"\nEnter Sports No. to be searched: ";
int sn,flag=0;
cin>>sn;
afile.open("Sport.dat",ios::in);
while(afile)
{
afile.read((char *)&s,sizeof(Sports));
if(!afile)
break;
cout << s.getsno();
if (sn==s.getsno())
{
s.display();
flag=1;
break;
}
}
if(flag==0)
cout<<"\n No record Found";
afile.close();
break;

case '3' :
cout<<"\nEnter Sports No. to be Deleted ";
int sn1,flag1=0;
cin>>sn1;
afile.open("Sport.dat",ios::in|ios::binary);
ofile.open("TSport.dat",ios::out|ios::binary);
while(afile)
{
afile.read((char *)&s,sizeof(Sports));
if(!afile)
break;
if (sn1==s.getsno())
{
flag1=1;
}
else
{
ofile.write((char *)&s,sizeof(Sports));
}
}
if(flag1==0)
cout<<"\n No record Found";
afile.close();
ofile.close();
afile.open("TSport.dat",ios::in|ios::binary);
ofile.open("Sport.dat",ios::out|ios::binary);
while(afile)
{
afile.read((char *)&s,sizeof(Sports));
ofile.write((char *)&s,sizeof(Sports));

}
afile.close();
ofile.close();
break;
case '4' : exit(0);
}
cout<<"\n\t DO U want to continue ";
cin>>ch1;
}while(ch1=='Y'||ch1=='y');
getch();
}

/* Given a binary file sports.dat containing records of the following structure type
STRUCT SPORTS { CHAR EVENT[20]; CHAR PARTICIPANT[10][30]; }
WAP to read this file and create another file called athletic.dat copying only those records where event name is athletic*/

#include<iostream>
#include<fstream>
#include<string.h>
#include<conio.h>
#include<stdio.h>
using namespace std;
struct sports {
char event[20];
char participants[10][30];
int no_of_participants;
} s[20], s2[20];
void copy(fstream &ob);
int i=0;
int main()
{
char choice;
fstream ob("sports.dat",ios::binary|ios::in|ios::out);
do
{
if(i>0)
cin.get();
cout<<"\n\nEnter the name of Event : ";
cin.getline(s[i].event,20);
cout<<"\n\nEnter the total number of participants in Event "<<s[i].event<<" : ";
cin>>s[i].no_of_participants;
cout<<"\n\nEnter the name of Praticipants : \n";
cin.get();
for(int j=0; j<s[i].no_of_participants; j++)
cin.getline(s[i].participants[j],30);
ob.write((char*)&s[i], sizeof(sports));
cout<<"\n\n\nWant to Enter Details of Another Event (Y/N) : ";
cin>>choice;

i++;
}while(choice=='y'||choice=='Y');
cout<<"\n\n\n\n\n*********************************************************\n\n";
copy(ob);
cout<<"\n\n*****************************************************************************\n\n\n";
getch();
}
void copy(fstream &o)
{
sports s[20];
o.seekg(0);
ofstream file;
file.open("athletic.dat",ios::binary);
file.seekp(0);
int j;
int c=0;
while(o)
{
o.read((char*)&s[c], sizeof(sports));
if(strcmp("athletic",s[c].event)==0)
{
file.write((char*)&s[c], sizeof(sports));
break;
}
c++;
}
o.close();
file.close();
sports sp;
ifstream oo;
oo.open("athletic.dat",ios::binary);
while(oo)
{
oo.read((char*)&sp, sizeof(sports));
}
cout<<"\n\nThe Records of file are : \n\n";
cout<<"\n\nEvent = "<<sp.event;
cout<<"\n\n\n\nThe Participants are : \n\n";
for(int i=0; i<sp.no_of_participants; i++)
{
cout<<sp.participants[i]<<"\n\n";
}
oo.close();
}

/*WAP to perform the given operations on linked list.a)insertion b) deletion c)traversal d) search*/
#include<iostream>
#include<stdlib.h>
#include<process.h>
using namespace std;
struct node
{
int info;
node *next;
}*start,*newptr,*save,*ptr;
node *create(int);
void insert_beg(node*);
void insert_end(node*);
void display(node*);
void del_beg();
void del_end();
void del(int);
int search(int);
int main()
{
start=NULL;
int ele,mn;
char ch='n';

do
{
system("cls");
cout<<"1: Insert a node"<<endl;
cout<<"2: Delete a node"<<endl;
cout<<"3: Traverse Linked list"<<endl;
cout<<"4: Search Element"<<endl;
cout<<"5: Exit"<<endl;
cout<<"Enter your choice"<<endl;
cin>>mn;
switch(mn)
{
case 1: int m;
system("cls");
cout<<"-----Insertion-----"<<endl;
cout<<"1: Insertion At Beginning"<<endl;
cout<<"2: Insertion At End"<<endl;
cout<<"Enter your choice"<<endl;
cin>>m;
cout<<"Enter information to be inserted"<<endl;
cin>>ele;
newptr=create(ele);
switch(m)
{
case 1: node *e;
insert_beg(newptr);
display(start);
break;
case 2: insert_end(newptr);
display(start);
break;
default: cout<<"wrong choice";
break;
}
break;
case 2:int n;
system("cls");
cout<<"-----Deletion-----"<<endl;
cout<<"1: Deletion At Beginning"<<endl;
cout<<"2: Deletion At End"<<endl;
cout<<"3: Deletion of an element"<<endl;
cout<<"Enter your choice"<<endl;
cin>>n;
switch(n)
{
case 1:del_beg();
display(start);
break;
case 2: del_end();
display(start);
break;
case 3:system("cls");
cout<<"Enter element to be deleted";
cin>>ele;
del(ele);
display(start);
break;
default: cout<<"wrong choice";
break;
}
break;

case 3: display(start);
break;
case 4: system("cls");
int ele,pos;
cout<<"enter the element to be searched"<<endl;
cin>>ele;
pos=search(ele);
if(pos==-1)
cout<<"element not found"<<endl;
else
{
cout<<"element found at "<<pos<<endl;
}
break;
case 5: exit(0);
default: cout<<"Wrong choice";
break;
}
cout<<"Do you want to continue";
cin>>ch;
}while(ch=='y'||ch=='Y');
}
node * create(int n)
{
ptr=new node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}
void insert_beg(node *np)
{
if(start==NULL)
start=np;
else
{
save=start;
start=np;
np->next=save;
}
}
void display(node *np)
{
if(np==NULL)
{
cout<<"empty list"<<endl;
}
else{
while(np!=NULL)
{
cout<<np->info<<"->";
np=np->next;
}
cout<<"\n";
}
}
void insert_end(node* n)
{
node *e;

if(start==NULL)
start=n;
else
{
e=start;
while(e->next!=NULL)
{
e=e->next;
}
e->next=n;
}
}
void del_beg()
{
if(start==NULL)
{
cout<<"No element is present in the list"<<endl;
}
else
{
ptr=start;
start=start->next;
delete ptr;
}
}
void del_end()
{
node *e;
if(start=NULL)
cout<<"No element is present in the list"<<endl;
else
{
ptr=start;
while((ptr->next)->next!=NULL)
{
ptr=ptr->next;
}
e=ptr->next;
ptr->next=NULL;
delete e;
}
}
void del(int er)
{
node *e,*f;
e=start;
if(e->info==er)
{
start=e->next;
delete e;
}
else
{
while(e->next->info!=er)
{
e=e->next;
}
f=e->next;

e->next=f->next;
delete f;
}
}
int search(int e)
{
ptr=start;
int counter=1;
while (ptr!=NULL && ptr->info!=e){
ptr = ptr->next;
counter++;
}
if (ptr != NULL && ptr->info==e) {
return counter;
}
}

/* WAP to implement stack using arrays.*/


#include<iostream>
#include<stdlib.h>
using namespace std;
int push(int [],int&,int);
void display(int [],int);
int pop(int[],int&);
const int size=20;
int main()
{
int stack[size],item,top=-1,res,c;
char ch='n';
do
{
cout<<"1: PUSH AN ELEMENT"<<endl;
cout<<"2: POP AN ELEMENT"<<endl;
cout<<"3: DISPLAY"<<endl;
cout<<"4: EXIT"<<endl;
cout<<"Enter your choice";
cin>>c;
switch(c)
{
case 1: system("cls");
cout<<"------PUSHING AN ELEMENT IN STACK---------"<<endl;
cout<<"enter the element";
cin>>item;
res=push(stack,top,item);
if(res==-1)
cout<<"OVERFLOW"<<endl;
cout<<"The stack now is"<<endl;
display(stack,top);
break;
case 2: system("cls");
cout<<"------POPING AN ELEMENT IN STACK--------"<<endl;
res=pop(stack,top);
if(res==-1)
{
cout<<"UNDERFLOW"<<endl;
}
else
{
cout<<"THE ELEMENT DELETED IS:"<<res<<endl;
cout<<"THE STACK NOW IS"<<endl;
display(stack,top);
}
break;
case 3:system("cls");
cout<<"THE ELEMENTS OF SATCK ARE"<<endl;
display(stack,top);
break;
case 4: exit(0);
default: cout<<"wrong choice"<<endl;
break;
}
cout<<"DO YOU WANT TO CONTINUE"<<endl;
cin>>ch;
}while(ch=='y'||ch=='Y');
}
int push(int stack[],int &top,int ele)
{
if(top==size-1)
return -1;
else

{
top++;
stack[top]=ele;
}
return 0;
}
int pop(int stack[],int &top)
{
int ret;
if(top==-1)
return-1;
else
{
ret=stack[top];
top--;
}
return ret;
}
void display(int stack[],int top)
{
if(top==-1)
return;
cout<<stack[top]<<"<--"<<endl;
for(int i=top-1;i>=0;i--)
{
cout<<stack[i]<<endl;
}
}

/* WAP to implement stacks using linked list.*/


#include<iostream>
#include<stdlib.h>
#include<process.h>
using namespace std;
struct node
{
int info;
node *next;
}*top,*newptr,*save,*ptr;
node *create(int);
void push(node*);
int pop();
void display(node *);
int main()
{
top=NULL;
int ele,mn;
char ch='n';
do
{
system("cls");
cout<<"1: PUSH"<<endl;
cout<<"2: POP"<<endl;
cout<<"3: DISPLAY"<<endl;
cout<<"Enter your choice"<<endl;
cin>>mn;
switch(mn)
{
case 1:
system("cls");
cout<<"-----PUSHING AN ELEMENT-----"<<endl;
cout<<"Enter the element to be pushed"<<endl;
cin>>ele;
newptr=create(ele);
push(newptr);
display(top);
break;
case 2: int r;
system("cls");
cout<<"-----POPING AN ELEMENT-----"<<endl;
r=pop();
if(r==-1)
cout<<"empty stack"<<endl;
else
cout<<"the element poped is---->>"<<r<<endl;
display(top);
break;
case 3: display(top);
break;
case 4: exit(0);
default: cout<<"WRONG CHOICE"<<endl;
break;
}
cout<<"DO YOU WANT TO CONTINUE"<<endl;
cin>>ch;
}while(ch=='y'||ch=='Y');
}
node * create(int n)
{
ptr=new node;
ptr->info=n;
ptr->next=NULL;

return ptr;
}
void push(node *np)
{
if(top==NULL)
top=np;
else
{
save=top;
top=np;
np->next=save;
}
}
int pop()
{
int r=-1;
if(top==NULL)
cout<<"--UNDERFLOW--"<<endl;
else
{
ptr=top;
r=top->info;
top=top->next;
delete ptr;
}
return r;
}
void display(node *np)
{
if(np==NULL)
cout<<"EMPTY STACK"<<endl;
while(np!=NULL)
{
cout<<np->info<<"-->";
np=np->next;
}
cout<<"\n";
}
OUTPUT

/* WAP to implement queues using array.*/


#include<iostream>
#include<stdlib.h>
using namespace std;
int dequeue(int []);
int enqueue(int [],int);
void display(int [],int,int);
const int size=20;
int queue[size],front=-1,rear=-1;
int main()
{
int item,res,c;
char ch='n';
system("cls");
do{
cout<<"----QUEUE------"<<endl;
cout<<"1: INSERT AN ELEMENT IN QUEUE(ENQUEUE)"<<endl;
cout<<"2: DELETE AN ELEMENT FROM QUEUE(DEQUEUE)"<<endl;
cout<<"3: DISPLAY"<<endl;
cout<<"4: EXIT"<<endl;
cout<<"ENTER YOUR CHOICE"<<endl;
cin>>c;
switch(c)
{
case 1: system("cls");
cout<<"------ENQUEUE-------"<<endl;
cout<<"ENTER THE ELEMENT TO BE INSERTED";
cin>>item;
res=enqueue(queue,item);
if(res==-1)
cout<<"--OVERFLOW--"<<endl;
cout<<"----THE QUEUE IS----"<<endl;
display(queue,front,rear);
break;
case 2: system("cls");
cout<<"------DEQUEUE-------"<<endl;
res=dequeue(queue);
if(res==-1)
cout<<"--UNDERFLOW--"<<endl;
cout<<"----THE QUEUE IS----"<<endl;
display(queue,front,rear);
break;
case 3: system("cls");
cout<<"----THE QUEUE IS----"<<endl;
display(queue,front,rear);
break;
case 4: exit(0);
default: cout<<"WRONG CHOICE"<<endl;
break;
}
cout<<"DO YOU WANT TO CONTINUE";
cin>>ch;
}while(ch=='y'||ch=='Y');
}
int enqueue(int queue[],int ele)
{
if(rear==size-1)
return -1;
else if(rear==-1)
{
front=rear=0;
queue[rear]=ele;

}
else
{
rear++;
queue[rear]=ele;
}
return 0;
}
int dequeue(int queue[])
{
int ret;
if(front==-1)
return -1;
else
{
ret=queue[front];
if(front==rear)
front=rear=-1;
else
front++;
}
return ret;
}
void display(int queue[],int front,int rear)
{
if(front==-1)
return;
for(int i=front;i<rear;i++)
{
cout<<queue[i]<<"<-";
}
cout<<queue[rear]<<endl;
}

/* WAP to implement queues using linked list*/


#include<iostream>
#include<stdlib.h>
#include<process.h>
using namespace std;
struct node
{
int info;
node *next;
}*front,*rear,*newptr,*save,*ptr;
node *create(int);
void enqueue(node*);
int dequeue();
void display(node *);
int main()
{
front=rear=NULL;
int ele,mn;
char ch='n';
do
{
system("cls");
cout<<"1: ENQUEUE"<<endl;
cout<<"2: DEQUEUE"<<endl;
cout<<"3: DISPLAY"<<endl;
cout<<"Enter your choice"<<endl;
cin>>mn;
switch(mn)
{
case 1:
system("cls");
cout<<"-----ENQUEUE-----"<<endl;
cout<<"Enter the element to be inserted"<<endl;
cin>>ele;
newptr=create(ele);
enqueue(newptr);
display(front);
break;
case 2: int r;
system("cls");
cout<<"-----DEQUEUE-----"<<endl;
r=dequeue();
if(r==-1)
cout<<"----UNDERFLOW"<<endl;
else
cout<<"the element removed is---->>"<<r<<endl;
display(front);
break;
case 3: display(front);
break;
case 4: exit(0);
default: cout<<"WRONG CHOICE"<<endl;
break;
}
cout<<"DO YOU WANT TO CONTINUE"<<endl;
cin>>ch;
}while(ch=='y'||ch=='Y');
}
node * create(int n)
{
ptr=new node;

ptr->info=n;
ptr->next=NULL;
return ptr;
}
void enqueue(node *np)
{
if(front==NULL)
{
front=rear=np;
}
else
{
rear->next=np;
rear=np;
}
}
int dequeue()
{
int r=-1;
if(front==NULL)
return r;
else
{
ptr=front;
front=front->next;
r=ptr->info;
delete ptr;
}
return r;
}
void display(node *np)
{
while(np!=NULL)
{
cout<<np->info<<"-->";
np=np->next;
}
}

/* WAP to implement circular queues.*/


#include<iostream>
#include<stdlib.h>
#include<conio.h>
using namespace std;
int enqueue(int[],int);
int dequeue(int[]);
void display(int [],int,int);
const int size=5;
int queue[size],rear=-1,front=-1;
int main()
{
int item, res,c;
char ch='n';
do
{
cout<<"----CIRCULAR QUEUE----"<<endl;
cout<<"1: ENQUEUE"<<endl;
cout<<"2: DEQUEUE"<<endl;
cout<<"3: DISPLAY"<<endl;
cout<<"4. EXIT"<<endl;
cout<<"ENTER YOU CHOICE"<<endl;
cin>>c;
switch(c)
{
case 1: system("cls");
cout<<"-----ENQUEUE-----"<<endl;
cout<<"Enter the element to be enqueued"<<endl;
cin>>item;
res=enqueue(queue,item);
if(res==-1)
cout<<"---OVERFLOW---"<<endl;
cout<<"The Queue is"<<endl;
display(queue,front,rear);
break;
case 2: system("cls");
cout<<"-----DEQUEUE-----"<<endl;
res=dequeue(queue);
if(res==-1)
cout<<"---UNDERFLOW---"<<endl;
else
{
cout<<"the element deleted is-->>"<<res<<endl;
cout<<"The Queue is"<<endl;
display(queue,front,rear);
}
break;
case 3: system("cls");
display(queue,front,rear);
break;
case 4: exit(0);
default: cout<<"WRONG CHOICE";
break;
}
cout<<"DO YOU WANT TO CONTINUE??"<<endl;
cin>>ch;
}while(ch=='y'|| ch=='Y');
}
int enqueue(int queue[],int e)
{
if((front==0&&rear==size-1)||(front==rear+1))

return -1;
else if(rear==-1)
front=rear=0;
else if (rear==size-1)
rear=0;
else
rear++;
queue[rear]=e;
return 0;
}
int dequeue(int queue[])
{
int ret;
if(front==-1)
return -1;
else
{
ret=queue[front];
if(front==rear)
rear=front=-1;
else if(front==size-1)
front=0;
else
front++;
}
return ret;
}
void display(int queue[],int front,int rear)
{
int i=0;
if(front==-1)
cout<<"empty queue"<<endl;
else if(rear>=front)
{
for(i=0;i<front;i++)
cout<<"-";
cout<<">>>";
for(i=front;i<rear;i++)
cout<<queue[i]<<"<-";
cout<<queue[rear]<<"<<<"<<endl;
}
else
{
for(i=0;i<rear;i++)
cout<<queue[i]<<"<-";
cout<<queue[rear]<<"<<<";
for(;i<front;i++)
cout<<"-";
cout<<">>>";
for(i=front;i<size;i++)
cout<<queue[i]<<"<-";
cout<<"circular";
}
}
Output

Anda mungkin juga menyukai