Introduction to Stack
A stack is another type of Data Structure, generally implemented using arrays. Generally, the implementation is LIFO i.e. Last in First out, also used for FILO, i.e. First in Last out
A stack is another type of Data Structure, generally implemented using arrays. Generally, the implementation is LIFO i.e. Last in First out, also used for FILO, i.e. First in Last out
If Stack is full, then it is said to be in an overflow condition
If a stack is empty then it is said to be in an underflow condition
Stack as a data structure can be represented in two ways.
There are a number of operations we can perform on a stack as per our need which are as follows:
The time complexity of each of these operations is constant time, i.e , O(1).
// Stack implementation in C
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int count = 0;
// Creating a stack
struct stack {
int items[MAX];
int top;
};
typedef struct stack st;
void createEmptyStack(st *s) {
s->top = -1;
}
// Check if the stack is full
int isfull(st *s) {
if (s->top == MAX - 1)
return 1;
else
return 0;
}
// Check if the stack is empty
int isempty(st *s) {
if (s->top == -1)
return 1;
else
return 0;
}
// Add elements into stack
void push(st *s, int newitem) {
if (isfull(s)) {
printf("STACK FULL");
} else {
s->top++;
s->items[s->top] = newitem;
}
count++;
}
// Remove element from stack
void pop(st *s) {
if (isempty(s)) {
printf("\n STACK EMPTY \n");
} else {
printf("Item popped= %d", s->items[s->top]);
s->top--;
}
count--;
printf("\n");
}
// Print elements of stack
void printStack(st *s) {
printf("Stack: ");
for (int i = 0; i < count; i++) {
printf("%d ", s->items[i]);
}
printf("\n");
}
// Driver code
int main() {
int ch;
st *s = (st *)malloc(sizeof(st));
createEmptyStack(s);
push(s, 1);
push(s, 2);
push(s, 3);
push(s, 4);
printStack(s);
pop(s);
printf("\nAfter popping out\n");
printStack(s);
}
#include <iostream>
using namespace std;
int stack[100], n=100, top=-1;
void push(int val) {
if(top>=n-1)
cout<< "Stack Overflow"<< endl;
else {
top++;
stack[top]=val;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<=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 from 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<<"Invalid Choice"<< endl;
}
}
}while(ch!=4);
return 0;
}
class Stack {
private int arr[];
private int top;
private int capacity;
// Creating a stack
Stack(int size) {
arr = new int[size];
capacity = size;
top = -1;
}
// Add elements into stack
public void push(int x) {
if (isFull()) {
System.out.println("OverFlow\nProgram Terminated\n");
System.exit(1);
}
System.out.println("Inserting " + x);
arr[++top] = x;
}
// Remove element from stack
public int pop() {
if (isEmpty()) {
System.out.println("STACK EMPTY");
System.exit(1);
}
return arr[top--];
}
// Utility function to return the size of the stack
public int size() {
return top + 1;
}
// Check if the stack is empty
public Boolean isEmpty() {
return top == -1;
}
// Check if the stack is full
public Boolean isFull() {
return top == capacity - 1;
}
public void printStack() {
for (int i = 0; i <= top; i++) {
System.out.println(arr[i]);
}
}
public static void main(String[] args) {
Stack stack = new Stack(5);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.pop();
System.out.println("\nAfter popping out");
stack.printStack();
}
}
Output : C
Stack: 1 2 3 4
Item popped= 4
After popping out
Stack: 1 2 3
Output : C++
1) Push in stack
2) Pop from stack
3) Display stack
4) Exit
Enter choice:
1
Enter value to be pushed:
10
Enter choice:
1
Enter value to be pushed:
20
Enter choice:
1
Enter value to be pushed:
34
Enter choice:
2
The popped element is 34
Enter choice:
3
Stack elements are:20 10
Enter choice:
4
Exit
Output : Java
Inserting 1Inserting 2
Inserting 3
Inserting 4
After popping out
1
2
3
Let us understand the concept of Stack. As we have learned about stack above, it stores the data in a particular fashion where the element can only be inserted or deleted at one end only.
Let us consider a stack of books as shown in the image.
Push | Pop | Display | Space Complexity |
O(1) | O(1) | O(n) | O(n) |