0

我无法让我的程序中的队列正常工作。我尝试的每一次调整都会导致“ERangeError:范围检查错误”。我知道这是因为程序试图访问不存在的东西,但我不知道如何修复它。这是我的队列实现...

node = record //Square on chessboard
  x: integer; //Row coordinate
  y: integer; //Col coordinate
  d: integer; //Distance from starting position
  next: ^node;// (for the queue)
 end; //node
coord = ^node;//'Coordinates'
queue = record//Queue w/ Linked List
  head: coord;
  tail: coord;
 end; //queue

procedure init(var q: queue);
  begin
    q.head := nil;
    q.tail := nil;
   end; //init
function isEmpty(const q: queue): boolean;
 begin
  isEmpty := (q.head = nil);
 end; //isEmpty
procedure push(var q: queue; m, n, o: integer);
 var C: coord;
 begin
  new(C);
    C^.x := m;
    C^.y := n;
    C^.d := o;

  if q.head = nil then begin
    q.head := C;
    q.tail := q.head;
    end
  else begin
    q.tail^.next := C;
    q.tail := C;
    end;
 end; //push
procedure pop(var q: queue; out m: int; out n: int; out o: int);
 var C: coord;
 begin
  m := q.head^.x;
  n := q.head^.y;
  o := q.head^.d;

  C := q.head;
  q.head := q.head^.next;
  dispose(C);
  if q.head = nil then q.tail := nil;
 end; //pop

...这是与它交互的功能的主要部分。

while not isEmpty(q) do begin
  pop(q, row, col, dst);
    new(crd2);
    crd2^.x := row;
    crd2^.y := col;
    crd2^.d := dst;

  if valid(B, crd2) and not visited[crd2^.x, crd2^.y] then
    visited[crd2^.x, crd2^.y] := true; //Mark square as visited
  if (crd2^.x = Gl^.x) and (crd2^.y = Gl^.y) then
    exit(crd2^.d); //Goal Check

  for i := 1 to 8 do begin
    crd2^.x := crd2^.x + mvmtX[i];
    crd2^.y := crd2^.y + mvmtY[i];

    if valid(B, crd2) and not visited[crd2^.x, crd2^.y] then begin
      crd2^.d := crd2^.d + 1;

      push(q, crd2^.x, crd2^.y, crd2^.d);

     end;//if valid(B, crd)...

  end;//for i
 end;//while not isEmpty(q)
 exit(crd^.d);
end;

我以为我已经通过在程序中添加足够的 new(node) 调用来解决这个问题,但错误仍然存​​在。我真的很茫然,如果你能提供任何澄清,我将不胜感激。
我会给我的教授发电子邮件,但他现在正在滑雪,很难联系到。谢谢你的任何建议:)

4

0 回答 0