我写了个 链表实验,并想用 Label1.Caption 来显示链中的数据。但 TForm1 中有 Label1 时一点击 Button1 就出现以下错误讯息。

  • 主题发起人 Pc 狂迷
  • 开始时间
P

Pc 狂迷

Unregistered / Unconfirmed
GUEST, unregistred user!
我写了个 链表实验,并想用 Label1.Caption 来显示链中的数据。但 TForm1 中有 Label1 时一点击 Button1 就出现以下错误讯息。 (20分)<br />我写了个 链表实验,并想用 Label1.Caption 来显示链中的数据。但 TForm1 中有 Label1 时一点击 Button1 就出现以下错误讯息。

Access violation at address 00454206 in module 'Xxxx.exe'.Read of address FFFFFFFF

如果把 Label1 删除则没有报错。请前辈们指点迷镇。
代码:
unit Link;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  alink = ^node
//单链表
  node = RECORD
           data : integer;
           next : alink
         end;
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel
 // <--- 如果此 Label1删除则不会出错为什么
  procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
// *****************************************//
// *  单链表插入过程 2002.5.11 11:06       *//
// *****************************************//
  procedure inlink(var head:alink;a,b:integer);


var
  Form1: TForm1;
implementation
{$R *.dfm}
// *****************************************//
// *  单链表插入过程 2002.5.11 11:06       *//
// *****************************************//
procedure inlink(var head:alink;a,b:integer);
{ 在 head 为头指针的 单链表 中查找a ,并在 a 前插入 b。 }
var
  q,p,s : alink;

begin
  new(s);
  s^.data := b
   {新建结点 S ,并赋值 B}
  if head = nil then  { 假若头指针为 nil 则为空表}
    begin {空表}
      head := s;
      s^.next := nil;
    end
  else {非空表}
    if head^.data = a then
      begin      {第一个结点值就是 a}
        s^.next := head
{将 s 的指针指向 head}
        head := s        {将 s 的指针设为 head}
      end
    else {第一个结点不是 a}
      begin
        p := head;
        while(p^.data<>a) and (p^.next<>nil) do
          begin //遍历单链表,寻找数据域为a 的结点
            q := p;
            p := q^.next;
          end;
        //寻找完毕
        if p^.data = a then //寻找到 a
          begin
            q^.next := s;
            s^.next := p;
          end
        else               // a 不存在
          begin // s 为结尾
            p^.next := s;
            s^.next := nil

          end;
  end;
end;


//***********************************
//* TForm1.Button1Click(Sender: TObject);
//***********************************
procedure TForm1.Button1Click(Sender: TObject);
var
  tp_link : alink;
begin
  new(tp_link);
  inlink(tp_link,12,34);
end;

end.
 
应该是IntToStr(ra_p.data)中的参数ra_p.data出错,ra_p的声明为什么"//"?
 
‘//’
是屏蔽了的意思。
 
我知道!但ra_p是在哪里定义的?是全局变量吗?
 
请看修改后的贴子。
 
把LABEL1去掉了,这样就可以运行了吗?
你有没有用单步调试看过是在哪里出错吗?
 
你的 head 就是 new(tp_link)
得来的罗,
tp_link^.next不能保证是nil啊,他可能是个非法指针
tp_link^.data也是个不确定的
 
把:
var
tp_link : alink;

剪切到:
implementation
{$R *.dfm}
下一行,问题解决并符合我原意。
自己又死蠢了多一会。
 
问题不是出在链上,不过你在链插入的时候,做的有点麻烦,先吧头指针置成nil。
head:=nil;
new(s)

s.next:=head;
head:=s;就可以了。
 
顶部