Sie sind auf Seite 1von 70

Sr

TOPIC
.
N
o.
1. Implementation of
Polymorphism
2.
Implementation of:

3.
4.
5.
6.
7.
8.
9.
10
.
11
.

a)
Single inheritance
b)
Multiple inheritance
c)
Multilevel
inheritance
d)
Hierarchical
inheritance
Addition of matrices
Multiplication of matrices
Insertion operation of an
array
Deletion operation of an
array
Sequential search in an
array
Binary search in an array
Sort an array using bubble
sort
Sort an array using
selection sort
Sort an array using
insertion sort
1

DAT TEACH
E
ERs
SIGN

12 Merging two arrays using


merge sort
13
Read a text file and count
.
the total no. of :
a)
b)
c)
d)

Uppercase letters
Lowercase letters
Digits
Special symbols

14
Read text file and copy
.
data in two text files
simultaneously on the
basis of :

15
.
16
.
17
.
18
.

a)
All the uppercase
letters and digits in the
text file
b)
All the lowercase
letters and special
symbols
Operate all data file
handling operations for a
binary file
Perform push and pop
operation in linear stack
Perform push and pop
operation in linked stack
Perform insertion and
deletion operation in linear

queue
19 Perform insertion and
.
deletion operation in
linked queue
20 SQL Queries
.

1. Implementation of polymorphism
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define pi 3.14
class fn
{
public:
void area(int); //circle
void area(int,int); //rectangle
void area(float ,int,int); //triangle
void area();
};
void fn::area(int a)
{cout<<"Area of Circle:"<<pi*a*a;

//Function 1}

void fn::area(int a,int b)


{ cout<<"Area of rectangle:"<<a*b;

//Function 2}

void fn::area(float t,int a,int b)


{ cout<<"Area of cuboid:"<<2*(a*b+b*t+a*t); } //Function 3
void fn::area()

//Function 4

{cout<<"\n1.Rectangle=l*b\n2.Cuboid=2*(l*h+h*b+b*l)\n3.Circle=3.14*r2";
}
void main()

{ int ch;
int a,b,r;
char ch1='y';
clrscr();
fn obj;
cout<<"\n\t\tFunction Overloading";
cout<<"\n1.Area of Circle\n2.Area of Rectangle\n3.Area of
Cuboid\n4.Formulas\n";
while(ch1=='y'||ch1=='Y')
{cout<<"Enter your Choice:";
cin>>ch;
switch(ch)
{ case 1: cout<<"Enter Radius of the Circle:";
cin>>r;
obj.area(r);

//Calls Function 1

break;
case 2: Cout<<"Enter Sides of the Rectangle:";
cin>>a>>b;
obj.area(a,b);

//Calls Function 2

break;
case 3: cout<<"Enter Sides of the Cuboid:";
cin>>a>>b;
obj.area(0.5,a,b);

//Calls Function 3

break;
case 4: cout<<"Formulas:";
obj.area();

//Calls Function 4

break;
}
cout<<"\t\tWant to enter more???";
cin>>ch1;
}
getch();}

Output

2. PROGRAM TO IMPLEMENT THE CONCEPT


A) SINGLE INHERITANCE
B) MULTIPLE INHERITANCE
C) MULTILEVEL INHERITANCE
D) HIERARCHIAL INHERITANCE
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
class A

//Base class

{
int a1,a2;
public:
void getdata()
{
cout<<"\nEnter value of a1 and a2\n";
cin>>a1>>a2;
}
void putdata()
{
cout<<"a1"<<a1<<"a2"<<a2<<endl;
}};
class A1

//Base class

int a11,a21;
public:
void getdata1()
{
cout<<"\nEnter value of a11 and a21\n";
cin>>a11>>a21;
}
void putdata1()
{
cout<<"\na11"<<a11<<"a21"<<a21<<endl;
}};
class B: public A
{
int b1,b2;
public:
void indata()
{
cout<<"\n";
cin>>b1>>b2;
}
void outdata()
{
cout<<"\nb1"<<b1<<"b2"<<b2<<endl;

}};
class C: public B
{
int c1,c2;
public:
void input()
{
cout<<"\nEnter the value of c1 and c2\n";
cin>>c1>>c2;
}
void output()
{
cout<<"c1"<<c1<<"c2"<<c2;
}};
class D: public A
{
int d1,d2;
public:
D()
{
d1=0;
d2=0;
}};

class E: public A,public A1


{
int e1,e2;
public:
E()
{
e1=0;
e2=0;
}};
void main()
{
clrscr();
E obj1;

//Multiple Inheritance

D obj2; //Hierarchical Inheritance ,single inheritance


C obj3;

//Multilevel Inheritance

int i;
cout<<"1.Single inheritance\n";
cout<<"2.Multiple inheritance\n";
cout<<"3.Multilevel inheritance\n";
cout<<"4.Hierarchial inheritance\n";
cin>>i;
switch(i)
{

10

case 1:cout<<"Accessing class A through object of B\n";


obj2.getdata();
obj2.putdata();

//member functions of class A

break;
case 2:cout<<"\nAccessing class A and A1(Base classes) by object of E\n";
obj1.getdata();
obj1.getdata1();
obj1.putdata();

//member functions of class A and A1;

obj1.putdata1();
break;
case 3:cout<<"Accessing Base class A by object of C";
obj3.getdata();
cout<<"\nAccessing subclass B by object of C";
obj3.indata();
cout<<"\nThe values of class A and B are:\n";
obj3.putdata();
obj3.outdata();
break;
case 4:cout<<"Accessing class A by object of its suclass B\n";
obj3.getdata();
obj3.putdata();
cout<<"\nAccessing class A by object of its subclass D\n";
obj2.getdata();

11

obj2.putdata();
cout<<"\nAccessing class A by object of its subclass E\n";
obj1.getdata();
obj1.putdata();
break; }
getch();
}

OUTPUT

12

13

3. Addition of matrices
#include<iostream.h>
#include<conio.h>
void main()
{ int a[5][5],b[5][5],c[5][5],i,k,j,l,n;
cout<<"enter the two arrays:";
cout<<"enter size of arrays:";
cin>>n;
cout<<"1.:";
for(i=0;i<n;++i)
{ for(j=0;j<n;++j)
{cin>>a[i][j];
}
}
cout<<"2.:";
for(i=0;i<n;++i)
{ for(j=0;j<n;++j)
{cin>>b[i][j];
}
}
cout<<"Addition of matrices:";
for(k=0;k<n;++k)
{ for(l=0;l<n;++l)

14

{ c[k][l]=a[k][l] + b[k][l];
}
}
cout<<"Result:\n";
for(i=0;i<n;++i)
{ for(j=0;j<n;++j)
{cout<<c[i][j];
cout<<"\t";
}
cout<<"\n"
}
getch(); }

4. Multiplication of matrices

15

#include<iostream.h>
#include<conio.h>

void main()
{ int a[10][10] , b[10][10] ,c[10][10],m,n,n1,m1,i,j,i1;

cout<<"enter the no. of row's in array 1.:";


cin>>m;
cout<<"enter the no. of column's in array 1.:";
cin>>n;
cout<<"enter the no. of row's in array 2.:";
cin>>m1;
cout<<"enter the no. of column's in array 2.:";
cin>>n1;
if(n==m1)
cout<<"array can be multiplied";
cout<<"enter the array 1.:";
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ cin>>a[i][j];
};
};
cout<<"enter the array 2.:";

16

for(i=0;i<m1;i++)
{ for(j=0;j<n1;j++)
{ cin>>b[i][j];
};
};
for(i=0;i<m;++i)
for(j=0;j<n1;++j)
{ for(i1=0;i1<m1;++i1)
{ c[i][j]=c[i][j]+(a[i][i1]*b[i1][j]);
cout<<"\t"; }
cout<<"\n"; }
cout<<" the product of the two matrix :";
for(i=0;i<m;i++)
{ cout<<"\n";
for(j=0;j<n1;j++)
{ cout<<c[i][j];
cout<<"\t"; }}
getch();}

17

18

5. Insertion operation in an array


#include<iostream.h>
#include<conio.h>
#include<process.h>
int insert(int[],int,int);
void main()
{int a[15],i,n,x,s;
cout<<"Enter the size of the array :";
cin>>n;
cout<<"Enter the array:";
for(i=0;i<n;++i)
{ cin>>a[i];
}
char ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"Enter the item to be inserted :";
cin>>x;
if(n==15)
{ cout<<"overflow!!!";
}
s=insert(a,n,x);
for(i=n;i>s;i--)
{ a[i]=a[i-1];

19

}
++n;
a[s]=x;
cout<<"Want to insert more??";
cin>>ch;
}
cout<<"The array now is:";
for(i=0;i<n;++i)
{ cout<<a[i]<<"\t";
}
getch();
}
int insert(int b[],int size,int item)
{ int p,i;
if(item<b[0])
p=0;
else
{for(i=0;i<size-1;++i)
{ if(b[i]<=item&&b[i+1]>item)
{p=i+1;
break;
}
}

20

if(i==size-1)
p=size;
}
return p;
}

6. Deletion operation in an array


#include<iostream.h>

21

#include<conio.h>
int search(int[], int,int);
void main()
{ int a[15],n,x,i,s;
cout<<"Enter the size of array :";
cin>>n;
cout<<"Enter the array:";
for(i=0;i<n;++i)
{ cin>>a[i]; }
char ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"Enter the item to be deleted :";
cin>>x;
s=search(a,n,x);
if(s==-1)
{cout<<"Item not found...";
}
else
for(i=s;i<n;i++)
{a[i]=a[i+1];}
n-=1;
cout<<"Want to delete more items???";
cin>>ch;}

22

cout<<"After deletion the array is:";


for(i=0;i<n;++i)
{ cout<<a[i];}
getch();
}
int search(int b[] ,int size ,int item)
{int g,l,m;
g=0;
l=size-1;
while(g<=l)
{m=(g+l)/2;
if(item==b[m])
return m;
else if(item >b[m])
g=m+1;
else l=m-1;
}return -1 ;}

23

24

7. Sequential search in an array


#include<iostream.h>
#include<conio.h>
int search(int[],int ,int);
void main()
{int a[15],n,x,i,s;
cout<<"Enter the size of the array :";
cin>>n;
cout<<"Enter the array :";

25

for(i=0;i<n;++i)
{cin>>a[i];
}
cout<<"Enter the element to be searched for:";
cin>>x;
s=search(a,n,x);
if(s==-1)
{ cout<<"The element could not be found !!!!";
}
cout<<"The element is at this position:"<<s;
getch();
}
int search(int b[],int size , int item)
{int i;
for(i=0;i<size;++i)
{ if(b[i]==item)
return i;}
return -1;
}

26

8. Binary search in an array


#include<iostream.h>
#include<conio.h>
int search(int[], int,int);
void main()
{ int a[15],n,x,i,s;

27

cout<<"Enter the size of array :";


cin>>n;
cout<<"Enter the array:";
for(i=0;i<n;++i)
{ cin>>a[i]; }
cout<<"Enter the item to be searched :";
cin>>x;
s=search(a,n,x);
if(s==-1)
{cout<<"Item not found...";
}
else
cout<<"Item found at this position:"<<s+1;
getch();
}
int search(int b[] ,int size ,int item)
{int g,l,m;
g=0;
l=size-1;
while(g<=l)
{m=(g+l)/2;
if(item==b[m])
return m;

28

else if(item >b[m])


g=m+1;
else l=m-1;
}
return -1;
}

9. Sort an array using bubble sort


#include<iostream.h>
#include<conio.h>
void bsort(int [],int);
void main()
{ int a[15],n,x,i;
cout<<"Enter the size of array:";

29

cin>>n;
cout<<"Enter the array:";
for(i=0;i<n;++i)
{cin>>a[i];
}
bsort(a,n);
cout<<"The sorted array is:";
for(i=0;i<n;++i)
{ cout<<a[i];
}
getch();
}
void bsort(int b[],int size)
{int i,j,t,c=0,m;
for(i=0;i<size;++i)
{ for(j=0;j<(size-1);++j)
{ if(b[j]>b[j+1])
{ t=b[j+1];
b[j+1]=b[j];
b[j]=t;
}
}
cout<<"Array after iteration "<<++c<<"\t";

30

for(m=0;m<size;++m)
{cout<<b[m];
}
cout<<"\n";
}
}

10.

Sort an array using selection sort

#include<iostream.h>
#include<conio.h>
void selsort(int[],int);
void main()
{

clrscr();
int ar[50],item,n,index;

31

cout<<"How many elements do you want to create array with?


(max.50)...";
cin >>n;
cout<<"\nEnter array elements...\n";
for(int i=0;i<n;i++)
cin >>ar[i];
selsort(ar,n);
cout<<"\n\nThe sorted array is as shown below...\n";
for(i=0;i<n;i++)
cout<<ar[i]<<" ";
cout<<endl;
getch();
}
void selsort(int ar[],int size)
{

int small,tmp,pos;
for(int i=0;i<size;i++)
{

small=ar[i];
for(int j=i+1;j<size;j++)
{

if(ar[j]<small)
{

small= ar[j];
pos=j;

}
}

32

tmp=ar[i];
ar[i]=ar[pos];
ar[pos]=tmp;
}
cout<<endl;
}

11.

Sort an array using insertion sort

#include<iostream.h>
#include<conio.h>
#include<limits.h>
void inssort(int[],int);
void main()
{

clrscr();

33

int ar[50],item,n,index;
cout<<"How many elements do you want to create array with?
(max.50)...";
cin >>n;
cout<<"\nEnter array elements...\n";
for(int i=1;i<=n;i++)
cin >>ar[i];
inssort(ar,n);
cout<<"\n\nThe sorted array is as shown below...\n";
for(i=1;i<=n;i++)
cout<<ar[i]<<" ";
cout<<endl;
getch();
}
void inssort(int ar[],int size)
{

int tmp,j;
ar[0]=INT_MIN;
for(int i=1;i<=size;i++)
{

tmp=ar[i];
j=i-1;
while(tmp<ar[j])
{

ar[j+1]=ar[j];
j--;

34

}
ar[j+1]=tmp;
cout<<endl;
}
}

35

12.

Merging an array using merge sort

#include<iostream.h>
#include<conio.h>
void merge(int[],int,int[],int,int[]);
void main()
{

clrscr();
int A[50],B[50],C[50],MN=0,M,N;

cout<<"How many elements do you want to create first array with?


(max. 50)...";
cin >>M;
cout<<"\nEnter first array's elements [ascending]...\n";
for(int i=0;i<M;i++)
{

cin >>A[i];}

cout<<"How many elements do you want to create second array with?


(max. 50)...";
cin >>N;
MN=M+N;
cout<<"\nEnter first array's elements [descending]...\n";
for(i=0;i<N;i++)
{

cin >>B[i];}

merge(A,M,B,N,C);
cout<<"\n\nThe merged array is as shown below...\n";
for(i=0;i<MN;i++)

36

cout<<C[i]<<" ";}

cout<<endl;
getch();
}
void merge(int A[], int M, int B[], int N, int C[])
{

int a,b,c;
for(a=0,b=N-1,c=0 ; a<M && b>=0;)
{

if(A[a]<=B[b])
{

C[c++]=A[a++];

}
else
{

C[c++]=B[b--];}

}
if(a<M)
{

while(a<M)
{

C[c++]=A[a++];}}

else
{

while(b>=0)
{

C[c++]=B[b--];}}}

37

38

13.
a)
b)
c)
d)

Read a text file and count total number of:


Uppercase letters
Lowercase letters
Digits
Special symbols

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<ctype.h>
void main()
{

clrscr();
char ch;
ofstream of("text1.txt");
for(int i=1;i<=5;i++)
{

cout<<"Enter a character : " ;


cin >>ch;
of<<ch;

}
of.close();
ifstream if1("text1.txt");
int u=0,l=0,d=0,s=0;
if1.seekg(0);
cout<<"\n";
for(i=1;i<=5;i++)

39

if1.get(ch);
if(isupper(ch))
u++;
else if(islower(ch))
l++;
else if(isdigit(ch))
d++;
else
s++;
cout<<"Character "<<i<<" = "<<ch;
cout<<"\n";

}
if1.close();
cout<<"Total Upper value = "<<u;
cout<<"\nTotal lower value = "<<l;
cout<<"\nTotal digit value = "<<d;
cout<<"\nTotal special symbol value = "<<s;
getch();}

40

41

14. Read a text file and copy the data in text files simultaneously
on the basis of:
a) All the uppercase letters and digits in the text file
b) All the lowercase letters and special symbols
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<ctype.h>
void main()
{

clrscr();
char ch;
ofstream of("text1.txt");
for(int i=1;i<=7;i++)
{

cout<<"Enter a character : ";


cin >>ch;
of << ch;

}
of.close();
ifstream if1("text1.txt");
ofstream of1("ud.txt");
ofstream of2("ls.txt");

int u=0,l=0;
if1.seekg(0);

42

cout<<"\n";
for(i=1;i<=7;i++)
{
if1.get(ch);
if(isupper(ch)||isdigit(ch))
{

of1<<ch;
u++;

}
else
{

of2<<ch;
l++;

}
cout<<"Character "<<i<<" = "<<ch;
cout<<"\n";
}
if1.close();
of1.close();
of2.close();
cout<<"total upper and digit value = "<<u;
cout<<"\nTotal lower and special symbol value = "<<l;
ifstream if2("ud.txt");
ifstream if3("ls.txt");
cout<<"\n\nContents of the file1\n";

43

for(i=1;i<=u;i++)
{

if2.get(ch);
cout<<"\t"<<ch;}

cout<<"\n\nContents of the file\n";


for(i=1;i<=l;i++)
{

if3.get(ch);
cout<<"\t"<<ch;}

getch();}

15.

Operate all data file handling operations for a binary file

44

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
int wcnt=0;
class student
{
public:
char name[80];
int rno;
void getdata()
{
cout<<"Enter the roll no.";
cin>>rno;
cout<<"\nEnter the name: ";
gets(name);
}
void putdata()
{
cout<<"\n\nRoll no: "<<rno;
cout<<"\nName: "<<name;
}
}s;

45

void main()
{
void rfile();
void afile();
void mfile();
void dfile();
char ans='y';
int choice=0;
while(ans=='y')
{
clrscr();
cout<<"Enter your choice"<<"\n";
cout<<"\n1.read file";
cout<<"\n2.append file";
cout<<"\n3.modify in file";
cout<<"\n4.delete from file";
cout<<"\nEnter your choice: ";
cin>>choice;
if(choice==1)
rfile();
if(choice==2)
afile();
if(choice==3)

46

mfile();
if(choice==4)
dfile();
cout<<"\n\nDo you want to continue? ";
cin>>ans;
}
getch();
}
void rfile()
{

ifstream fr;
fr.open("temp",ios::in|ios::binary);
while(fr)
{
fr.read((char*)&s,sizeof(s));
s.putdata();
}
fr.close();
}
void afile()
{
ofstream fr;

47

fr.open("temp",ios::app|ios::binary);
s.getdata();
fr.write((char*)&s,sizeof(s));
fr.close();
cout<<"Record added successfuly";
}
void mfile()
{
fstream fr;
fr.open("temp",ios::in|ios::out|ios::binary);
int item;
cout<<"Enter searching rollno: ";
cin>>item;
int record=0;
fr.seekg(0);
while(fr)
{
fr.read((char*)&s,sizeof(s));
record++;
if(item==s.rno)
{
s.putdata();
cout<<"\nEnter the modified data: ";

48

s.getdata();
fr.seekp((record-1)*sizeof(s),ios::beg);
getch();
fr.write((char*)&s,sizeof(s));
}
}
fr.close();
}
void dfile()
{ fstream fr,f2;
fr.open("temp",ios::in|ios::out|ios::binary);
f2.open("try",ios::out|ios::binary);
int item;
cout<<"Enter the roll number to be deleted: ";
cin>>item;
while(fr)
{
fr.read((char*)&s,sizeof(s));
if(item==s.rno);
else
f2.write((char*)&s,sizeof(s));
}
fr.close();

49

f2.close();
remove("temp");
rename("try","temp");
}

50

16.

Perform push and pop operation in a linear stack

#include<iostream.h>
#include<conio.h>
#include<process.h>
int p(int [],int&,int);
int pop(int [],int&);
void d(int [],int);

51

int const size=15;


void main()
{ int i,s[size],r,res,t=-1;
char ch='y',ch1;
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the item for insertion:";
cin>>i;
r=p(s,t,i);
if(r==-1)
{ cout<<"Overflow!!!";
exit(1);
}
cout<<"\nThe stack is:";
d(s,t);
cout<<"\nWant to insert more??";
cin>>ch;
}
ch1='y';
cout<<" delete elements";
while(ch1=='y'||ch1=='Y')
{ res=pop(s,t);
if(res==-1)
{ cout<<"Underflow!!!";

52

exit(1);
}
cout<<"The element which is deleted is:"<<res;
cout<<"\nThe stack is:";
d(s,t);
cout<<"\nWant to delete more elements??";
cin>>ch1;}
getch();
}
int p(int a[],int & top,int e)
{ if(top==size-1)
{ return -1;
}
else
{ top++;
a[top]=e;
return 0;}
}
int pop(int a[],int&top)
{int f;
if(f==size-1)
{return -1;}
else

53

{ f=a[top];
top--;
return f;}}
void d(int a[], int top)
{ if(top==-1) return;
cout<<a[top]<<"-";
for(int i=top-1;i>=0;i--)
cout<<"\n"<<a[i];}

54

17.

Perform push and pop operation in linked stack

#include<iostream.h>
#include<conio.h>
#include<process.h>
struct Node {

int info ;
Node * next ;

} *top, *newptr, *save, *ptr ;


Node * Create_New_Node( int ) ;
void Push( Node* ) ;
void Display( Node* ) ;
void Pop( ) ;
void main( )
{

top = NULL ;
int inf ;

char ch = 'y' ;

while ( ch =='y' || ch == 'Y' )

55

cout << "\n Enter INFOrmation for the new node..." ;


cin >> inf ;
newptr = Create_New_Node( inf ) ;
if ( newptr == NULL )
{

cout <<"\nCannot create new node!!! Aborting!!\n" ;


getch() ;
exit(1) ;

}
Push( newptr ) ;
cout << "\n Press Y to enter more nodes, N to exit...";
cin >> ch ;
}
clrscr( ) ;
do
{

cout << "\n The Stack now is : \n" ;


Display( top ) ;

getch( ) ;

cout << "Want to pop an element? (y/n)..." ;


cin >> ch ;
if ( ch == 'y' || ch == 'Y' )Pop( ) ;
} while ( ch == 'y' || ch == 'Y' ) ;
}
Node * Create_New_Node( int n )
{

ptr = new Node ;

56

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 ;


}}
void Pop( )
{

if( top == NULL ) cout << " UNDERFLOW !!!\n " ;


else
{

ptr = top ; top= top -> next ;


delete ptr ;}

}
void Display( Node* np )
{

while ( np !=NULL )
{

cout << np -> info << " -> " ;


np = np -> next ;}

cout << "!!!\n" ;}

57

58

18.

Perform push and pop operation in linear queue

#include<iostream.h>
#include<conio.h>
#include<process.h>
int Remove(int []) ;
int Insert(int [], int) ;
void Display(int [], int, int) ;
const int size = 50 ;
int Queue[size], front = -1, rear = -1 ;
void main()
{

int Item, res ; char ch='y' ;


clrscr() ;
while ( ch =='y' || ch == 'Y' )
{

cout << "\n Enter ITEM for insertion : " ;


cin >> Item ;
res = Insert(Queue,Item) ;
if (res == -1 )
{ cout << "OVERFLOW!!! Aborting!! \n" ;exit(1) ; }
cout << "\n Now the Queue (Front...to...Rear) is : \n" ;
Display(Queue, front, rear) ;
cout << "\n Want to insert more elements? (y/n)..." ;
cin >> ch ;

59

cout << "Now deletion of elements begins...\n" ;


ch = 'y' ;
while ( ch == 'y' || ch == 'Y' )
{

res = Remove(Queue) ;
if (res == -1 )
{ cout << "UNDERFLOW!!! Aborting!! \n" ;

exit(1) ; }

else
{

cout << "\nElement deleted is : " << res << endl ;

cout << "Now the Queue( Front...to...Rear ) is : \n" ;


Display(Queue, front, rear) ;
}
cout << "Want to delete more elements? (y/n)..." ;
cin >> ch ;
}
}
int Insert(int Queue[], int ele)
{

if ( rear == size -1 )return -1 ;


else if ( rear == -1 )
{

front = rear = 0 ;
Queue[rear] = ele ;

}
else
{

rear++ ;

60

Queue[rear] = ele ;
}
return 0 ;
}
int Remove( 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] << " <- \t" ;
cout << Queue[rear] << endl ;

61

62

19.

Perform push and pop operation in linked queue

#include<iostream.h>
#include<process.h>
#include<conio.h>
struct Node

int info ;

Node * next ;
} *front, *newptr, *save, *ptr, *rear ;
Node * Create_New_Node( int ) ;
void Insert( Node* ) ;
void Display( Node* ) ;
void delNode_Q( ) ;
void main( )
{

front = rear = NULL ;


int inf ; char ch = 'y' ;
while ( ch == 'y' || ch == 'Y' )
{
cout << "\n Enter INFOrmation for the new node..." ;
cin >> inf ;
newptr = Create_New_Node( inf ) ;
if ( newptr == NULL )
{ cout << "\nCannot create new node!!! Aborting!!\n" ;

exit(1) ; }
Insert ( newptr) ;

63

cout<< "\n Press Y to enter more nodes, N to exit...\n" ;


cin >> ch ;
}
clrscr() ;
do
{

cout << "\n The Linked Queue now is ( Front...to...Rear ) : \n" ;


Display( front ) ;
cout << "Want to delete first node? (y/n)..." ;
cin >> ch ;
if ( ch == 'y' || ch == 'Y' )
delNode_Q( ) ;

} while ( ch == 'y' || ch == 'Y' ) ;


}
Node * Create_New_Node( int n )
{

ptr = new Node ;


ptr -> info = n ;

ptr -> next = NULL ;

return ptr ;
}
void Insert( Node* np)
{

if ( front == NULL ) { front = rear = np ; }


else
{ rear -> next = np ;

rear = np ; }

64

void delNode_Q( )
{

if ( front == NULL ) cout << " UNDERFLOW !!!\n " ;


else
{

ptr = front ;
front = front -> next ;
delete ptr ;

}
}
void Display( Node* np )
{

while ( np != NULL )
{

cout << np -> info << " -> " ;


np = np -> next ;

}
cout << "!!!\n" ;
}

65

66

20.

SQL Queries

67

68

69

70

Das könnte Ihnen auch gefallen