0% found this document useful (1 vote)
2K views

Turbo C++ Code For Stack

This document defines a class called stack that implements basic stack operations like push, pop, and print. The main function uses a menu to allow the user to push values onto or pop values off a stack object and print the stack after each operation. It includes error handling for overflow and underflow conditions.

Uploaded by

khan10250
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
2K views

Turbo C++ Code For Stack

This document defines a class called stack that implements basic stack operations like push, pop, and print. The main function uses a menu to allow the user to push values onto or pop values off a stack object and print the stack after each operation. It includes error handling for overflow and underflow conditions.

Uploaded by

khan10250
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

STACK //stack #include <conio.h> #include <iostream.

h> class stack { private: int s[10]; int top; public: stack() { top= -1; } void push(int n) { if(top==9) { cout<<"Stack overflow..."; return; } top= top + 1; s[top]= n; } int pop() { int data; if(top == -1) { cout<<"Stack underflow..."; return NULL; } data= s[top]; top=top-1; return data; } void print() { if(top== -1) { cout<<"Stack is empty..."; return; } else for (int i=top+1; i>=0; i--) cout<<s[i]<<endl; } }; void main() { int val, option; stack obj; while (option!=3) { Page 1

STACK clrscr(); cout<<"Press 1: for Push"<<endl <<"Press 2: for Pop" <<endl <<"Press 3: for exit"<<endl; cin>>option; switch (option) { case 1: cout<<"Enter value to insert"; cin>>val; obj.push(val); cout<<"Stack after insertion: "; obj.print(); break; case 2: cout<<"Value " <<obj.pop()<<" is deleted "; cout<<"Stack after deletion: "; obj.print(); break; } getch(); } }

Page 2

You might also like