SPECIAL STACK USING C
//This is a special stack in which popped value is replaced with -1 and value insertion is done at the position where -1 is found first from the bottom.
#include<stdio.h>
#include<conio.h>
#define max 50
typedef struct stack
{
int ar[max];
int top;
}st;
void push(st *,int);
int pop(st *);
int peek(st);
int isfull(st);
int isempty(st);
void disp(st);
void main()
{
int ch,g;
st s;
s.top=-1;
while(1)
{
printf("\n1)Push\n2)Pop\n3)Peek\n4)Disp\n5)Exit");
printf("\n Enter your choice ; ");
scanf("%d",&ch);
switch(ch)
{
case 1: if(!isfull(s))
{
printf("Enter the value : ");
scanf("%d",&g);
push(&s,g);
}
else
printf("No More Space");
break;
case 2: if(!isempty(s))
printf("Popped value is %d",pop(&s));
else
printf("Nothing to display ");
break;
case 3: if(!isempty(s))
{
printf("Top value is %d",peek(s));
}
else
printf("Nothing to display");
break;
case 4: if(!isempty(s))
{
printf("Values in the stack are : ");
disp(s);
}
else
printf("Nothing to display");
break;
case 5: exit(0);
}
}
}
void push(st *s,int g)
{
int p=s->top,t;
while(p>=0)
{
if(s->ar[p]==-1)
t=p;
p--;
}
if(t>=0)
s->ar[t]=g;
else
s->ar[++s->top]=g;
}
int pop(st *s)
{
int t,p;
t=s->top;
while(t>=0)
{
if(s->ar[t]>=0)
break;
t--;
}
p=s->ar[t];
s->ar[t]=-1;
return p;
}
int peek(st s)
{
return s.ar[s.top];
}
int isfull(st s)
{
if(s.top==max-1)
return 1;
else
return 0;
}
int isempty(st s)
{
if(s.top==-1)
return 1;
else
return 0;
}
void disp(st s)
{
int p=s.top;
printf("\n\n");
while(p>=0)
{
printf("|%d|\n",s.ar[p]);
p--;
}
}
#include<stdio.h>
#include<conio.h>
#define max 50
typedef struct stack
{
int ar[max];
int top;
}st;
void push(st *,int);
int pop(st *);
int peek(st);
int isfull(st);
int isempty(st);
void disp(st);
void main()
{
int ch,g;
st s;
s.top=-1;
while(1)
{
printf("\n1)Push\n2)Pop\n3)Peek\n4)Disp\n5)Exit");
printf("\n Enter your choice ; ");
scanf("%d",&ch);
switch(ch)
{
case 1: if(!isfull(s))
{
printf("Enter the value : ");
scanf("%d",&g);
push(&s,g);
}
else
printf("No More Space");
break;
case 2: if(!isempty(s))
printf("Popped value is %d",pop(&s));
else
printf("Nothing to display ");
break;
case 3: if(!isempty(s))
{
printf("Top value is %d",peek(s));
}
else
printf("Nothing to display");
break;
case 4: if(!isempty(s))
{
printf("Values in the stack are : ");
disp(s);
}
else
printf("Nothing to display");
break;
case 5: exit(0);
}
}
}
void push(st *s,int g)
{
int p=s->top,t;
while(p>=0)
{
if(s->ar[p]==-1)
t=p;
p--;
}
if(t>=0)
s->ar[t]=g;
else
s->ar[++s->top]=g;
}
int pop(st *s)
{
int t,p;
t=s->top;
while(t>=0)
{
if(s->ar[t]>=0)
break;
t--;
}
p=s->ar[t];
s->ar[t]=-1;
return p;
}
int peek(st s)
{
return s.ar[s.top];
}
int isfull(st s)
{
if(s.top==max-1)
return 1;
else
return 0;
}
int isempty(st s)
{
if(s.top==-1)
return 1;
else
return 0;
}
void disp(st s)
{
int p=s.top;
printf("\n\n");
while(p>=0)
{
printf("|%d|\n",s.ar[p]);
p--;
}
}
Comments
Post a Comment