
Stack Using Array
Opreation:
- push
- pop
- count
- print
- max
Snippet options
Download: Download snippet as stack-using-array.cpp.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!
#include<iostream> using namespace std; #define SIZE 50 class arrayy{ private: int stack[SIZE]; int top; public: arrayy(){ top = -1; } bool isempty(); bool isfull(); void push(int); int pop(); void traverse(); void tr(); int count(); void max(); }; bool arrayy::isempty(){ if (top == -1) return true; else return false; } bool arrayy::isfull(){ if (top == SIZE - 1) return true; else return false; } void arrayy::push(int item){ if (isfull()) cout << "overflow"; else { top++; stack[top] = item; } } int arrayy::pop(){ if (isempty()){ cout << "underflow"; return 0; } else { int temp = stack[top]; top--; return temp; } } void arrayy::traverse(){ if (isempty()) cout << "underflow"; else{ int toptemp = top; do{ cout << stack[toptemp]<<"\t"; toptemp--; } while (toptemp >= 0); } } void arrayy::tr(){ for (int i = 0; i <= top; i++){ cout << stack[i]; } cout << endl; } int arrayy::count(){ return top + 1; } void arrayy::max(){ int max = stack[top]; for (int i = top; i >= 0; i--){ if (max < stack[i]) max = stack[i]; } cout << "max is " << max << endl; } int main(){ arrayy x; int y; int item; int size; cout << "enter size"; cin >> size; for (int i = 0; i < size; i++){ cin >> item; x.push(item); } x.traverse(); cout << endl; x.tr(); y=x.count(); cout << "count " << y<<endl; x.max(); return 0; }
Create a free my code stock.com account now.
my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!
Find out more and register nowYou can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.