Stack using Array in C
/* stacks using arrays */
#include<stdio.h>
#include<conio.h>
#define MAX 6
typedef struct stack
{
int data[MAX];
int top;
} stack;
void initialize(stack *);
int empty(stack *);
int full(stack *);
int pop(stack *);
void push(stack *, int);
void print(stack *);
void main()
{
stack s;
int x,op;
initialize(&s);
clrscr();
do
{
printf("\n 1.push \n2.pop \n3.print \n4.quit");
printf("\n enter your choice");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\n enter a number");
scanf("%d",&x);
if(!full(&s))
push(&s,x);
else
printf("\n stack is full");
break;
case 2:
if(!empty(&s))
{
x=pop(&s);
printf("\n popped value is=%d",x);
}
else
printf("stack is empty");
break;
case 3:
print(&s);
break;
}
}
while(op!=4);
}
void initialize(stack *s)
{
s->top=-1;
}
int empty(stack *s)
{
if(s->top==-1)
return(1);
return 0;
}
int full(stack *s)
{
if(s->top==MAX-1)
return (1);
return (0);
}
void push(stack *s,int x)
{
s->top=s->top+1;
s->data[s->top]=x;
}
int pop(stack *s)
{
int x;
x=s->data[s->top];
s->top=s->top-1;
return (x);
}
void print(stack *s)
{
int i;
printf("\n");
for(i=s->top;i>=0;i--)
printf("%d",s->data[i]);
}
#include<stdio.h>
#include<conio.h>
#define MAX 6
typedef struct stack
{
int data[MAX];
int top;
} stack;
void initialize(stack *);
int empty(stack *);
int full(stack *);
int pop(stack *);
void push(stack *, int);
void print(stack *);
void main()
{
stack s;
int x,op;
initialize(&s);
clrscr();
do
{
printf("\n 1.push \n2.pop \n3.print \n4.quit");
printf("\n enter your choice");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\n enter a number");
scanf("%d",&x);
if(!full(&s))
push(&s,x);
else
printf("\n stack is full");
break;
case 2:
if(!empty(&s))
{
x=pop(&s);
printf("\n popped value is=%d",x);
}
else
printf("stack is empty");
break;
case 3:
print(&s);
break;
}
}
while(op!=4);
}
void initialize(stack *s)
{
s->top=-1;
}
int empty(stack *s)
{
if(s->top==-1)
return(1);
return 0;
}
int full(stack *s)
{
if(s->top==MAX-1)
return (1);
return (0);
}
void push(stack *s,int x)
{
s->top=s->top+1;
s->data[s->top]=x;
}
int pop(stack *s)
{
int x;
x=s->data[s->top];
s->top=s->top-1;
return (x);
}
void print(stack *s)
{
int i;
printf("\n");
for(i=s->top;i>=0;i--)
printf("%d",s->data[i]);
}
0 comments:
Post a Comment