Mistu4u
Mistu4u

Reputation: 5416

Correct value of the user input is not getting inserted into the stack

So, I wrote a program to insert a user input to a stack myself. But despite my rigorous trying, I could not insert the data correctly. It show data has been inserted, but while showing, garbage value is shown. Here is my main function:

//Stack
#include<stdio.h>
#include<stdlib.h>
#define MAXSTK 10

void push(int *, int, int *, int);
//void pop();
void show_stack();
int main()
{  
int ch, ch1, stack[MAXSTK], top=-1;
do{ 
    printf("\n <<Stack MENU>>");
printf("1. Add Element");
printf("2. Delete Element");
printf("3. Show Stack");
printf("4. Exit menu");
printf("\n Enter your choice->");
scanf("%d", &ch);

    switch(ch)
    {
        case 1: printf("\n Enter element to add->");
            scanf("%d",&ch1);
            push(stack,ch1, &top, MAXSTK);
            break;
          /*   case 2:      pop();
            break;*/
        case 3: printf("\n The stack is->");
            show_stack(stack, MAXSTK);
            break;
        default: printf("\n Invalid Choice!!!");
            break;
       }
 }while(ch!=4);
return 0;
   }

And here is my push function:

void push(int newstack[], int num, int *newtop, int bound)
 {
 *newtop=*newtop+1;
if(*newtop==0)
printf("\n Stack was Empty. New Value inserted.");

if(*newtop>(bound-1))
{
    printf("\n Caution! OVERFLOW!!!");

}
newstack[*newtop]=num;
}

And here is my show function:

void show_stack(int newstack[], int bound)
{
int i;
printf("\n");
for(i=0;i<=bound;i++)
printf("%d",newstack[i]);
 }     

Please help me finding the error.

Upvotes: 2

Views: 143

Answers (2)

Amarnath Krishnan
Amarnath Krishnan

Reputation: 1263

You are passing the array length and printing all array elements. so you see garbage value. Try to print only the inserted elements.

 show_stack(stack, top);

and your function prototype should be

void show_stack(int *,int);

you increment your newtop everytime regardless of overflow. it's a bad practice. It will cause issues while popping() and show_stack(). you can do something like this to avoid it.

void push(int newstack[], int num, int *newtop, int bound)
{
    // if newtop is < 0 display the message
    if(*newtop<0)
       printf("\n Stack was Empty. New Value inserted.");
    // newtop will always point to top element. so if newtop is 9 it means your stack is full. so if newtop is >= bound-1(9) stack is full
    if(*newtop>=(bound-1))
       printf("\n Caution! OVERFLOW!!!");
    else
    {
      *newtop=*newtop+1; //increment newtop
      newstack[*newtop]=num; //store value in newtop
    }
}

Upvotes: 5

Hilborn
Hilborn

Reputation: 754

You are calling show_stack with the capacity (MAXSTK), not its actual size. Therefore it will display all elements in stack, whatever values they have. Simply calling it with top instead should fix the problem.

Another note: Your declaration of show_stack does not match the argument list of the implementation.

Upvotes: 4

Related Questions