Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

STACK ERRORS

/* Spot errors in the following code */


#include
#define max 10

typedef struct
{
char entry[max];
int top;
}stack;


int stackfull(stack *s)
{
if(s->top==max)
return 0;
else
return 1;
}

int stackempty(stack *s)
{
if(!(s->top=-1))
return 0;
else
return 1;
}
void push(stack *stk,char data)
{
if(!(stackfull(stk)))
stk->entry[stk->top++]=data;/*try ++top */
}

char pop(stack *stk)
{ if(!(stackempty(stk)))
return(stk->entry[stk->top--]);
else
return NULL;
}

void show(stack *stk)
{
int temp=stk->top;

while(!(temp<0))
printf(" %c
",stk->entry[temp--]);
}

void createstack(stack *s)
{
s->top=-1;
}
char stacktop(stack *stk)
{
return (stk->entry[stk->top]);
}

void clearstack(stack *stk)
{
stk->top=-1;
}
int stacksize(stack *stk)
{
return ((stk->top)+1);
}


int main()
{

stack stk;
createstack(&stk);
int n,k;
char data;

for(k=0;k<4;++k) /* test */
{
printf("
enter push or pop or show ,1,2,3");
scanf(" %d ",&n);

switch(n)
{
case 1:

printf("
enter data ");
scanf(" %c ",&data);
push(&stk,data);
break;
case 2:

printf(" the data removed is %c",pop(&stk));
break;
case 3:
show(&stk);
printf(" the stack top is %c ",stacktop(&stk));
printf(" the stack size is %d ",stacksize(&stk));
clearstack(&stk);
show(&stk);
break;





default: printf("
wrong case entered ");
}
}

return 0;

}


Comments

Sign In or Register to comment.