- What is Stack: Stack is an linear data structure in which both insertion and deletion take's place at one end which is the TOP Of the Stack.
Stack is also known as LIFO
LIFO=LAST IN FIRST OUT.
This state's that the element that is inserted at last will come first out.
It is an example of plate's which indicate's the Stack OPeration LIFO.
There are some Basic operation's which are performed on Stack's
they are :
1.Push()
2.Pop()
3.top()
4.isEmpty()
Push():
Adds an item to stack. If the stack is full,then it is said to be
OVERFLOW.
Pop():
Remove's an item from the stack.If the stack is empty it is said to be UNDERFLOW.
Top():
Return's top element of stack.
isempty():
Returns True if stack is empty and false if stack is Full.
Basic Implementation of stack using array's in c:
`#include
define SIZE 5
int stack[SIZE];
int top = -1;
void push();
void pop();
void display();
int main()
{
int choice = 0;
printf("******Stack operations using array******\n");
while (choice != 4)
{
printf("-----------------------------------------------\n");
printf("Choose the stack operation to be performed:\n");
printf("-----------------------------------------------\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Please enter a valid choice.\n");
}
}
return 0;
}
void push()
{
int val;
if (top == SIZE - 1)
{
printf("Overflow: Stack is full!\n");
}
else
{
printf("Enter the value to be pushed: ");
scanf("%d", &val);
top++;
stack[top] = val;
}
}
void pop()
{
if (top == -1)
{
printf("Underflow: Stack is empty!\n");
}
else
{
top--;
}
}
void display()
{
if (top == -1)
{
printf("Stack is empty.\n");
}
else
{
printf("Elements inside the stack are:\n");
for (int i = top; i >= 0; i--)
{
printf("%d\n", stack[i]);
}
}
}
`
Complexity analysis:
Push() O(1)
Pop() O(1)
isEmpty() O(1)

Top comments (0)