Очередь



Очередь - структура данных, предназначенная для добавления элемента в конец последовательности и извлечения элемента из начала последовательности.


Листинг Delphi

const QueueLength = 1000;
type QueueData = integer;
     Queue = object
       public
         Q : array[1..QueueLength] of QueueData;
         head, tail : integer;
       public
         constructor Create;
         procedure Write(x : QueueData);
         function  Read : QueueData;
         function  Empty : boolean;
     end;

implementation

constructor Queue.Create;     // Создание очереди
  begin
    head := 1;
    tail := 1;
  end;

  procedure Queue.Write(x : QueueData);  // Добавление элемента в очередь
  begin
    Q[tail] := x;
    if tail = QueueLength
      then tail := 1
      else inc(tail);
  end;

  function Queue.Read : QueueData;  // Извлечение элемента
  begin
    Result := Q[head];
    if head = QueueLength
      then head := 1
      else inc(head);
  end;

  function Queue.Empty : boolean// Пуста ли очередь?
  begin
    Result := (head = tail);
  end;

08.12.2007, 16:23

По всем вопросам обращаться: rumterg@gmail.com