Stack using Arrays using C
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void push(int a[],int*tp)
{
int data,choice=1;
while(choice==1)
{
printf("enter the element to be inserted");
scanf("%d",&data);
*tp = *tp+1;
a[*tp]=data;
printf("want another data to be pushed (0,1): ");
scanf("%d",&choice);
}
}
void pop(int a[],int*tp)
{
int data,choice=1;
while(choice==1)
{
data=a[*tp];
*tp=*tp-1;
printf("deleted %d from stack\n",data);
printf("want another data to be popped (0,1): ");
scanf("%d",&choice);
}
}
void display(int a[],int*tp)
{
int i;
for(i=*tp;i>=0;i--)
printf("%d\n",a[i]);
}
int main()
{
int arr[100],top=-1,i,choice=1,ch;
while(choice==1)
{
printf("enter 1. to Push element in stack\n");
printf("enter 2. to pop element in stack\n");
printf("enter 3. to display elements of stack\n");
scanf("%d",&ch);
if(ch==1)
{
if(top==99)
printf("Stack overflow");
else
push(arr,&top);
}
else if(ch==2)
{
if(top==-1)
printf("stack underflow");
else
pop(arr,&top);
}
else if(ch==3)
{
display(arr,&top);
}
else
printf("Invalid Choice");
printf("Do you want to continue (0,1): ");
scanf("%d",&choice);
}
}
Comments
Post a Comment