Title: implementation of stack
Aim: a study of the implementation of stack
Theory:
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