Hello again guys,

I got here a Stack and Queue Array program. My problem is deleting an item in Queue. Here's a quick video of my problem:

http://www.youtube.com/watch?v=X_G82aU_LV4

Here's my code:

#include <stdio.h>
#include <conio.h>

int main()
{
 int a[100],n=0,i;
 int cho,choi,choice;
 clrscr();
 do
 {
  printf("\n\n\n [1] Stack\n");
  printf(" [2] Queue\n");
  printf(" [3] Exit\n");
  printf("\n Enter your choice: ");
  scanf("%d", &cho);
  switch (cho)
  {
      case 1:
      {
    do
      {
        printf("\n\n\n [1] Push items\n");
        printf(" [2] Pop items\n");
        printf(" [3] Display items\n");
        printf(" [4] Exit\n");
        printf("\n Enter your choice: ");
        scanf("%d", &choi);
     switch (choi)
        {
      case 1:
      {
       clrscr();
       printf("\n\n Enter integer for total numbers to be sorted: ");
        scanf("%d", &n);
        printf("\n\n");
        for (i = 0; i < n; i++)
        {
         printf(" Enter integer for no. %d : ", i + 1);
         scanf("%d", &a[i]);
       }
        break;
      }
       case 2:
       {
       clrscr();
       if (n == 0)
        {
        printf("\n The stack is empty.");
        }
       else
        {
         n = n - 1;
         printf("\n The poped element is %d.", a[n]);
       }
        break;
      }
       case 3:
       {
       clrscr();
       if (n == 0)
        {
        printf("\n The stack is empty.");
       }
       else
       {
         printf("\n The stack contains: ");
         for (i = 0; i < n; printf("%3d", a[i]), i++);
       }
        break;
      }
      case 4:
      {
       break;
      }
      default:
       {
       printf("\n\n Please enter the right choice.");
       break;
       }
     }
    }while (choi != 4);
    return 0;
   }
   case 2:
   {
    int rear=-1,n=-1;
    do
    {
        printf("\n\n\n [1] Queue items\n");
        printf(" [2] Deque items\n");
        printf(" [3] Display items\n");
        printf(" [4] Exit\n");
        printf("\n Enter your choice: ");
        scanf("%d", &choice);
     switch (choice)
        {
      case 1:
      {
       clrscr();
       printf("\n\n Enter integer for total numbers to be sorted: ");
        scanf("%d", &n);
        printf("\n\n");
        for (i = 0; i < n; i++)
        {
         printf(" Enter integer for no. %d : ", i + 1);
         scanf("%d", &a[i]);
       }
        break;
      }
       case 2:
       {
       clrscr();
       if (n == -1)
        {
        printf("\n The stack is empty.");
        }
       else
        {
         printf("\n The poped element is %d.", a[n]);
        if(n == rear)
        {
         n = rear=-1;
        }
        else
        {
         n++;
        }
       }
        break;
      }
       case 3:
       {
       clrscr();
       if (n == -1)
        {
        printf("\n The stack is empty.");
       }
       else
       {
         printf("\n The stack contains: ");
         for (i = 0; i < n; printf("%3d", a[i]), i++);
       }
        break;
      }
      case 4:
      {
       break;
      }
      default:
       {
       printf("\n\n Please enter the right choice.");
       break;
       }
     }
    }while (choice != 4);
    return 0;
   }
  }
 }while (cho != 3);
}

Anyone?

Hello guys,

I already fixed the "Dequeue" function in Queue. However, there's a little bit problem.

I inputted 4 integers in an array: 2 8 3 6

When I try to dequeue, the result would be this:

The poped element is 0.

instead this:

The poped element is 2.

and so on...


Here's my code:

#include <stdio.h>
#include <conio.h>

int main()
{
 int a[100],n=0,i;
 int cho,choi,choice;
 clrscr();
 do
 {
  printf("\n\n\n [1] Stack\n");
  printf(" [2] Queue\n");
  printf(" [3] Exit\n");
  printf("\n Enter your choice: ");
  scanf("%d", &cho);
  switch(cho)
  {
      case 1:
      {
    do
      {
        printf("\n\n\n [1] Push items\n");
        printf(" [2] Pop items\n");
        printf(" [3] Display items\n");
        printf(" [4] Back to main\n");
        printf("\n Enter your choice: ");
        scanf("%d", &choi);
     switch (choi)
        {
      case 1:
      {
       clrscr();
       printf("\n\n Enter integer for total numbers to be sorted: ");
        scanf("%d", &n);
        printf("\n\n");
        for (i = 0; i < n; i++)
        {
         printf(" Enter integer for no. %d : ", i);
         scanf("%d", &a[i]);
       }
        break;
      }
       case 2:
       {
       clrscr();
       if (n == 0)
        {
        printf("\n The stack is empty.");
        }
       else
        {
         n--;
         printf("\n The poped element is %d.", a[n]);
       }
        break;
      }
       case 3:
       {
       clrscr();
       if (n == 0)
        {
        printf("\n The stack is empty.");
       }
       else
       {
         printf("\n The stack contains: ");
         for (i = n; i > 0; i--)
        {
         printf("%3d", a[i-1]);
        }
       }
        break;
      }
      case 4:
      {
       main();
       break;
      }
      default:
       {
       printf("\n\n Please enter the right choice.");
       break;
       }
     }
    }while (choi != 4);
    return 0;
   }
   case 2:
   {
    do
    {
        printf("\n\n\n [1] Queue items\n");
        printf(" [2] Deque items\n");
        printf(" [3] Display items\n");
        printf(" [4] Back to main\n");
        printf("\n Enter your choice: ");
        scanf("%d", &choice);
     switch (choice)
        {
      case 1:
      {
       clrscr();
       printf("\n\n Enter integer for total numbers to be sorted: ");
        scanf("%d", &n);
        printf("\n\n");
       for (i = 0; i < n; i++)
       {
        printf(" Enter integer for no. %d : ", i);
        scanf("%d", &a[i]);
       }
          break;
      }
       case 2:
       {
       clrscr();
       if (n == 0)
        {
        printf("\n The stack is empty.");
        }
       else
        {
        printf("\n The poped element is %d.", a[n]);
         for(i = 0; i < n; i++)
        {
         a[i] = a[i+1];
        }
        n--;
       }
       break;
      }
       case 3:
       {
       clrscr();
       if (n == 0)
        {
        printf("\n The stack is empty.");
       }
       else
       {
         printf("\n The stack contains: ");
         for (i = 0; i < n; i++)
        {
         printf("%3d", a[i]);
        }
       }
        break;
      }
      case 4:
      {
       main();
       break;
      }
      default:
       {
       printf("\n\n Please enter the right choice.");
       break;
       }
     }
    }while (choice != 4);
    return 0;
   }
   default:
   {
    clrscr();
    printf("\n\n Please enter the right choice.");
    break;
   }
  }
 }while (cho != 3);
}
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.