0

I have a stack where I want to push distinct elements. I wrote the following code. But it always checks only first element. If I enter a duplicate value first value, it doesn't accept that, but if I enter a duplicate value of 2nd or 3rd value it accepts it. For example if I enter 20 and again enter 20 it will not accept the second, but if I enter 20, 22, 22 it accepts the duplicate. How can I prevent this?

#include <stdio.h>
#define MAXSIZE 5

struct stack
{
    int stk[MAXSIZE];
    int top;
};
typedef struct stack STACK;
STACK s;

void push(void);
int  pop(void);
void display(void);

void main ()
{
    int choice;
    int option = 1;
    s.top = -1;

    printf ("STACK OPERATION\n");
    while (option)
    {
        printf ("------------------------------------------\n");
        printf ("      1    -->    Bus Status               \n");
        printf ("      2    -->    Enter Bus              \n");
        printf ("      3    -->    Exit Bus               \n");
        printf ("      4    -->    EXIT           \n");
        printf ("------------------------------------------\n");

        printf ("Enter your choice\n");
        scanf    ("%d", &choice);
        switch (choice)
        {
        case 1:
            display();
            break;
        case 2:
            push();
            break;
        case 3:
            pop();
            break;
        case 4:
            return;
        }
        fflush (stdin);
        printf ("Do you want to continue(Type 0 or 1)?\n");
        scanf    ("%d", &option);
    }
}

/*  Function to add an element to the stack */
void push ()
{
    int num,i,j,status=0;
    if (s.top == (MAXSIZE - 1))
    {
        printf ("Terminal is Full\n");
        return;
    }
    else
    {

        printf ("Enter the Bus Number\n");
        scanf ("%d", &num);
        if(num<1 || num>30) {
            printf("This bus Does not Exist.\n");
        }
            for(i=0;i<5;i++){
                if(s.stk[i]==num){
                    printf("Bus Already in the Terminal\n");
                    break;
                }
                else {
                    s.top = s.top + 1;
                    s.stk[s.top] = num;
                    status=1;
                    break;
                }
            }
        if(status==1)
            printf("Bus %d Successfully Entered\n", num);

    }
    return;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Nishan Singha
  • 169
  • 1
  • 3
  • 13
  • 1
    You should learn to use a debugger and step through your code line by line. You can then look at the state of the program and watch what is happening. – crashmstr Mar 15 '16 at 17:24
  • Completely agree with @crashmstr. However, I will provide you a hint: you must check the whole stk before pushing a bus --> the "else" statement is not in the correct position – vad Mar 15 '16 at 17:49
  • You have defined a constant, which is a good practice. To use it would be a better practice: `for(i=0;i<5;i++)` – MikeC Mar 15 '16 at 17:51
  • Unless you're working on Windows, [Using `fflush(stdin)`](http://stackoverflow.com/questions/2979209/using-fflushstdin) probably isn't appropriate. Similarly for [What should `main()` return in C and C++?](http://stackoverflow.com/questions/204476/) – Jonathan Leffler Mar 15 '16 at 17:52
  • Do you know anything about the range of inputs that is valid? If the range is small, you could use an array and set corresponding elements to 1 if their index has been added to the list and 0 if not. – akinfermo Mar 15 '16 at 18:54

1 Answers1

2

You're bus insert loop is looping through and adds a bus the first time it finds s.stk[i] != num. You need to check to see if the bus is in the station ie search the whole stack before inserting a new bus.

for(i=0;i<5;i++) {
  if(s.stk[i]==num){
    printf("Bus Already in the Terminal\n");
    break;
  }
  else {
    s.top = s.top + 1;
    s.stk[s.top] = num;
    status=1;
    break;
  }
}
Harry
  • 11,298
  • 1
  • 29
  • 43