Program to perform operations on a stack in C language.
#include<stdio.h>
int stack[100], n, i, top=-1;
void main()
{
int choice;
printf("Enter size of stack: ");
scanf("%d",&n);
do
{
printf("\n----MENU----\n1. PUSH\n2. POP\n3. PEEP\n4. DISPLAY\n5. CHANGE\n6. EXIT\n");
printf("Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: peep();
break;
case 4: display();
break;
case 5: change();
break;
int stack[100], n, i, top=-1;
void main()
{
int choice;
printf("Enter size of stack: ");
scanf("%d",&n);
do
{
printf("\n----MENU----\n1. PUSH\n2. POP\n3. PEEP\n4. DISPLAY\n5. CHANGE\n6. EXIT\n");
printf("Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: peep();
break;
case 4: display();
break;
case 5: change();
break;
case 6:
break;
default: printf("Enter valid choice.\n");
break;
}
}while(choice!=6);
}
void push()
{
int value;
if(top>=n-1)
{
printf("Stack is overflow.\n");
}
else
{
printf("Enter element to insert: ");
scanf("%d",&value);
top++;
stack[top]= value;
printf("Element Inserted.\n");
}
}
void pop()
{
int value;
if(top<=-1)
{
printf("Stack is underflow.\n");
}
else
{
value = stack[top];
top--;
printf("Element %d Deleted.\n",value);
}
}
void peep()
{
int value;
if(top<=-1)
{
printf("Stack is underflow.\n");
}
else
{
value = stack[top];
printf("Element at top is %d.\n",value);
}
}
void change()
{
int value, position;
if(top<=-1)
{
printf("Stack is underflow.\n");
}
else
{
printf("Enter index of stack to get value: ");
scanf("%d",&position);
if(position<=n && position>-1)
{
value = stack[position];
printf("Element at top is %d.\n",value);
}
else
{
printf("Entered position is not valid.\n");
}
}
}
void display()
{
int value;
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
}
break;
default: printf("Enter valid choice.\n");
break;
}
}while(choice!=6);
}
void push()
{
int value;
if(top>=n-1)
{
printf("Stack is overflow.\n");
}
else
{
printf("Enter element to insert: ");
scanf("%d",&value);
top++;
stack[top]= value;
printf("Element Inserted.\n");
}
}
void pop()
{
int value;
if(top<=-1)
{
printf("Stack is underflow.\n");
}
else
{
value = stack[top];
top--;
printf("Element %d Deleted.\n",value);
}
}
void peep()
{
int value;
if(top<=-1)
{
printf("Stack is underflow.\n");
}
else
{
value = stack[top];
printf("Element at top is %d.\n",value);
}
}
void change()
{
int value, position;
if(top<=-1)
{
printf("Stack is underflow.\n");
}
else
{
printf("Enter index of stack to get value: ");
scanf("%d",&position);
if(position<=n && position>-1)
{
value = stack[position];
printf("Element at top is %d.\n",value);
}
else
{
printf("Entered position is not valid.\n");
}
}
}
void display()
{
int value;
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
}