链表节点之间的访问的问题
一个数据表格的一些记录被读到链表中,链表的节点定义如下:
//定义链表节点
type                        
  PTFInfo=^TFInfo;
  TFInfo=record
    TFNo:string;
    TFName:string;
    TFDate:string;
    TFTime:integer;
    TFFQInfo:integer;
    TFValue:string;
    Jin:real;
    Wei:real;
    TFRadii:integer;
    TFDirect:string;
    Pnext:PTFInfo;
  end;
那么如何在某个节点中访问其上一个节点的域的值呢?我还没未用过链表,也没有什么概念,现在要改别人写的代码。请大家详细指教。

解决方案 »

  1.   

    定义如下:                      
      PTFInfo=^TFInfo;
      TFInfo=record
        TFNo:string;
        TFName:string;
        TFDate:string;
        TFTime:integer;
        TFFQInfo:integer;
        TFValue:string;
        Jin:real;
        Wei:real;
        TFRadii:integer;
        TFDirect:string;
        Pnext:PTFInfo;   //保存下一个节点
        PPrior: PTFInfo; //保存上一个节点
      end;
      

  2.   

    假设如下:有链表 PHead:  PTFInfo;有如下的函数查询可以访问指定结点的上一个结点.
    //Head为链表头结点,CurNode为当前某一个结点;返回值为CurNode的上一个结点.
    function GetPriorNode(Head, CurNode: PTFInfo): PTFInfo;
    var P,P1, PriorNode: PTFInfo;
    begin
      Result := nil;
      P := Head;
      PriorNode := nil;
      while Assigned(P) do
      begin
        if (P = CurNode) then
        begin
          Result := PriorNode;
          break;
        end;
        PriorNode := P;
        P := P.Pnext;
      end;
    end;
      

  3.   

    同意 BlueDreaming(蓝色海岸,只是最后少了result
    function GetPriorNode(Head, CurNode: PTFInfo): PTFInfo;
    var P,P1, PriorNode: PTFInfo;
    begin
      Result := nil;
      P := Head;
      PriorNode := nil;
      while Assigned(P) do
      begin
        if (P = CurNode) then
        begin
          Result := PriorNode;
          break;
        end;
        PriorNode := P;
        P := P.Pnext;
      end;
    if P<>nil then result := PriorNode ;//查找成功
    else result := nil;//查找失败
    end;