Wednesday 12 October 2011

IMPLEMENTATION OF QUEUE USING EXCEPTION HANDLING


IMPLEMENTATION OF QUEUE USING EXCEPTION HANDLING

AIM:
To implement a Queue using exception handling using C++.
ALGORITHM:
Step 1: Start the process.
Step 2: Create a class queue.
Step 3: Initialize the front and rear pointers of the queue.
Step 4: Create the members full, empty, insert, dele and display.
Step 5: Inside the insert function check if rear = max. If rear is not equal to max then add the element to the queue. If rear reaches the max then throw the message “Queue Overflow”.
Step 6: Inside the dele function, check if front = rear. If front is not equal to rear then deletion is possible. If front is equal to the rear, throw the message “Queue Underflow”.
Step 7: Display the queue.
Step 8: Stop the process.

PROGRAM:
#include<iostream>
#include<iomanip>
using namespace std;
class queue
{
private:
int *q;
int max, front, rear;
int cnt;
public:
class FULL{};  //for exception handling
class EMPTY{}; //for exception handling
queue(int);
void insert(int);
int dele(void);
void display(void);
};
queue::queue(int m)
{
q=new int[m];
rear=0;
front=0;
cnt=0;
max=m;
}
void queue::insert(int item)
{
if(cnt<max)
{
front = front % max;
q[front++]=item;
cnt++;
}
else
throw FULL(); //FULL object is thrown
}
int queue::dele(void)
{
if(cnt>0)
{
cnt--;
rear = rear % max;
return q[rear++];
}
else
throw EMPTY(); //EMPTY object is thrown
}
void queue::display(void)
{
if(cnt>0)
for(int i=0,j=front; i<cnt; i++,j++)
cout<<"|"<<q[j % max]<<"|";
else
throw EMPTY();
}
int main()
{
int item, size;
int ch=1;
cout<<"\nEnter the size of the queue…";
cin>>size;
queue q(size);
cout<<"\nQueue Operations using Exception Handling";
cout<<"\n\n\tMENU\n1.INSERT\n2.DELETE\n 3.SHOW QUEUE\n4.EXIT";
cout<<"\nEnter your choice…";
cin>>ch;
do
{
switch(ch)
{
case 1:
cout<<"\nEnter the item to insert in to the queue…";
cin>>item;
try
{
q.insert(item);
}
catch(queue::FULL)  //FULL object is caught
{
cout<<"\n***Queue Full***\n";
}
break;
case 2:
try
{
cout<<"\nRemoved Item from the Qis…"<<q.dele();
}
catch(queue::EMPTY) //EMPTY object is caught
{
cout<<"\n***Queue Empty***\n";
}
break;
case 3:
cout<<"\nThe Queue is…\n";
try
{
q.display();
}
catch(queue::EMPTY)
{
cout<<"\n***Queue Empty***\n";
}
break;
case 4:
exit(0);
}
cout<<"\nEnter your choice…";
cin>>ch;
}while(ch<5);
return 0;
}
OUTPUT:
[ex@localhost ~]$ g++ q2.cpp
[ex@localhost ~]$ ./a.out
Enter the size of the queue…2
Queue Operations using Exception Handling
        MENU
1.INSERT
2.DELETE
 3.SHOW QUEUE
4.EXIT
Enter your choice…1
Enter the item to insert in to the queue…12
Enter your choice…1
Enter the item to insert in to the queue…23
Enter your choice…1
Enter the item to insert in to the queue…34
***Queue Full***
Enter your choice…3
The Queue is…
|12||23|
Enter your choice…2
Removed Item from the Qis…12
Enter your choice…2
Removed Item from the Qis…23
Enter your choice…2
***Queue Empty***
Enter your choice…3
The Queue is…
***Queue Empty***
Enter your choice…4

RESULT:
Thus the program to implement the queue  using exception handling was executed.

0 comments:

Post a Comment