Table of contents
No headings in the article.
In C, the Stack data structure is an ordered, linear sequence of items. It is a LIFO (Last In First Out) data structure, meaning we can only insert or remove an item at the top of the stack. In array implementation, the stack is formed by using the array. All the operations regarding the stack are performed using arrays. Let's see how each operation can be implemented on the stack using an array data structure.
The stack includes these functions
Push
Pop
Print
Peek
Is Empty
Is Full
#include<stdio.h>
#include<stdlib.h>
void push(int);
void pop();
void print();
void display();
int is_full();
int is_empty();
int peek();
int TOP = -1;
#define SIZE 10
int stack[SIZE];
void main(){
int op,ele;
while(1){
printf("\n*********\n");
printf("1.Push\n2.Pop\n3.Print\n4.Peek\n5.Is full\n6.Is empty.\n7.Exit\n8.Display\n");
printf("***********\n\nEnter your option: ");
scanf("%d",&op);
switch (op)
{
case 1: printf("\nEnter element: ");
scanf("%d",&op);
push(op);
break;
case 2: pop();
break;
case 3: print();
break;
case 4: printf("\nPeek = %d\n",peek());
break;
case 5:
if(is_full())
printf("\nStack is overlow.\n");
else
printf("\nStack is not full.\n");
break;
case 6:
if(is_empty())
printf("\nStack is underlow.\n");
else
printf("\nStack is not empty.\n");
break;
case 7: exit(1);
break;
case 8: display();
break;
default: printf("\nEnter valid option.\n");
break;
}
}
}
void push(int in){
if(is_full())
printf("\nStack is over flow.\n");
else
stack[++TOP]=in;
}
void pop(){
if(is_empty())
printf("\nStack is under flow.\n");
else
printf("\n%d is poped.\n",stack[TOP--]);
}
int is_full(){
if(TOP == SIZE-1)
return 1;
else
return 0;
}
int is_empty(){
if(TOP == -1)
return 1;
else
return 0;
}
void print(){
if(is_empty())
printf("\nStack is underflow.\n");
else
printf("\nStack elements are: ");
for(int i=0;i<=TOP;i++)
printf("%d ",stack[i]);
printf("\n");
}
int peek(){
return stack[TOP];
}
void display(){
printf("\nWhole Stack is : ");
for(int i=0;i<SIZE;i++)
printf("%d ",stack[i]);
printf("\n");
}
This is a C program that implements a stack data structure using an array. A stack is a Last In First Out (LIFO) data structure where elements are added (pushed) to the top and removed (popped) from the top. The program implements the following operations:
Push - to add an element to the top of the stack.
Pop - to remove an element from the top of the stack.
Print - to display the elements of the stack.
Peek - to return the top element of the stack without removing it.
is_full - to check if the stack is full.
is_empty - to check if the stack is empty.
Exit - to exit the program.
Display - to display the whole stack.
The stack is implemented as an array with a fixed size defined by the macro SIZE and a TOP pointer that points to the top of the stack. The program implements error handling to prevent stack overflow and underflow.
The output of the above program will be generated like this
*********
1.Push
2.Pop
3.Print
4.Peek
5.Is full
6.Is empty.
7.Exit
8.Display
***********
Enter your option: 1
Enter element: 1
*********
1.Push
2.Pop
3.Print
4.Peek
5.Is full
6.Is empty.
7.Exit
8.Display
***********
Enter your option: 1
Enter element: 2
*********
1.Push
2.Pop
3.Print
4.Peek
5.Is full
6.Is empty.
7.Exit
8.Display
***********
Enter your option: 1
Enter element: 3
*********
1.Push
2.Pop
3.Print
4.Peek
5.Is full
6.Is empty.
7.Exit
8.Display
***********
Enter your option: 3
Stack elements are: 1 2 3
*********
1.Push
2.Pop
3.Print
4.Peek
5.Is full
6.Is empty.
7.Exit
8.Display
***********
Enter your option: 2
3 is poped.
*********
1.Push
2.Pop
3.Print
4.Peek
5.Is full
6.Is empty.
7.Exit
8.Display
***********
Enter your option: 4
Peek = 2
*********
1.Push
2.Pop
3.Print
4.Peek
5.Is full
6.Is empty.
7.Exit
8.Display
***********
Enter your option: 5
Stack is not full.
*********
1.Push
2.Pop
3.Print
4.Peek
5.Is full
6.Is empty.
7.Exit
8.Display
***********
Enter your option: 6
Stack is not empty.
*********
1.Push
2.Pop
3.Print
4.Peek
5.Is full
6.Is empty.
7.Exit
8.Display
***********
Enter your option: 7
press any key to continue . . .