Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 3) WAP to implement Stack
- #include <iostream>
- using namespace std;
- const int StackSpace=4;
- int top=-1;
- int Stack[StackSpace];
- void push(int data){
- if(top==(StackSpace-1)){
- cout<<"Stack over flowed!!!"<<endl;
- return ;
- }
- Stack[++top] = data;
- }
- void pop(){
- if(top==-1){
- cout<<"Stack under flowed!!!"<<endl;
- return ;
- }
- top--;
- }
- void peek(){
- if(top==-1){
- cout<<"Stack is empty!!!"<<endl;
- return ;
- }
- cout<<"Current top element of stack: "<<Stack[top]<<endl;
- }
- void size(){
- cout<<"Current stack size: "<<top+1<<endl;
- }
- void printStack(){
- cout<<"Current stack elements:"<<endl;
- for(int i=top;i>=0;i--){
- cout<<Stack[i]<<endl;
- }
- }
- int main(){
- push(10);
- push(20);
- push(30);
- push(40);
- peek();
- size();
- printStack();
- pop();
- peek();
- size();
- printStack();
- return 0;
- }
- /*
- OUTPUT:
- Current top element of stack: 40
- Current stack size: 4
- Current stack elements:
- 40
- 30
- 20
- 10
- Current top element of stack: 30
- Current stack size: 3
- Current stack elements:
- 30
- 20
- 10
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement