Ch4b Queue

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

A

B
C
D
F
G
E
H
K
CU TRC D LIU V
GII THUT
ng dng ca DSLK: Queue
2
M t queue
Mt queue l mt cu trc d liu m vic thm vo c thc
hin mt u (rear) v vic ly ra c thc hin u cn li
(front)
Phn t vo trc s ra trc FIFO (First In First Out)
3
V d v Queue
A B C D B C D B C D E
Sau khi thm A,B, C,D
Ly ra 1 phn t:
Thm vo 1 phn t E
Ban u hng i rng
4
Mt s php ton trn hng i
void CreateEmpty(QueueType &Q): to hng i rng
int EmptyQueue(QueueType Q): Kim tra hng i c
rng hay khng?
int FullQueue(QueueType Q): Kim tra hng i Q y
hay khng? (trng hp hng i c to bng mng)
void EnQueue(QueueType &Q,DataType x): a phn
t x vo hng i Q.
int DeQueue(QueueType &Q,DataType &x): Ly phn
t u ca hng i gn cho x nu hng i khng
rng.
int FrontQueue(QueueType S,DataType &x): Ly gi tr
phn t u ca hng i gn cho x xem nu hng
i khng rng.
5
Ci t hng i bng DSLK
typedef . DataType;
struct Node
{
DataType Data;
Node *Next;
};
typedef Node *NodePtr;
struct QueueType
{
NodePtr Head,Tail;
};
6
Ci t hng i bng DSLK
void CreateEmpty(QueueType &Q)
{
Q.Head=Q.Tail=NULL;
}
int EmptyQueue(QueueType S)
{
return (Q.Head==NULL);
}
7
Ci t hng i bng DSLK
Thm mt phn t vo Queue
Tail
front middle last
Head
new_last
new_tail
8
Ci t hng i bng DSLK
void EnQueue(QueueType &Q,DataType x)
{NodePtr temp;
temp=new Node;
if(temp==NULL)
{ cout<<Khng cp pht c vng nh; exit(1);}
else
{
gn(temp->Data,x);//Gn x cho vng Data ca Temp
temp->Next=NULL;
if(EmptyQueue(Q))
{Q.Head=Q.Tail=temp;}
else
{
Q.Tail->Next=temp;
Q.Tail=temp;
}
}
}
9
Ci t hng i bng DSLK
int DeQueue(QueueType &Q,DataType &x)
{NodePtr temp;
if(EmptyQueue(Q))
{ cout<<Hng i rng; return 0;}
else
{
temp=Q.Head;
Q.Head=Q.Head->Next;
gn(x,temp->Data);//Gn vng Data ca temp cho
delete(temp);
return 1;
}
}
A B C
D
Head Tail
10
Ci t ngn xp bng DSLK
int FrontQueue(QueueType &Q,DataType &x)
{
if(EmptyQueue(Q)) return 0;
else
{
Gn(x,Q.Head->Data);
return 1;
}
}
11
ng dng ca Hng i
C ch vng m cho cc thao tc xut nhp
my tnh
Lu cc tin trnh ch x l H iu hnh,
trnh bin dch.
C ch bn v, bn phi trng,

You might also like