Experiment 9
Aim
To design and implement Stack and Queue classes with
necessary exception handling for handling overflow and underflow
conditions.
Objectives
- To
implement Stack and Queue using classes.
- To
understand exception handling.
- To
handle overflow and underflow conditions.
- To
use try, throw and catch.
Theory
An exception represents an abnormal condition during program
execution. C++ provides three major keywords for exception handling:
- try
— contains code that may generate an exception.
- throw
— generates an exception.
- catch
— handles the exception.
Program
#include <iostream>
using namespace std;
class Stack {
int a[5], top;
public:
Stack() {
top = -1;
}
void push(int x) {
if (top == 4)
throw
"Stack Overflow";
a[++top] = x;
}
void pop() {
if (top == -1)
throw
"Stack Underflow";
cout <<
"Popped: " << a[top--] << endl;
}
void display() {
for (int i =
top; i >= 0; i--)
cout
<< a[i] << " ";
cout <<
endl;
}
};
class Queue {
int a[5], front,
rear;
public:
Queue() {
front = 0;
rear = -1;
}
void insert(int x)
{
if (rear == 4)
throw
"Queue Overflow";
a[++rear] = x;
}
void remove() {
if (front >
rear)
throw "Queue Underflow";
cout <<
"Deleted: " << a[front++] << endl;
}
void display() {
for (int i =
front; i <= rear; i++)
cout
<< a[i] << " ";
cout <<
endl;
}
};
int main() {
try {
Stack s;
s.push(10);
s.push(20);
s.push(30);
cout <<
"Stack: ";
s.display();
s.pop();
Queue q;
q.insert(10);
q.insert(20);
q.insert(30);
cout <<
"Queue: ";
q.display();
q.remove();
}
catch (const char
*msg) {
cout <<
"Exception: " << msg << endl;
}
return 0;
Experiment 9
Aim
To design and implement Stack and Queue classes with
necessary exception handling for handling overflow and underflow
conditions.
Objectives
- To
implement Stack and Queue using classes.
- To
understand exception handling.
- To
handle overflow and underflow conditions.
- To
use try, throw and catch.
Theory
An exception represents an abnormal condition during program
execution. C++ provides three major keywords for exception handling:
- try
— contains code that may generate an exception.
- throw
— generates an exception.
- catch
— handles the exception.
Program
#include <iostream>
using namespace std;
class Stack {
int a[5], top;
public:
Stack() {
top = -1;
}
void push(int x) {
if (top == 4)
throw
"Stack Overflow";
a[++top] = x;
}
void pop() {
if (top == -1)
throw
"Stack Underflow";
cout <<
"Popped: " << a[top--] << endl;
}
void display() {
for (int i =
top; i >= 0; i--)
cout
<< a[i] << " ";
cout <<
endl;
}
};
class Queue {
int a[5], front,
rear;
public:
Queue() {
front = 0;
rear = -1;
}
void insert(int x)
{
if (rear == 4)
throw
"Queue Overflow";
a[++rear] = x;
}
void remove() {
if (front >
rear)
throw "Queue Underflow";
cout <<
"Deleted: " << a[front++] << endl;
}
void display() {
for (int i =
front; i <= rear; i++)
cout
<< a[i] << " ";
cout <<
endl;
}
};
int main() {
try {
Stack s;
s.push(10);
s.push(20);
s.push(30);
cout <<
"Stack: ";
s.display();
s.pop();
Queue q;
q.insert(10);
q.insert(20);
q.insert(30);
cout <<
"Queue: ";
q.display();
q.remove();
}
catch (const char
*msg) {
cout <<
"Exception: " << msg << endl;
}
return 0;
}
Result
Thus, Stack and Queue classes were successfully implemented
with exception handling for overflow and underflow conditions.
No comments:
Post a Comment