Please spot the errors and right the correct code back .


Thanks in advance
amit kumar

#include<stdio.h>
#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 \n ",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("\n enter push or pop or show ,1,2,3");
	scanf(" %d ",&n);

	switch(n)
{
	case 1:

		printf("\n 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("\n wrong case entered ");
}
}

	return 0;

}

If you need help, then this isn't a code snippet. Please post as a regular thread.

Read the comments.

#include<stdio.h>
#define max 10

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


int stackfull(stack *s) //SHOULD BE OF TYPE BOOL RETURN
{
	if(s->top==max) // IF FULL
	return 0;  //SHOULD RETURN true
	else
	return 1; //ELSE RETURN false
}

	int stackempty(stack *s) //BOOL RETURN TYPE?
{
       // if(! (s->top == -1) // see the difference
	if(!(s->top=-1)) // JUST DO RETURN  top == -1. 
	return 0;
	else
	return 1;
}
	void push(stack *stk,char data)
{
	if(!(stackfull(stk)))
	stk->entry[stk->top++]=data;/*try ++top */
   /* maybe something like this :
        if( ! stackfull( stk) )
        {
             stk->top++;
            stk->entry[str->top] = data;
         }
    */
}   


	char pop(stack *stk) //WHY IS THE RETURN TYPE CHAR ?
{	if(!(stackempty(stk)))
	return(stk->entry[stk->top--]);
        else
        return NULL;
}

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

	while(!(temp<0))
	printf(" %c \n ",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("\n enter push or pop or show ,1,2,3");
	scanf(" %d ",&n);

	switch(n)
{
	case 1:

		printf("\n 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("\n wrong case entered ");
}
}

	return 0;

}

// AND THERE IS PROBABLY MORE PROBLEMS WITH IT, BUT IT GETS
YOU STARTED.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.