Code đây,rất mong nhận được sự góp ý
Code:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
//KHOI TAO DSLK QUEUE
struct tagnode{
char Data;
struct tagnode *next;
};
typedef tagnode *node;
typedef struct taglist{
node head;
node tail;
}linkedlist;
typedef linkedlist Queue;
void initQueue(Queue &q){
q.head=q.tail=NULL;
}
int isempty(Queue q) //kiem tra q rong hay khong
{
return(q.head==NULL);
}
void enQueue(Queue &q,char x) //them x vao cuoi q
{ node p= new tagnode;
if(p==NULL) return;
p->Data=x;
p->next=NULL;
if(isempty(q)){
q.head=q.tail=p;
}
else
{ q.tail->next=p;
q.tail=p;
}
}
void deQueue(Queue &q) //xoa phan tu dau cua q
{ node p=q.head;
if(isempty(q))
return;
q.head=p->next;
if(q.head==NULL)
q.tail=NULL;
p->next=NULL;
delete p;
}
void insertQueue(Queue &q,int n) //nhap phan tu cua q(nhap nguoi mua ve)
{ char j=65;
initQueue(q);
for(int i=0;i<n;i++)
enQueue(q,j++);
}
void drawQueue(int x,int y) //ve phong cho mua ve
{ int i,j;
for(i=0;i<4;i++)
if(i==0 || i==3)
for(j=0;j<16;j++)
{ gotoxy(x+j,y+i);
putchar(196);
}
else
for(j=0;j<17;j+=3)
{ gotoxy(x+j,y+i);
putchar(221);
}
}
void putoutQueue(Queue q,int x,int y) //in hang doi mua ve xem phim ra man hinh
{ node p=q.head;
int i=1;
clrscr();
gotoxy(20,1);
printf("MO PHONG HANG DOI MUA VE XEM PHIM");
drawQueue(x,y);
if(isempty(q))
return;
else
while(p)
{ gotoxy(x+i,y+2);
putchar(p->Data);
p=p->next;
i+=3;
}
}
void out_Queue(Queue q,int n) //quan ly so nguoi cho mua ve
{ int x=29,y=4;
insertQueue(q,n);
putoutQueue(q,x,y);
gotoxy(25,10);
printf("Co %d nguoi cho mua ve",n);
delay(2000);
for(int i=0;i<n;i++)
{ deQueue(q);
putoutQueue(q,x,y);
if(i!=(n-1))
{ gotoxy(25,10);
printf("Con %d nguoi cho mua ve",((n-1)-i));
}
else
{ gotoxy(33,10);
printf("SOLD OUT!");
}
delay(1000);
}
}
////////////////////////////////////////////
//HAM MAIN
void main()
{ Queue q;int n;
clrscr();
printf("nhap so nguoi muon mua ve");
scanf("%d",&n);
out_Queue(q,n);
getch();
}