**Write a C++ code to implement Stack ADT and understand its basic operations.**

*A Stack is an ordered list in witch insertion and deletion are made at one end called the Top. The restrictions on the stack imply that if we add the elements 1,2,3,4,5 to the stack in that order, then 5 is the first element we delete from the stack. The add operation is called “Push”, and the delete operation is called “Pop”. Since the last element pushed into the stack is the first element popped, a stack is also known as a Last In First Out (LIFO) List.*

**Implementation of the Stack Operations:***template<class T>*

*class stack*

*{*

*private:*

*int stacksize, top;*

*T *astack;*

*Public:*

*stack (int size); //constructor*

*int isFull();//returns 1 if full, 0 if not full*

*int isEmpty();//returns 1 if Empty, 0 is not empty*

*void push(T item);//pushes item to stack*

*T pop(); //pops one item from stack*

*};*

*/* Constructor – define & initialize stack */*

*template<class T>*

*stack::stack(int size)*

*{*

*stacksize=size;*

*astack=new T[stacksize];*

*top=-1;*

*}*

*/* isFull() function */*

*template<class T>*

*int stack<T>::isFull()*

*{*

*if (top==stacksize-1)*

*return 1;*

*else*

*return 0;*

*}*

*/* isEmpty() function */*

*template<class T>*

*int stack<T>::isEmpty()*

*{*

*if (top==-1)*

*return 1;*

*else*

*return 0;*

*}*

*/* push() function:inserts an item on top of stack */*

*template<class T>*

*void stack<T>::push(T item)*

*{*

*if (isFull())*

*Stack is full;*

*else*

*stack[++top]=item;*

*}*

*/* pop() function:deletes an item from top of stack */*

*template<class T>*

*T stack<T>::pop()*

*{*

*if(isEmpty())*

*Stack is empty;*

*else*

*{*

*T x;*

*x=stack[top--];*

*return x;*

*}*

*}*

**Thanks**

**Mukesh Rajput**
## No comments:

## Post a Comment

Thanks

Mukesh Rajput