Jump to content
thoga31

Problema e dúvida numa classe com lista ligada

Recommended Posts

thoga31

Na sequência deste post, decidi implementar uma classe que facilita o trabalho com listas ligadas.

Contudo, tenho dois problemas:

  1. O código compila mas com o seguinte warning: linked_list_integer_class.pas(16,20) Warning: An inherited method is hidden by "destructor TLinkedList.Destroy;". O que significa na prática?
  2. Quando executo, obtenho o seguinte output:
    Loading list with random values...
    Stack:
    Queue:
    Sorted: EACCESSVIOLATION -> Access violation
    


Portanto, se o código anterior funcionava, não estou a entender porque é que este não funciona. Não está a haver output de dados das listas e suponho que o erro de access violation esteja relacionado com o facto de head2^.next não existir na realidade uma vez que é nil, o valor de head^.next.

Vai uma ajudinha aqui ao velho thoga? :D

Cumprimentos.

{$mode objfpc}
program linked_list_integer_class;
uses sysutils;

type TLinkedList = class(TObject)
    private
       type TLnkLst = ^TLL;
            TLL = record
               data : integer;
               next : TLnkLst;
            end;
       var head : TLnkLst;

    public
       constructor Create;
       destructor Destroy;
       procedure AddValue(const v : integer);
       procedure ShowValuesAsQueue;
       procedure ShowValuesAsStack;
       procedure CopyList(newlist : TLinkedList);
       procedure SortList;
    end;

constructor TLinkedList.Create;
begin
   inherited Create;
   new(self.head);
end;

destructor TLinkedList.Destroy;
var cur : TLnkLst;
begin
   inherited Destroy;
   while self.head <> nil do begin
       cur := self.head;
       self.head := self.head^.next;
       dispose(cur);
   end;
end;

procedure TLinkedList.AddValue(const v : integer);
var cur : TLnkLst;
begin
   new(cur);
   cur^.data := v;
   cur^.next := nil;
   while self.head^.next <> nil do
       self.head := self.head^.next;
   self.head^.next := cur;
end;

procedure TLinkedList.ShowValuesAsQueue;
begin
   self.head := self.head^.next;
   while self.head <> nil do begin
       write(self.head^.data,', ');
       self.head := self.head^.next;
   end;
   writeln;
end;

procedure TLinkedList.ShowValuesAsStack;
var cur, last : TLnkLst;
begin
   self.head := self.head^.next;
   cur := self.head;
   while cur^.next <> nil do
       cur := cur^.next;
   last := cur;

   while last <> self.head do begin
       cur := self.head;
       while cur^.next <> last do
           cur := cur^.next;
       last := cur;
       cur := cur^.next;
       write(cur^.data, ', ');
   end;
   writeln;
end;

procedure TLinkedList.CopyList(newlist : TLinkedList);
begin
   self.head := self.head^.next;
   while self.head <> nil do begin
       newlist.AddValue(self.head^.data);
       self.head := self.head^.next;
   end;
end;

procedure TLinkedList.SortList;
var head2 : TLnkLst;
   val : integer;
begin
   self.head := self.head^.next;
   while self.head^.next <> nil do begin
       head2 := self.head^.next;
       while head2 <> nil do begin
           if self.head^.data > head2^.data then begin
               val := self.head^.data;
               self.head^.data := head2^.data;
               head2^.data := val;
           end;
           head2 := head2^.next;
       end;
       self.head := self.head^.next;
   end;
end;


var i : integer;
   list   : TLinkedList;
   sorted : TLinkedList;

begin
   try
       list := TLinkedList.Create;
       writeln('Loading list with random values...');
       randomize;
       for i := 1 to 10 do
           list.AddValue(random(20));

       write(' Stack: ');
       list.ShowValuesAsStack;
       write(' Queue: ');
       list.ShowValuesAsQueue;

       sorted := TLinkedList.Create;
       write('Sorted: ');
       list.CopyList(sorted);
       sorted.SortList;
       sorted.ShowValuesAsQueue;

       list.Destroy;
       sorted.Destroy;

   except
       on e:exception do begin
           writeln(upcase(e.classname), ' -> ', e.message);
       end;
   end;
   readln;
end.


Knowledge is free!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.