Torderedlist, Tstack, Tqueue
The CONTNRS unit also defines three other classes: Torderedlist, Tstack, Tqueue
Torderedlist
Torderedlist = Class (TObject) Private flist:tlist;protected procedure Pushitem (aitem:pointer); virtual; abstract; ... public function Count:integer; function AtLeast (Acount:integer): Boolean; Procedure Push (aitem:pointer); function Pop:pointer; function Peek:pointer;end;
Tstack
Tstack = Class (torderedlist) protected procedure Pushitem (aitem:pointer); override;end;
Tqueue
Tqueue = Class (torderedlist) protected procedure Pushitem (aitem:pointer); override;end;
Note that although torderedlist is not inherited from Tlist, it uses tlist to store pointers when implemented internally. Also note that the pushitem process of the Torderedlist class is a smelly abstract process, so we cannot instantiate the Torderlist class, but we should inherit the new class from torderlist and implement the abstract Pushitem method.
Therefore, the tstack and tqueue inherit from Torderedlist, so the tstack and tqueue pointers also exist in the tlist of their own.
Tstack and Tqueue are classes that implement Pushitem abstract methods, and we can instantiate tstack and Tqueue classes as last-in-first-out stacks (LIFO) and FIFO-first-out queues (FIFO). Here are the instructions for using the methods of these two classes:
- Count returns the number of items in the list.
- AtLeast can be used to check the size of the linked list, to determine whether the number of pointers in the current listing is greater than the passed parameter value, and if true, the number of items in the list is greater than the arguments that came in.
- Push for the Tstack class, the push method adds the pointer to the end of the list, and for the Tqueue class, the push method inserts the pointer at the beginning of the list.
- POP returns the end pointer to the list and removes it from the linked list.
- Peek returns the end pointer of the linked list, but does not remove it from the linked list.
Tobjectstack, Tobjectqueue
The last two classes in the Contnrs unit are the Tobjectstack and Tobjectqueue classes, which are defined as follows
Tobjectstack
Tobjectstack = Class (tstack) public procedure Push (aobject:tobject); function Pop:tobject; function Peek:tobject;end;
Tobjectqueue
Tobjectqueue = Class (Tqueue) public procedure Push (aobject:tobject); function Pop:tobject; function Peek:tobject;end;
These two classes are just simple extensions of the tstack and Tqueue classes, which hold TObject object references in the linked list, rather than simple pointers.
Delphi Container Class---torderedlist, tstack, Tqueue, Tobjectstack, tobjectqueue use