[学习官方例子]TStack

代码如下:

program TStackTest;

{$APPTYPE CONSOLE}

uses
  SysUtils, Generics.Collections;

var
  Stack: TStack<String>;
begin
  Stack := TStack<String>.Create();

  Stack.Push('John');
  Stack.Push('Mary');
  Stack.Push('Bob');
  Stack.Push('Anna');
  Stack.Push('Erica');

  writeln('Last pushed element is: "' + Stack.Peek() + '".');
  writeln('The stack contains '+IntToStr(Stack.Count)+' elements.');

  Stack.Extract();     //出去顶元素
  Stack.TrimExcess();   //设置容量为当前的元素个数

  writeln('The stack contains '+IntToStr(Stack.Count)+' elements.');
  writeln('Last pushed element is: "'+Stack.Pop()+'".');

  Stack.Clear;
  Stack.Free;

  readln;

end.

效果图:

mx37B89

posted @ 2011-10-14 00:20  wouldguan  阅读(251)  评论(0编辑  收藏  举报