我声明的链表如下:
type
   PImageInfor=^TImageInfor;
   TImageInfor=record
      filename:string;
      fileid:integer;
      next:PImageInfor;
      Prior:PImageInfor;
   end;然后定义了几个全局变量:
head,last,p:PImageInfor;在Form.Create事件中初始化全局变量:
         new(head);
         head.next:=nil;
         head.prior:=nil;
         last:=head;在button1中建立链表,在button2中遍历链表
button1的事件如下:
     for i:=0 to 10 do
     begin
            new(p);
            p.fileid:=i;
            p.next:=nil;
            p.Prior:=last;
            last.next:=p;
            last:=last.next;
            dispose(p);
    end;在button2中遍历:
     p:=head.next;
     while(p<>nil)do
     begin
        listbox1.items.add(inttostr(p.fileid));
        p:=p.next;
     end;按道理,在listbox中显示的应该是0~10这几个数。但是我得到的结果是6~10这几个数。
而且如果我在button2的遍历事件中,在p:=head.next后加一句:showmessage(inttostr(p.fileid)),在listbox中得到的数就只有一个0了。我已经要晕死了,乱七八糟的链表    

解决方案 »

  1.   

    for i:=0 to 10 do
         begin
                new(p);
                p.fileid:=i;
                p.next:=nil;
                p.Prior:=last;
                last.next:=p;
                last:=last.next;
                dispose(p);
        end;好像有问题,不应该dispose(p)的,你试试。
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls, Buttons;type
      PImageInfor=^TImageInfor;
       TImageInfor=record
          filename:string;
          fileid:integer;
          next:PImageInfor;
          Prior:PImageInfor;
       end;
      TForm1 = class(TForm)
        Button1: TButton;
        Shape1: TShape;
        BitBtn1: TBitBtn;
        ListBox1: TListBox;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure BitBtn1Click(Sender: TObject);
      private
        { Private declarations }
        head,last:PImageInfor;  public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var i: integer;
        p: PImageInfor;
    begin
    for i:=0 to 10 do
         begin
                new(p);
                p.fileid:=i;
                p.next:=nil;
                p.Prior:=last;
                last.next:=p;
                last:=last.next;
        end;end;procedure TForm1.FormCreate(Sender: TObject);
    begin
             new(head);
             head.next:=nil;
             head.prior:=nil;
             last:=head;end;procedure TForm1.BitBtn1Click(Sender: TObject);
    var p: PImageInfor;
    begin
         p:=head.next;
         while(p <> nil)do
         begin
            listbox1.items.add(inttostr(p.fileid));
            p:=p.next;
         end;end;end.========================================================================
    如果可以请支持一下
    http://expert.csdn.net/Expert/topic/2308/2308724.xml?temp=.2405664