Last active
April 21, 2016 13:13
-
-
Save murapadev/382fda08eea69bbd444466a63d52006d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Unit RequestQueue; | |
interface | |
Const | |
NULLQ = NIL; | |
type | |
tCode = string; | |
tPosQ = ^tNodoQ; | |
tItemQ = record | |
code: tCode; | |
param1: string; | |
param2: string; | |
param3: string; | |
end; | |
tNodoQ = record | |
item: tItemQ; | |
next: tPosQ; | |
end; | |
tQueue = record | |
first: tPosQ; | |
last: tPosQ; | |
end; | |
procedure createEmptyQueue(var queue:tQueue); | |
function isEmptyQueue(Queue:tQueue):boolean; | |
function front(queue:tQueue):tItemQ; | |
function enqueue(item:tItemQ;var queue:tqueue):boolean; | |
procedure dequeue(var queue:tQueue); | |
implementation | |
(**********************************************************************) | |
Function isEmptyQueue (queue:tQueue):boolean; | |
(*Determina si la cola está vacía.*) | |
begin | |
isEmptyQueue := (queue.first = NULLQ); | |
end; | |
(**********************************************************************) | |
(**********************************************************************) | |
procedure createEmptyQueue (var queue:tqueue); | |
(*Crea una cola vacía. | |
PostCD: La cola queda inicializada y vacía.*) | |
begin | |
queue.first:=NULLQ; | |
queue.last:=NULLQ; | |
end; | |
(**********************************************************************) | |
(**********************************************************************) | |
Function front(queue:tQueue):tItemq; | |
(*Devuelve el contenido (tItemQ) | |
* del frente de la cola (i.e. el elemento más antiguo). | |
PreCD: la cola no está vacía.*) | |
Begin | |
front := queue.first^.item; | |
End; | |
(**********************************************************************) | |
(**********************************************************************) | |
procedure dequeue(var queue: tQueue); | |
(*Elimina el elemento que está en el frente de la cola. | |
PreCD: la cola no está vacía.*) | |
var | |
pos :tPosQ; | |
begin | |
pos:=queue.first; | |
queue.first := queue.first^.next; | |
dispose(pos); | |
pos := NULLQ; | |
end; | |
(**********************************************************************) | |
(************************************************************************) | |
function enqueue(item:tItemQ;var queue:tQueue):boolean; | |
(*Inserta un nuevo elemento (tItemQ) en la cola. | |
* Devuelve false si no hay memoria | |
* suficiente para realizar la operación.*) | |
var | |
p:tPosq; | |
Begin | |
new(P); | |
p^.item:=item; //Creamos el elemento a agregar. | |
p^.next:=NULLQ; | |
if p <> NULLQ //Si hay memoria se agrega a la cola. | |
then begin | |
enqueue:= true; | |
if isEmptyqueue(queue) //Si la cola está vacia... | |
then | |
queue.first := p //La cola está vacia. | |
else | |
queue.last^.next := p; | |
queue.last := p; //La cola tiene elementos. | |
end | |
else enqueue := false; | |
end; | |
(**********************************************************************) | |
End. | |
Begin | |
end. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program TestQueue; | |
uses sysutils,RequestQueue; | |
// -------------------------------------------------------------------------------------- | |
// Read a file text with the different task to do by the program --- | |
// It prints a message indicating the task to perform with the required parameters --- | |
// The allowed tasks are: [C]reate, [Q]uit, [T]ransform, [F]avorites | |
// [A]buse, [D]iscard, [L]ike, [R]emove, [S]tatus, [U]pload | |
// -------------------------------------------------------------------------------------- | |
procedure readTasks(taskFile:string); | |
var | |
fileId:Text; | |
line:string; | |
UL : tItemQ; | |
QRequest: tQueue; | |
i:integer; | |
begin | |
{$i-} | |
assign(fileId, taskFile); | |
reset(fileId); | |
{$i+} | |
if (IOResult<>0) then begin | |
writeln('**** Reading. Error when trying to open ', taskFile); | |
halt(1); | |
end; | |
createEmptyQueue(QRequest); | |
writeln('**** Starting bumping of file'); | |
writeln; | |
i:=0; | |
While (not EOF(fileId)) do | |
begin | |
readln(fileId, line); | |
UL.code := trim(copy(line, 1, 2)); | |
UL.param1 := trim(copy(line, 3, 10)); | |
UL.param2 := trim(copy(line, 14, 15)); | |
UL.param3 := trim(copy(line, 30, 20)); | |
enqueue(UL,QRequest); | |
i:=i+1; | |
writeln('*Lines into queue ',i); | |
end; | |
writeln; | |
writeln('****Starting showing Request Queue'); | |
writeln; | |
while not(IsEmptyQueue(QRequest)) do begin | |
writeln('*',front(QRequest).code,' ',front(QRequest).param1,' ',front(QRequest).param2,' ',front(QRequest).param3,' '); | |
dequeue(QRequest); | |
end; | |
if IsEmptyQueue(QRequest) | |
then writeln('**** The queue is empty'); | |
end; | |
// --------------------------------------------------------------------------------- | |
BEGIN | |
if (paramCount>0) then | |
//Use the file specified as parameter | |
readTasks(ParamStr(1)) | |
else | |
//Use a "hard-coded" filename | |
readTasks('create-users.txt'); | |
writeln; | |
writeln('Press any key to exit....'); | |
readln; | |
END. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment