#include<stdio.h>
#include<stdlib.h>
#define structsize 10
struct stack
{
int top;
int item[structsize];
};
void push(struct stack *ps)
{
int element;
printf("\npush an element:");
scanf("%d",&element);
if(ps->top==(structsize-1)){
printf("overflow\n");
exit(0);
}
else
{
ps->item[++ps->top]=element;
}
}
void pop(struct stack *ps)
{
int element;
if(ps->top==-1){
printf("underflow flow\n");
exit(0);
}
else
{
element=ps->item[ps->top];
ps->top--;
printf("\npoped element is :%d",element);
}
}
void display(struct stack *ps)
{
int i;
printf("\ncurrent stack is:\n");
for(i=0;i<=(ps->top);i++)
{
printf("\n %d",ps->item[i]);
}
}
int main()
{
int arr[50],ch;
struct stack s;
s.top=-1;
while(1)
{
printf("\n\nEnter \n1: push\n2:pop\n3:print stack \n4:exit \n\n enter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:push(&s);
break;
case 2:pop(&s);
break;
case 3:display(&s);
break;
default:exit(0);
}
}
return 0;
}
#include<stdlib.h>
#define structsize 10
struct stack
{
int top;
int item[structsize];
};
void push(struct stack *ps)
{
int element;
printf("\npush an element:");
scanf("%d",&element);
if(ps->top==(structsize-1)){
printf("overflow\n");
exit(0);
}
else
{
ps->item[++ps->top]=element;
}
}
void pop(struct stack *ps)
{
int element;
if(ps->top==-1){
printf("underflow flow\n");
exit(0);
}
else
{
element=ps->item[ps->top];
ps->top--;
printf("\npoped element is :%d",element);
}
}
void display(struct stack *ps)
{
int i;
printf("\ncurrent stack is:\n");
for(i=0;i<=(ps->top);i++)
{
printf("\n %d",ps->item[i]);
}
}
int main()
{
int arr[50],ch;
struct stack s;
s.top=-1;
while(1)
{
printf("\n\nEnter \n1: push\n2:pop\n3:print stack \n4:exit \n\n enter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:push(&s);
break;
case 2:pop(&s);
break;
case 3:display(&s);
break;
default:exit(0);
}
}
return 0;
}
તમારા લેખોનું નિયમીત વાંચન થી સી પ્રોગ્રામિંગ માં ઘણીજ મદદ મળી રહે છે
ReplyDelete