C++ program to implement Stack using Formula Based Representation

#include<iostream.h>
#include<constream.h>
template<class T>
class Stack
{
	public:
		Stack(int MaxStackSize);
		~Stack(){delete[] S;}
		int IsEmpty()const{return top==-1;}
		int IsFull()const{return top==MaxTop;}
		T Peek()const;
		void Push(T);
		T Pop();
		void Display();
	private:
		int top; //current top of stack
		int MaxTop; //max val for top
		T *S; //element array
};
	template<class T>
Stack<T>::Stack(int MaxStackSize)
{
	//stack constructor
	MaxTop=MaxStackSize-1;
	S=new T[MaxStackSize];
	top=-1;
}
template<class T>
T Stack<T>::Peek()const
{
	if(IsEmpty()) //top fails
		return 0;
	else
		return S[top];
}
	template<class T>
void Stack<T>::Push(T x)
{
	if(IsFull())
		cout<<"no memory()"; //add fails
	else
	{
		S[++top]=x;
	}
}
	template<class T>
T Stack<T>::Pop()
{
	T x;
	if(IsEmpty())
	{
		cout<<"stack is empty\n";
		return -1;
	}
	else
	{
		x=S[top--];
		return x;
	}
}
	template<class T>
void Stack<T>::Display()
{
	if(IsEmpty())
		cout<<"out of bounds"; //delete fails
	else
		for(int i=top;i>=0;i--)
		{
			cout<<S[i]<<"\t";
		}
}
void menu()
{
	cout<<"1.Push\n 2.Pop\n 3.Peek\n 4.Display\n";
}
void main()
{
	Stack<int>iobj(5);
	int ch,x;
	clrscr();
	do
	{
		menu();
		cout<<"enter the choice\n";
		cin>>ch;
		switch(ch)
		{
			case 1:
				cout<<"enter x value to push into the stack\n";
				cin>>x;
				iobj.Push(x);
				break;
			case 2:
				x=iobj.Pop();
				if(x!=-1)
					cout<<"poped value is \t"<<x<<endl;
				break;
			case 3:
				x=iobj.Peek();
				cout<<"top most value is \t"<<x<<endl;
				break;
			case 4:
				iobj.Display();
				break;
		}
	}while(ch>=1&&ch<=4);
	getch();
}

2 Responses to “C++ program to implement Stack using Formula Based Representation”

  1. ((this is my programme about stack implimentation .It gives error type mismatching.please anyone help me
    #include
    #include
    # define SIZE 5
    int stack[SIZE];
    int top;
    void main(void)
    {
    int ch;
    top=-1;
    clrscr();
    do
    {

    printf(“\n1:push\n2:pop\n3:display\n4:exit\n enter your choice”);
    scanf(“%d”,&ch);
    switch(ch);
    {
    case1:push();
    break;
    case2:pop();
    break;
    case3:display();
    break;
    case4:break;
    Default:printf(” wrong choice”);
    }
    } while(ch!=4);

    getch();
    }

    void push(int x)
    {
    int x;
    if(top==SIZE-1)
    {
    printf(” stack overflow”);
    return;
    }
    printf(“enter data”);
    scanf(“%d”,&x);
    top++;
    stack[top]=x;
    printf(“%d is pushed”,x);
    return x;

    }

    int pop()
    {
    int x;
    if(top==-1)
    {
    printf(“stack underflow”);
    return;
    }
    printf(“enter data”);
    scanf(“%d”,&x);
    top–;
    x=stack[top];
    printf(“%d is poped”,x);
    return x;

    }
    void display(void)
    {
    int i;
    if(top==-1)
    {
    printf(“stack is empty”);
    return;
    }
    i=top;
    while(i>=0)
    {
    printf(“%d\n”,stack[i]);
    i–;

    }
    }

    Reply

Leave a Reply