C Program to implement basic operation of Stacks using arrays and pointers
Program
#include<stdio.h>
#include<stdlib.h>
/* Defines the stack size */
#define SIZE 5
/* Function Declarations */
int isFull(int);
int isEmpty(int);
void push(int*, int [], int);
void pop(int*, int []);
void display(int, int []);
int isFull(int top)
{
if(top == SIZE - 1)
return 1;
else
return 0;
}
int isEmpty(int top)
{
if(top == -1)
return 1;
else
return 0;
}
void push(int *top, int s[], int item)
{
if(isFull(*top))
{
printf("Stack overflow\nCannot Insert\n");
return;
}
printf("Item to be pushed is: %d at location %d\n", item, *top + 1);
(*top)++;
s[*top] = item;
}
void pop(int *top, int s[])
{
if(isEmpty(*top))
{
printf("Stack underflow\nNo elements in the stack\n");
return;
}
printf("Item to be deleted is %d at location %d\n", s[*top], *top);
(*top)--;
}
void display(int top, int s[])
{
int i;
if(top == -1)
{
printf("Stack underflow\nNo elements in the stack\n");
return;
}
else
{
printf("Contents of stack are:\n");
for(i = top; i >= 0; i--)
{
printf("|--|\n");
printf("|%d|\n",s[i]);
}
}
}
void main()
{
int choice, item, s[SIZE], top;
top = -1;
printf("Welcome to Stack Program using arrays and pointers\n");
for(;;)
{
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter the choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Please enter an item to be inserted\n");
scanf("%d",&item);
push(&top, s, item);
break;
case 2: pop(&top, s);
break;
case 3: display(top, s);
break;
default: exit(0);
}
}
}
Output
Welcome to Stack Program using arrays and pointers
1. Push
2. Pop
3. Display
4. Exit
Enter the choice
1
Please enter an item to be inserted
10
Item to be pushed is: 10 at location 0
1. Push
2. Pop
3. Display
4. Exit
Enter the choice
2
Item to be deleted is 10 at location 0
1. Push
2. Pop
3. Display
4. Exit
Enter the choice
3
Stack underflow
No elements in the stack
1. Push
2. Pop
3. Display
4. Exit
Enter the choice
2
Stack underflow
No elements in the stack
1. Push
2. Pop
3. Display
4. Exit
Enter the choice
1
Please enter an item to be inserted
10
Item to be pushed is: 10 at location 0
1. Push
2. Pop
3. Display
4. Exit
Enter the choice
1
Please enter an item to be inserted
20
Item to be pushed is: 20 at location 1
1. Push
2. Pop
3. Display
4. Exit
Enter the choice
1
Please enter an item to be inserted
30
Item to be pushed is: 30 at location 2
1. Push
2. Pop
3. Display
4. Exit
Enter the choice
1
Please enter an item to be inserted
40
Item to be pushed is: 40 at location 3
1. Push
2. Pop
3. Display
4. Exit
Enter the choice
1
Please enter an item to be inserted
50
Item to be pushed is: 50 at location 4
1. Push
2. Pop
3. Display
4. Exit
Enter the choice
1
Please enter an item to be inserted
60
Stack overflow
Cannot Insert
1. Push
2. Pop
3. Display
4. Exit
Enter the choice
3
Contents of stack are:
|--|
|50|
|--|
|40|
|--|
|30|
|--|
|20|
|--|
|10|
1. Push
2. Pop
3. Display
4. Exit
Enter the choice
4