Skip to content

Commit ca55205

Browse files
ADD: ufilo.pas
1 parent 1962762 commit ca55205

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Collection of classes and examples corresponding to data processing, math and co
6161
| udomxml.pas | generic xml parser |
6262
| ueventer.pas| class toc reate events for components that are not derived from LCL-Components |
6363
| ufifo.pas | generic first in first out class |
64+
| ufilo.pas | generic first in last out clas |
6465
| ugenmathcalc.pas | generic parser / solver for binary and unary operands (typically used to parse mathematical formulas) |
6566
| uimodbus.pas | Modbus server class for Modbus TCP or Modbus RTU |
6667
| uinterpreter.pas | Pascal Interpreter |

data_control/ufilo.pas

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
(******************************************************************************)
2+
(* ufilo.pas 12.06.2005 *)
3+
(* *)
4+
(* Version : 0.01 *)
5+
(* *)
6+
(* Author : Uwe Schächterle (Corpsman) *)
7+
(* *)
8+
(* Support : www.Corpsman.de *)
9+
(* *)
10+
(* Description : Implements a First in last out element *)
11+
(* *)
12+
(* License : See the file license.md, located under: *)
13+
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
14+
(* for details about the license. *)
15+
(* *)
16+
(* It is not allowed to change or remove this text from any *)
17+
(* source file of the project. *)
18+
(* *)
19+
(* Warranty : There is no warranty, neither in correctness of the *)
20+
(* implementation, nor anything other that could happen *)
21+
(* or go wrong, use at your own risk. *)
22+
(* *)
23+
(* Known Issues: none *)
24+
(* *)
25+
(* History : 0.01 - Initial version *)
26+
(* 0.02 - Umstellen auf Generics *)
27+
(* *)
28+
(******************************************************************************)
29+
Unit ufilo;
30+
31+
{$MODE objfpc}{$H+}
32+
33+
Interface
34+
35+
Uses sysutils; // Für die Exception
36+
37+
Type
38+
39+
{ TStack }
40+
41+
generic TFiLo < T > = Class
42+
private
43+
{ Private-Deklarationen }
44+
Type
45+
// Pointer des stacks
46+
PStackP = ^TStackT;
47+
48+
// Ein Stack Element
49+
TStackT = Record
50+
value: T;
51+
next: PStackP;
52+
End;
53+
// Pointer auf Oberstes Element
54+
var
55+
fTopItem: PStackP;
56+
fcount:integer;
57+
public
58+
{ Public-Deklarationen }
59+
Property Count:integer read fcount;
60+
// Initialisieren
61+
Constructor create;
62+
// Freigeben
63+
Destructor Destroy; override;
64+
// Leeren
65+
Procedure Clear;
66+
// Hinzufügen eines Wertes
67+
Procedure Push(Value: T);
68+
// Rückgabe des Obersten Elementes und Löschen
69+
Function Pop: T;
70+
// Rückgabe des Obersten Elements
71+
Function Top: T;
72+
// Gibt True zurück wenn Leer
73+
Function IsEmpty: Boolean;
74+
End;
75+
76+
// Die Exeption
77+
StackException = Class(Exception);
78+
79+
Implementation
80+
81+
constructor TFiLo.create;
82+
Begin
83+
fTopItem := Nil;
84+
fcount := 0;
85+
End;
86+
87+
destructor TFiLo.Destroy;
88+
Begin
89+
Clear;
90+
End;
91+
92+
function TFiLo.IsEmpty: Boolean;
93+
Begin
94+
result := Nil = fTopItem;
95+
End;
96+
97+
procedure TFiLo.Clear;
98+
Var
99+
v, v2: PStackP;
100+
Begin
101+
v := fTopitem;
102+
While v <> Nil Do Begin
103+
v2 := v;
104+
v := v^.next;
105+
dispose(v2);
106+
End;
107+
fTopitem := Nil;
108+
fcount := 0;
109+
End;
110+
111+
procedure TFiLo.Push(Value: T);
112+
Var
113+
v: PStackP;
114+
Begin
115+
inc(fcount);
116+
new(v);
117+
v^.value := Value;
118+
V^.next := fTopitem;
119+
fTopitem := v;
120+
End;
121+
122+
function TFiLo.Top: T;
123+
Begin
124+
// wird von einer Leeren Schlange Gepoppt dann Exception
125+
If fTopItem = Nil Then Begin
126+
Raise StackException.create('Error Stack Empty');
127+
End
128+
Else Begin
129+
// Rückgabe des Wertes
130+
result := fTopItem^.value;
131+
End;
132+
End;
133+
134+
function TFiLo.Pop: T;
135+
Var
136+
b: PStackP;
137+
Begin
138+
// wird von einer Leeren Schlange Gepoppt dann Exception
139+
If fTopItem = Nil Then Begin
140+
Raise StackException.create('Error Stack Empty');
141+
End
142+
Else Begin
143+
dec(fcount);
144+
// Rückgabe des Wertes
145+
result := fTopItem^.value;
146+
// Löschen des Knoten in dem Stack
147+
b := fTopItem;
148+
fTopItem := fTopItem^.Next;
149+
// Freigeben des Speichers
150+
Dispose(b);
151+
End;
152+
End;
153+
154+
End.
155+

0 commit comments

Comments
 (0)