STACK ERRORS - Programmers Heaven

Howdy, Stranger!

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

Categories

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

  • johny_1015_198johny_1015_198 Posts: 5Member
    Hello,
    Sorry to say that your entire concept of STACK is wrong as well as your entire program. Which leads you to wrong use of array. use the counter outside the struct. I believe it will be your best solution.

    Johny
Sign In or Register to comment.