0% found this document useful (0 votes)
47 views

Practical No.3 Write A Program in C++ To Implement Stack Data Structure, Use Exception Handling To Check Overflow and Underflow Conditions. Sol

The document describes a C++ program that implements a stack data structure using classes and exception handling. The stack class contains an array to store elements and tracks the top index. Methods are included to push, pop, and display elements, with exception handling for overflow and underflow conditions. The main function contains a menu loop to test the stack methods and catch any exceptions.

Uploaded by

poojagndu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Practical No.3 Write A Program in C++ To Implement Stack Data Structure, Use Exception Handling To Check Overflow and Underflow Conditions. Sol

The document describes a C++ program that implements a stack data structure using classes and exception handling. The stack class contains an array to store elements and tracks the top index. Methods are included to push, pop, and display elements, with exception handling for overflow and underflow conditions. The main function contains a menu loop to test the stack methods and catch any exceptions.

Uploaded by

poojagndu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

PRACTICAL NO.

3 Write a program in C++ to implement Stack Data Structure, use Exception handling to check Overflow and Underflow conditions. Sol:#include<iostream.h> #include<conio.h> #define max 6 class excpovrflw {}; class excpudrflw {}; class stack { int arr[max]; int top; public: stack() { top=-1; } void push(int x); int pop(); void display(); }; void stack::push(int x) { if(top==max-1)

throw excpovrflw(); arr[++top]=x; } int stack::pop() { if(top==-1) throw excpudrflw(); return arr[top--]; } void stack::display() { if(top==-1) throw excpudrflw(); cout<<"stck is:\n"; for(int i=top;i>0;i--) { cout<<endl<<arr[i]; } } int main() { int elmnt,choice; stack s; do { cout<<endl<<"1:push";

cout<<endl<<"2:pop"; cout<<endl<<"3:dsply"; cout<<endl<<"4:ext"; cout<<endl<<"entr choice"; cin>>choice; switch(choice) { case 1: cout<<"enter element 2 push"; cin>>elmnt; try { s.push(elmnt); cout<<endl<<elmnt<<"pushed"; } catch(excpovrflw) { cout<<"cant push"<<elmnt<<"stckfll"; } break; case 2: try { elmnt=s.pop(); cout<<endl<<elmnt<<"poped" }

catch(excpudrflw) { cout<<"stack empty"; } break; case 3: try { s.display(); } catch(excpudrflw) { cout<<"stack empty"; } break; case 4:cout<<"exit"; break; default: cout<<"invalid"; } } while(choice!=(4)); return (0); }

OUTPUT OF PROGRAM

You might also like