Search This Blog

Saturday, 1 January 2022

implementation of stack

Title: implementation of stack

 Aim: a study of the implementation of stack

Theory:

What is stack in CPP
A stack is a standard C++ container adapter, designed to be used in a LIFO context, and is implemented with an interface/wrapper to the type passed to it as a template argument, which defaults to a deque. It is so simple, that it can be described with just a sample interface: C++ Standard Library. Input/output.
for  creating a stack:
For creating a stack, we must include the <stack> header file in our code. We then use this syntax to define the std::stack:
template <class Type, class Container = deque<Type> > class stack;
Type – is the type of element contained in the std::stack. It can be any valid C++ type or even a user-defined type.

Container – is the Type of underlying container object.

Member Types:-

value_type- The first template parameter, T. It denotes the element types.

container_type- The second template parameter, Container. It denotes the underlying container type.

size_type- Unsigned integral type.

The functions associated with stack are: 
empty() – Returns whether the stack is empty – Time Complexity: O(1) 
size() – Returns the size of the stack – Time Complexity: O(1) 
top() – Returns a reference to the topmost element of the stack – Time Complexity: O(1) 
push(g) – Adds the element ‘g’ at the top of the stack – Time Complexity: O(1) 
pop() – Deletes the topmost element of the stack – Time Complexity: O(1)


Program:

#include <iostream>

#include<conio.h>

using namespace std;


 int stack[100], n=100,top=-1;

void push(int val)

{

    if(top >=n-1)

    {

        cout<<"stack is Overflow";

    }

    else

    {

        top++;

        stack[top]=val;

    }

}


void pop()

{

    if(top <=-1)

    {

        cout<<"Stack is Underflow";

    }

    else

    {

        cout<<"The Popped Element is "<<stack[top];

        top--;

    }

}


void display()

{

    if(top >=0)

    {

        cout<<"Stack Elements are:";

        for(int i=top;i>=0;i++)

        {

            cout<<stack[i]<<" ";

            cout<<endl;

        }

    }    

    else

    {    

            cout<<"Stack is Empty:";

        

    }

}


int main()

{

    int ch,val;

    cout<<"1) Push in Stack"<<endl;

    cout<<"2) Pop in Stack"<<endl;   

    cout<<"3) Display Stack"<<endl;   

    cout<<"4) Exit"<<endl;

    do{

        cout<<"Enter Choice:"<<endl;

        cin>>ch;

        switch(ch)

        {

            case 1:

            {

                cout<<"Enter Value to be Pushed:"<<endl;

                cin>>val;

                push(val);

                break;

            }

            case 2:

            {

                pop();

                break;

            }

            case 3:

            {

                display();

                break;

            }

            case 4:

            {

                cout<<"Exit"<<endl;

                break;

            }

            default:

        {

                cout<<"Wrong Choice"<<endl;

            }

        }

    }

    while(ch!=4);

    return 0;

}


OutPut


No comments:

Post a Comment

Search This Blog

Contact Form

Name

Email *

Message *

Popular Posts