Autor Beitrag
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Do 25.05.06 15:34 
Hallo,

kann mir mal jemand an einem anschaulichen Beispiel den Einsatz verketteter Listen erklären? Die Suche liefert drei Ergebnisse. Wikipedia sagt es so unverständlich.

Dankeschön


Moderiert von user profile iconChristian S.: Topic aus Delphi Language (Object-Pascal) / CLX verschoben am Do 25.05.2006 um 15:51

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Do 25.05.06 15:55 
sers
Du meinst Verkette mit Listen mit Zeigern?
Ein Beispiel dafür, wo man diese einsetzt, wär mein kleiner Rechner, unten im Anhang. Die verketteten Listen gebrauche ich, um Rechenoperationen rückgängig zu machen oder um einfach einige Schritte zurückzugehen. (Auch Undo/Redo genannt) ;)

Mfg
Einloggen, um Attachments anzusehen!
Marco D. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Do 25.05.06 16:22 
Würdest du den Quellcode auch hochladen? Oder ansonsten kurz das Prinzip mit Quelltextbeispiel(en) erklären?

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Do 25.05.06 16:51 
Also, das ganze läuft mit einer sogenannten Vorwärtsdeklaration
Das die Definition von TEelement erst nach der Zuweißung erfolgt, ist absicht!
Ich bin nun so vorgegangen, dass ich einen Record erstellt habe, in dem sich ein Zeiger auf den nächsten Record befindet.
Also erstes habe ich einen Zeiger auf den Typ TElement deklariert.
Dann erst wird TElement definiert: Dieser enthält, neben einigen anderen Variablen, einen Zeiger. Dieser Zeiger ist wiederum vom zuerst definierten Typ PElement, er erlaubt es also, auf einen solchen Record zu zeigen, wie der, in dem er sich selbst befindet!
Sowas nennt man eine Vorwärtsdeklaration.
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
type
  PElement = ^TElement;
  TElement = Record
   Text: String;
   Next: PElement;
  end;

...

var
  Rechner: TRechner;
  value1,value2: real;  // zahl 1 und zahl 2
  rewrite:boolean;  // 2te zahl eingeben und somit erste überschreiben True/False
  operator: char;  // verwendeter Operator (+, -, *, /)
  vorzeichen: char; // plus / minus
   Anker: PElement; // Anker
   Aktuell: PElement; // Aktuell
implementation
   uses About;
{$R *.dfm}

...

{******* Werte auf NIL setzen *******}
procedure TRechner.FormActivate(Sender: TObject);
begin
 anker := NIL// NIL = Not in list 
 aktuell := NIL;
end;

{******* Speicher freigeben *******} // Wichtig!
procedure TRechner.FormDeactivate(Sender: TObject);
var p, p_n: PElement;
begin
 p := anker;          
 while p <> NIL do
    begin
       p_n := p^.next; // zum nächsten Record
       Dispose(p); // p freigeben
       p := p_n; // wenn kein Record mehr vorhanden ist, ist P_N = Nil
    end;
  end;

{******* Neuer  Wert *******}
procedure new_value;
var p1,p2: PElement;
begin
 New(p1); 
 p1^.text := unit_rechner.Rechner.Edit.text; 
 p1^.next := NIL// nächster record = nil

 if Anker = NIL then // falls noch kein record angelegt wurde
  anker := p1 
 else               // record in die liste einklinken
 begin
  p2 := anker;
  while(p2^.next <> NILdo  // nach vorne hangeln, bis zum letzten record
   p2 := p2^.next;
   p2^.next := p1;
  end;
  aktuell := p1;
end;

{******* Forward *******}
procedure TRechner.Button_forwardClick(Sender: TObject);
begin
 try
  if aktuell^.next <> NIL then
   aktuell := aktuell^.next; 
   Edit.Text := aktuell^.text 
  except
   on EAccessViolation do
    begin
     statusbar.Panels.Items[0].Text := 'Es sind noch keine Elemente definiert!';
     exit;
    end;
  end;
end;

{******* Backward *******}
procedure TRechner.Button_backwardClick(Sender: TObject);
var p: PElement;
begin
 try
  p := anker;
 while (p <> NILand (p^.next <> aktuell) do
  p := p^.next;

  if p <> NIL then
   aktuell := p;
  Edit.Text := aktuell^.text;
 except
   on EAccessViolation do
    begin
     statusbar.Panels.Items[0].Text := 'Es sind noch keine Elemente definiert!';
     exit;
    end;
  end;
end;

{*******  Taste C: Clear Edit *******}
procedure TRechner.Button_deleteClick(Sender: TObject);
var p: PElement;
begin
 edit.Clear;
 value1 := 0;
 value2 := 0;
 rewrite := false;
 statusbar.Panels.Items[0].Text := 'Wert gelöscht.';

  try                              // wert löschen
  if aktuell = anker then  // falls aktuell = erster record (in der liste)
   begin
      anker := aktuell^.next; // anker eins weiter setzen
      Dispose(aktuell);       // akutellen (ersten) record löschen
      aktuell := anker;      
   end
   else
   begin
      p := anker;                   
      while (p <> NILand (p^.next <> aktuell) do
       p := p^.next;
      p^.next := aktuell^.next;
      Dispose(aktuell);
      aktuell := p;
   end
    except
   on EAccessViolation do
    begin
     statusbar.Panels.Items[0].Text := 'Es sind noch keine Elemente definiert!';
     exit;

   end;
  end;
 end;


{*******  Taste RESET *******}
procedure TRechner.Button_resetClick(Sender: TObject);
var p, p_n: PElement;
begin
 p := anker;                     // alle werte löchen 
 while p <> NIL do
    begin
       p_n := p^.next;
       Dispose(p);
       p := p_n;
    end;
 anker := NIL;
 aktuell := NIL;

    statusbar.Panels.Items[0].Text := 'Alle Werte gelöscht.';
    edit.Text := '';
  end;



Mfg
Marco D. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Fr 26.05.06 13:53 
Das erschlägt mich jetzt irgendwie.
Gibt es ein Tutorial zu verketteten Listen?

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Fr 26.05.06 14:01 
Mit Sicherheit, aber ich könnte auch versuchen eins zu schreiben, solltest du keins finden ;)
Marco D. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Fr 26.05.06 14:12 
Wo gibt es denn eins? Im DF hab ich schon gesucht.

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot