You are on page 1of 7

STACKS

IMPLEMENTATION
Divyam Agrawal
CSE
20103088

Stack is a linear data structure that follows LIFO principle ,i.e., the
element that is entered latest at the back of the stack is accessed first.

It can perform following functions:

• POP()
The pop() function is used to access the topmost element and reducing the size
of the size of the stack data structure. It returns the element if the stack is not
empty, but returns “Stack Underflow” warning if the stack is empty.

void pop()
{
if(t==0)// ‘t’ represents the number of terms in the stack
{
cout<<"Stack Underflow"<<endl;
}
else
{
cout<<Stack[t-1]<<endl;
t--;
}
}

The pop() function uses single extra storage space for number of terms in the
stack irrespective of the size of the stack. Thus,
Space Complexity : - 𝑶(𝟏)

The pop() function only displays a single term out of the stack without running
any loop across the stack. Thus,
Time Complexity :- 𝑶(𝟏)
• PUSH()
The push() function adds an element to stack at the topmost position and
increases the size of the Stack data structure. The push() takes the input
element as a parameter and adds the element when the stack is not full , but
returns “Stack Overflow” when the stack is full.

void push(int x)
{
if(t==3) // ‘t’ represents the number of terms in the stack
{
cout<<"Stack Overflow"<<endl;
}
else
{
t++;
Stack[t-1] = x;
}
}

The push() function uses single extra storage space for number of terms in the
stack irrespective of the size of the stack. Thus,

Space Complexity : - 𝑶(𝟏)

The push() function only adds a single term in the stack without running any loop
across the stack. Thus,

Time Complexity :- 𝑶(𝟏)


• DISPLAY()
The display() function displays all the elements in the stack starting from the
bottommost element of the stack. If the stack is empty , the display() function
shows nothing.

void display()
{// ‘t’ represents the number of terms in the stack
for(int i=0;i<t;i++)
{
cout<<Stack[i]<<" ";
}
cout<<endl;
}

The push() function uses single extra storage space for number of terms in the
stack irrespective of the size of the stack. Thus,

Space Complexity : - 𝑶(𝟏)

The display() function runs through the whole stack once to print all its elements
by using a single loop that depends on the number of terms in the stack. Thus,

Time Complexity :- 𝑶(𝒏)


STACK IMPLEMENTATION

#include<bits/stdc++.h>
using namespace std;
class Stack
{
public:
int Stack[3];
int t = 0;
void push(int x)
{
if(t==3)
{
cout<<" Stack Overflow"<<endl;
}
else
{
t++;
Stack[t-1] = x;
cout<<" "<<x<<" added at the top of the stack , the size
of stack is now "<<t<<"."<<endl;
}
}
void pop()
{
if(t==0)
{
cout<<" Stack Underflow"<<endl;
}
else
{
cout<<" "<<Stack[t-1]<<" is now removed from the top of
the stack , now the size of the stack is "<<t-1<<"."<<endl;
t--;
}
}
void Empty()
{
if(t==0)
{
cout<<" The stack is empty."<<endl;
}
else
{
cout<<" The stack is not empty."<<endl;
}
}
void Full()
{
if(t==3)
{
cout<<" The stack is full."<<endl;
}
else
{
cout<<" The stack is not full."<<endl;
}
}
void display()
{
cout<<" There are "<<t<<" elements on the stack : "<<endl;
for(int i=0;i<t;i++)
{
cout<<" "<<Stack[i];
}
cout<<endl;
}
};

int main()

{
Stack Stack1;
Stack1.Empty();
Stack1.pop();
Stack1.push(12);
Stack1.push(24);
Stack1.Empty();
Stack1.pop();
Stack1.pop();
Stack1.Full();
Stack1.display();
Stack1.pop();
Stack1.Empty();
Stack1.push(18);
Stack1.push(21);
Stack1.display();
Stack1.push(39);
Stack1.push(47);
Stack1.display();
Stack1.pop();
Stack1.Full();
Stack1.Empty();
cout<<endl<<endl<<" Divyam Agrawal"<<endl<<" CSE"<<endl<<"
20103088";
}
RESULTS

You might also like