\ linkedlistexample.pas - /g/pasta 2.4
From Bistre Iguana, 5 Years ago, written in Pascal.
Embed
  1. program LinkedListExample;
  2.  
  3. type
  4.   PNode = ^TNode;
  5.  
  6.   TNode = record
  7.     Data: Integer;
  8.     Next: PNode;
  9.   end;
  10.  
  11.   PLinkedList = ^TLinkedList;
  12.  
  13.   TLinkedList = record
  14.     Head: PNode;
  15.   end;
  16.  
  17. function NewList: PLinkedList;
  18. begin
  19.   New(NewList);
  20.   NewList^.Head := Nil
  21. end;
  22.  
  23. procedure DisposeList(List: PLinkedList);
  24. var
  25.   RemovedNode, CurrentNode: PNode;
  26. begin
  27.   CurrentNode := List^.Head;
  28.  
  29.   while CurrentNode <> Nil do
  30.   begin
  31.     RemovedNode := CurrentNode;
  32.     CurrentNode := CurrentNode^.Next;
  33.     Dispose(RemovedNode)
  34.   end
  35. end;
  36.  
  37. procedure AddToFront(List: PLinkedList; Data: Integer);
  38. var
  39.   NewNode: PNode;
  40. begin
  41.   New(NewNode);
  42.   NewNode^.Data := Data;
  43.   NewNode^.Next := List^.Head;
  44.   List^.Head := NewNode
  45. end;
  46.  
  47. procedure AddToBack(List: PLinkedList; Data: Integer);
  48. var
  49.   NewNode, CurrentNode: PNode;
  50. begin
  51.   New(NewNode);
  52.   NewNode^.Data := Data;
  53.   NewNode^.Next := Nil;
  54.  
  55.   if List^.Head = Nil then
  56.   begin
  57.     List^.Head := NewNode
  58.   end else
  59.   begin
  60.     CurrentNode := List^.Head;
  61.  
  62.     while CurrentNode^.Next <> Nil do
  63.     begin
  64.       CurrentNode := CurrentNode^.Next
  65.     end;
  66.  
  67.     CurrentNode^.Next := NewNode
  68.   end
  69. end;
  70.  
  71. procedure RemoveFromFront(List: PLinkedList);
  72. var
  73.   RemovedNode: PNode;
  74. begin
  75.   RemovedNode := List^.Head;
  76.   List^.Head := List^.Head^.Next;
  77.   Dispose(RemovedNode)
  78. end;
  79.  
  80. procedure RemoveFromBack(List: PLinkedList);
  81. var
  82.   CurrentNode: PNode;
  83. begin
  84.   if List^.Head <> Nil then
  85.   begin
  86.     if List^.Head^.Next = Nil then
  87.     begin
  88.       Dispose(List^.Head);
  89.       List^.Head := Nil
  90.     end else
  91.     begin
  92.       CurrentNode := List^.Head;
  93.  
  94.       while CurrentNode^.Next^.Next <> Nil do
  95.       begin
  96.         CurrentNode := CurrentNode^.Next
  97.       end;
  98.  
  99.       Dispose(CurrentNode^.Next);
  100.       CurrentNode^.Next := Nil
  101.     end
  102.   end
  103. end;
  104.  
  105. procedure PrintList(List: PLinkedList);
  106. var
  107.   CurrentNode: PNode;
  108. begin
  109.   CurrentNode := List^.Head;
  110.  
  111.   while CurrentNode <> Nil do
  112.   begin
  113.     Write(CurrentNode^.Data);
  114.  
  115.     if CurrentNode^.Next <> Nil then
  116.     begin
  117.       Write(' => ')
  118.     end;
  119.  
  120.     CurrentNode := CurrentNode^.Next
  121.   end;
  122.  
  123.   Writeln
  124. end;
  125.  
  126. var
  127.   List: PLinkedList;
  128. begin
  129.   List := NewList;
  130.  
  131.   AddToFront(List, 3);
  132.   AddToFront(List, 2);
  133.   AddToFront(List, 4);
  134.  
  135.   PrintList(List);
  136.  
  137.   AddToBack(List, 1);
  138.   AddToBack(List, 0);
  139.  
  140.   PrintList(List);
  141.  
  142.   RemoveFromFront(List);
  143.   RemoveFromBack(List);
  144.  
  145.   PrintList(List);
  146.  
  147.   DisposeList(List)
  148. end.
captcha