15 - Data Structure and Algorithms - Stack
15 - Data Structure and Algorithms - Stack
Stack
The stack follows the LIFO (Last in - First out) structure where the last element
inserted would be the first element deleted.
Stack Representation
A Stack ADT allows all data operations at one end only. At any given time, we
can only access the top element of a stack.
Stack operations usually are performed for initialization, usage and, de-
initialization of the stack ADT.
The most fundamental operations in the stack ADT include: push(), pop(),
peek(), isFull(), isEmpty(). These are all built-in operations to carry out data
manipulation and to check the status of the stack.
Stack uses pointers that always point to the topmost element within the stack,
hence called as the top pointer.
Insertion: push()
push() is an operation that inserts elements into the stack. The following is an
algorithm that describes the push() operation in a simpler way.
Algorithm
3 − If the stack is not full, increments top to point next empty space.
5 − Returns success.
Example
C
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int isfull(){
if(top == MAXSIZE)
return 1;
else
return 0;
if(!isfull()) {
top = top + 1;
stack[top] = data;
} else {
printf("Could not insert data, Stack is full.\n");
/* Main function */
int main(){
int i;
push(44);
push(10);
push(62);
push(123);
push(15);
return 0;
}
Output
Stack Elements:
44 10 62 123 15 0 0 0
Deletion: pop()
Algorithm
3 − If the stack is not empty, accesses the data element at which top is
pointing.
5 − Returns success.
Example
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int isempty(){
if(top == -1)
return 1;
else
return 0;
int isfull(){
if(top == MAXSIZE)
return 1;
else
return 0;
}
/* Function to delete from the stack */
int pop(){
int data;
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
} else {
if(!isfull()) {
top = top + 1;
stack[top] = data;
} else {
printf("Could not insert data, Stack is full.\n");
/* Main function */
int main(){
int i;
push(44);
push(10);
push(62);
push(123);
push(15);
while(!isempty()) {
printf("%d ",data);
return 0;
Output
Stack Elements:
44 10 62 123 15 0 0 0
Elements popped:
15 123 62 10 44
peek()
The peek() is an operation retrieves the topmost element within the stack,
without deleting it. This operation is used to check the status of the stack with
the help of the top pointer.
Algorithm
1. START
3. END
Example
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int isfull(){
if(top == MAXSIZE)
return 1;
else
return 0;
}
/* Function to return the topmost element in the stack */
int peek(){
return stack[top];
if(!isfull()) {
top = top + 1;
stack[top] = data;
} else {
/* Main function */
int main(){
int i;
push(44);
push(10);
push(62);
push(123);
push(15);
return 0;
Output
Stack Elements:
44 10 62 123 15 0 0 0
isFull()
isFull() operation checks whether the stack is full. This operation is used to
check the status of the stack with the help of top pointer.
Algorithm
1. START
2. If the size of the stack is equal to the top position of the stack, the stack is
full. Return 1.
3. Otherwise, return 0.
4. END
Example
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int isfull(){
if(top == MAXSIZE)
return 1;
else
return 0;
/* Main function */
int main(){
return 0;
Output
isEmpty()
The isEmpty() operation verifies whether the stack is empty. This operation is
used to check the status of the stack with the help of top pointer.
Algorithm
1. START
3. Otherwise, return 0.
4. END
Example
Following are the implementations of this operation in various programming
languages −
#include <stdio.h>
int MAXSIZE = 8;
int stack[8];
int isempty() {
if(top == -1)
return 1;
else
return 0;
/* Main function */
int main() {
return 0;
}
Output