thu vien Queue
UNIT QUEUE;
INTERFACE TYPE
QueueElement= integer;
QueuePtr=^QueueNode;
QueueNode=record
Du_lieu:QueueElement;
Next:QueuePtr;
end;
QueueType=record
front,rear:QueuePtr;
end;
Procedure CreateQ(Var Queue:QueueType);
Function EmptyQ(Queue:QueueType):Boolean;
Procedure AddQ(Var Queue:QueueType;Item:QueueElement);
Procedure RemoveQ(Var Queue:QueueType;var Item:QueueElement);
IMPLEMENTATION
Procedure CreateQ(Var Queue:QueueType);
Begin
Queue.front:=Nil;Queue.rear:=NIL;end;
Function EmptyQ(Queue:QueueType):Boolean;
Begin
EmptyQ:=(Queue.front=Nil) and (Queue.rear=Nil) end;
Procedure AddQ(Var Queue:QueueType;Item:QueueElement);
Var Pt:QueuePtr;
Begin
New(Pt);
Pt^.Du_lieu:=Item;
With Queue do
If EmptyQ(Queue) then
Begin front:=Pt;rear:=Pt end
else
Begin Rear^.Next:=Pt;Rear:=Pt end;
end;
Procedure RemoveQ(var Queue:QueueType;Var Item:QueueElement);
Var Pt:QueuePtr;
Begin
If EmptyQ(Queue) Then Writeln('Hang Doi Rong')
else With Queue Do
Begin Item:=Front^.Du_lieu;
Pt:=Front;
If Front<>rear then Front:=Front^.Next
Else Begin Front:=Nil;Rear:=Nil end;
Dispose(Pt);
end;
End;
End.
Bạn đang đọc truyện trên: AzTruyen.Top