you see, i have this program implementing stack and queue. my problem is that when my program executes dequeue(i used leave), the dequeued number should immediately transfer and be displayed in my stack(airplaine). my program does the dequeue and push just fine but the display is late.

for example: if i dequeue 2 & 3, then the moment that 2 is dequeued, it should be displayed in the airplaine list(stack). But what happens is that, 2 is only displayed when 3 is already dequeued.

i also need the program to end ONLY when any key is pressed on the keyboard.


main program:
Code:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<time.h>
#include<windows.h>
#include "gotoxy.h"
#include "queue.h"
#include "stack.h"

int rand(void);

main(){
int num1,num2,num3,temp=0;
Queue Q;
Stack S;
do{

    num1=rand()%100+1;
    num2=rand()%100+1;
    num3=rand()%100+1;

    if((num1%2)==1){
         Q.Join(num2);
         clrscr();
         cout<<"Customer Representative:"<<endl;
         cout<<"------------------------"<<endl;
         cout<<"Now Serving: ";
         gotoxy(30,2);cout<<"Airplane: ";
         Q.Display();
         S.Display();                      
    }
    
    if((num3%2)==1){
         S.Push(Q.Leave());
         clrscr();
          cout<<"Customer Representative:"<<endl;
          cout<<"------------------------"<<endl;
          cout<<"Now Serving: ";
          gotoxy(30,2);cout<<"Airplane: ";
          Q.Display();
                S.Display();
    }

    delay(1000);
    
}while(true);

getch();
}
queue.h:
Code:
#include<iostream.h>

class Queue
{
      private:
         int Q[100];
         int front,rear;
      public:
         Queue(void){front=rear=-1;}
         void Join(int item);
         int Leave(void);
         int EmptyQ(void);
         void Display(void);
};

void Queue::Join(int item)
{
     if(rear==99)
         rear=0;
     else
         rear++;
     Q[rear]=item;
}

int Queue::Leave(void)
{
    int temp=Q[front];
    front ++;
    return temp;
}

int Queue::EmptyQ(void)
{
    if(rear<0 || rear==front)
        return 1;
    else
        return 0;
}

void Queue::Display(void)
{   int n=2;
    for(int z=front;z<rear+1;z++){
            gotoxy(14,n);cout<<Q[z];
            n++;
    }
}
stack.h:
Code:
#include <iostream.h>

class Stack
{
	private:
		int S[50];
		int top;
	public:
		Stack(void);
		void Push(int elem);
		int Pop(void);
		int Top(void);
		int Empty(void);
		void Display(void);
};

Stack::Stack(void)
{
	top=-1;
}

void Stack::Push(int elem)
{
	top++;
	S[top]=elem;
}

int Stack::Pop(void)
{
	int temp = S[top];
	top--;
	return temp;
}

int Stack::Top(void)
{
	return S[top];
}

int Stack::Empty(void)
{
    if(top<0)
        return 1;
    else
        return 0;
}

void Stack::Display(void)
{
    int m=2;
    for(int i=1;i<top;i++){
            gotoxy(40,m);cout<<S[i];
            m++;
    }
}
i use DevC++ which does not have its own gotoxy, clrscr, and delay function, so i made my own header file.

gotoxy.h
Code:
void gotoxy(int x, int y)
{
HANDLE hConsoleOutput;
COORD dwCursorPosition;
cout.flush();
dwCursorPosition.X = x;
dwCursorPosition.Y = y;
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);
}

void clrscr()
{
system("cls");
} 

void delay( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}