This Program is a DATA STRUCTURE program of a stack to a reverse a string using static stack.
The output of the program is correct and is compiled correctly..... Your feedback is welcomed
#include<stdio.h>
#define max 10
typedef struct stack
{
char data[max];
int top;
}stack;
void init(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,char x)
{
s->top=s->top+1;
s->data[s->top]=x;
}
char pop(stack *s)
{
char x;
x=s->data[s->top];
s->top=s->top-1;
return x;
}
int main()
{
stack s;
char x;
int i;
init(&s);
printf("Enter a string\n");
while((x=getchar())!='\n')
{
if(!full(&s))
push(&s,x);
else
{
printf("stack is full\n");
}
}
printf("reverse string is\n");
while(!empty(&s))
{
x=pop(&s);
printf("%c",x);
}
printf("\n");
}
OUTPUT IS -
Enter a string
NAKODA
reverse string is
ADOKAN
The output of the program is correct and is compiled correctly..... Your feedback is welcomed
#include<stdio.h>
#define max 10
typedef struct stack
{
char data[max];
int top;
}stack;
void init(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,char x)
{
s->top=s->top+1;
s->data[s->top]=x;
}
char pop(stack *s)
{
char x;
x=s->data[s->top];
s->top=s->top-1;
return x;
}
int main()
{
stack s;
char x;
int i;
init(&s);
printf("Enter a string\n");
while((x=getchar())!='\n')
{
if(!full(&s))
push(&s,x);
else
{
printf("stack is full\n");
}
}
printf("reverse string is\n");
while(!empty(&s))
{
x=pop(&s);
printf("%c",x);
}
printf("\n");
}
OUTPUT IS -
Enter a string
NAKODA
reverse string is
ADOKAN
No comments:
Post a Comment