demo_pascal_lists.htm

来自「Delphi脚本控件」· HTM 代码 · 共 103 行

HTM
103
字号
<html>
<head>
<link rel=stylesheet type="text/css" href="styles.css">
</head>

<body>

<h3>
LISPPA: Linked lists (paxPascal).
</h3>
<hr>

<blockquote>
<pre>
<font color="blue"><b>program</b></font> Lists;

<font color="blue"><b>var</b></font>
  L, P: Variant;
  I: Integer;
<font color="blue"><b>begin</b></font>
  writeln(<font color="Red">'Create a list'</font>);
  L := [100, [200, [300, <font color="blue"><b>nil</b></font>]]];
  writeln(L);

  writeln(<font color="Red">'Insert new item at the beginning'</font>);
  L := [50, L];
  writeln(L);

  writeln(<font color="Red">'Add new item at the end'</font>);
  P := @L; // Create alias of L
  <font color="blue"><b>while</b></font> P <> <font color="blue"><b>nil</b></font> <font color="blue"><b>do</b></font> P := @P[1]; // Find last item
  P := [400, <font color="blue"><b>nil</b></font>]; // Add new item
  writeln(L);

  writeln(<font color="Red">'Insert new item at the middle'</font>);
  P := @L[1];
  P := @P[1]; // insert before 200
  P := [150, P];
  writeln(L);

  writeln(<font color="Red">'Print list'</font>);
  P := @L; // create alias of L
  <font color="blue"><b>while</b></font> P <> <font color="blue"><b>nil</b></font> <font color="blue"><b>do</b></font>
  <font color="blue"><b>begin</b></font>
    writeln(P[0]);
    P := @P[1]; // go to the next item
  <font color="blue"><b>end</b></font>;

  writeln(<font color="Red">'Delete the first item of the list'</font>);
  <font color="blue"><b>reduced</b></font> L := L[1];
  writeln(L);

  writeln(<font color="Red">'Delete the last item of the list'</font>);
  P := @L; // Create alias of L
  <font color="blue"><b>while</b></font> P[1] <> <font color="blue"><b>nil</b></font> <font color="blue"><b>do</b></font> P := @P[1]; // Find last item
  <font color="blue"><b>reduced</b></font> P := P[1];
  writeln(L);

  writeln(<font color="Red">'Delete item at the middle of the list'</font>);
  P := @L;
  P := @P[1];
  <font color="blue"><b>reduced</b></font> P := P[1];
  writeln(L);

  writeln(<font color="Red">'Delete all items from the list'</font>);
  <font color="blue"><b>while</b></font> L <> <font color="blue"><b>nil</b></font> <font color="blue"><b>do</b></font> <font color="blue"><b>reduced</b></font> L := L[1];
  writeln(L);

  writeln(<font color="Red">'Create a cycled list'</font>);
  L := [100, [200, [300, <font color="blue"><b>nil</b></font>]]];
  L[1][1][1] := @L; // join head and tail of the list
  writeln(L);

  <font color="blue"><b>delete</b></font> L;

  writeln(<font color="Red">'Another way to create a cycled list'</font>);
  L := [100, [200, [300, <font color="blue"><b>nil</b></font>]]];
  P := @L; // Create alias of L
  <font color="blue"><b>while</b></font> P <> <font color="blue"><b>nil</b></font> <font color="blue"><b>do</b></font> P := @P[1]; // Find last item
  P^ := @L; // join head and tail of the list
  writeln(L);

  writeln(<font color="Red">'Print the cycled list'</font>);
  P := @L;
  I := 0;
  <font color="blue"><b>repeat</b></font>
    writeln(P[0]);
    P := @P[1];
    Inc(I);
  <font color="blue"><b>until</b></font> I = 15;
<font color="blue"><b>end</b></font>.
</pre>
</blockquote>

<p>
<HR>
<font size = 1 color ="gray">
Copyright &copy; 1999-2005
VIRT Laboratory. All rights reserved.
</font>
</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?