Header Ads

All Stack Standard Operation | Stack Standard Function Like Push Pop

This Post Cover The All Stack Standard Operation using C++ Program

How to Push Value in Stack using C++

How to Pop Value in Stack using C++

How to Find Size of Stack using C++

How to Find Top Value of Stack using C++

How to Change Value of Stack using C++

How to Find Stack is Empty or Not using C++

How to Find Stack is Full or Not using C++

How to Display Value of Stack on Screen using C++

Program Source Code:


#include<iostream>

#include<string.h>

using namespace std;

class stack{

private:

int top;

int stk[5];

public:

stack(){

top = -1;

for(int i = 0; i<5; i++){

stk[i]=0;

}

}

void push(int val){

if(isFull()){

cout<<"Stack is Overflow"<<endl;

}

else{

top++;

stk[top] = val;

}

}

int pop(){

if(isEmpty()){

cout<<"Stack is Underflow"<<endl;

return 0;

}

else{

int popValue = stk[top];

stk[top] = 0;

top--;

return popValue;

}

}

bool isEmpty(){

if(top == -1)

return true;

else

return false;

}

bool isFull(){

if(top == 4)

return true;

else

return false;

}

int count(){

return(top+1);

}

int peek(int pos){

if(isEmpty()){

cout<<"Stack is Underflow"<<endl;

return 0;

}

else{

return stk[pos];

}

}

void change(int pos, int val){

stk[pos]=val;

cout<<" item change at location : "<<pos<<endl;

}

void display(){

cout<<"All values in the stack :"<<endl;

for(int i = 4; i>=0; i--)

{

cout<<stk[i]<<endl;

}

}

};

main(){

stack objS;

int option, postion, value,loop = 1;

while(loop){   

cout<<"1. Pushing Stack"<<endl;

cout<<"2. Popping Stack"<<endl;

cout<<"3. isEmpty() Stack"<<endl;

cout<<"4. isFull() Stack"<<endl;

cout<<"5. Peek() Stack"<<endl;

cout<<"6. Count() Stack"<<endl;

cout<<"7. Change Value in Stack"<<endl;

cout<<"8. Display Stack"<<endl;

cout<<"9. Clear Screen "<<endl;

cout<<"10. Exit"<<endl;

cout<<"Enter Your Choice [1-9] ?";

cin>>option;

switch(option){

case 1:

cout<<"Enter item to push in stack:";

cin>>value;

objS.push(value);

break;

case 2:

cout<<"Pop Function is Called value is Poped :"<<objS.pop()<<endl;

break;

case 3:

if(objS.isEmpty())

cout<<"Stack is Empty "<<endl;

else

cout<<"Stack is not Empty "<<endl;

break; 

case 4:

if(objS.isFull())

cout<<"Stack is Full "<<endl;

else

cout<<"Stack is not Full "<<endl;

break;

case 5:

    cout<<"Enter Position of item you want to peek "<<endl;

    cin>>postion;

    cout<<"Peek Function Called "<<endl<<objS.peek(postion)<<endl;

    case 6:

    cout<<"Count Function Called Number of Item in Stack is :"<<objS.count()<<endl;

    break;

case 7:

cout<<"Change Function is Called"<<endl;

cout<<"Enter Position of Item Do you want ot Change:";

cin>>postion;

cout<<"Enter Value of item you want ot Change :";

cin>>value;

objS.change(postion,value);

case 8:

cout<<"Display Function is Called "<<endl;

objS.display();

break;

case 9:

system("cls");

break;

case 10:

loop =0;

break;

default:

cout<<"Invalid Number";

}

}

}


Code Related Video


Full Tutorial Link

Data Structures and Algorithm using C++


No comments

Powered by Blogger.