Advertisement
THOMAS_SHELBY_18

ListUnit5_1

Mar 15th, 2024
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.66 KB | Source Code | 0 0
  1. unit ListUnit;
  2.  
  3. interface
  4. type
  5.     TElemPointer = ^TElem;
  6.     TElem = record
  7.         Value: Integer;
  8.         Next: TElemPointer;
  9.     end;
  10.  
  11. procedure InsertElement(Head: TElemPointer; NewValue: Integer);
  12. function MergeLists(FirstHeader, SecondHeader: TElemPointer): TElemPointer;
  13. function InitializeList(): TElemPointer;
  14. procedure DisposeList (Header: TElemPointer);
  15. procedure OutputListToTextFile (Header: TElemPointer; var FileOut: TextFile);
  16.  
  17. implementation
  18.  
  19. procedure InsertElement(Head: TElemPointer; NewValue: Integer);
  20. var
  21.     Curr, Temp: TElemPointer;
  22. begin
  23.     Curr := Head;
  24.     while (Curr^.Next <> nil) and (Curr^.Next^.Value <= NewValue) do
  25.         Curr := Curr^.Next;
  26.  
  27.     New(Temp);
  28.     Temp^.Next := Curr^.Next;
  29.     Curr^.Next := Temp;
  30.     Temp^.Value := NewValue;
  31. end;
  32.  
  33. procedure InsertElementForMerge(var Curr: TElemPointer; NewValue: Integer);
  34. var
  35.     Temp: TElemPointer;
  36. begin
  37.     while (Curr^.Next <> nil) and (Curr^.Next^.Value <= NewValue) do
  38.         Curr := Curr^.Next;
  39.  
  40.     if Curr^.Value <> NewValue then
  41.     begin
  42.         New(Temp);
  43.         Temp^.Next := Curr^.Next;
  44.         Curr^.Next := Temp;
  45.  
  46.         Temp^.Value := NewValue;
  47.     end;
  48. end;
  49.  
  50. function InitializeList(): TElemPointer;
  51. var
  52.     Header: TElemPointer;
  53. begin
  54.     New(Header);
  55.     Header^.Next := nil;
  56.     Header^.Value := High(Integer);
  57.  
  58.     InitializeList := Header;
  59. end;
  60.  
  61. function MergeLists(FirstHeader, SecondHeader: TElemPointer): TElemPointer;
  62. var
  63.     Temp, Curr, ResultHeader: TElemPointer;
  64. begin
  65.     ResultHeader := InitializeList();
  66.  
  67.     Temp := FirstHeader;
  68.     Curr := ResultHeader;
  69.     while Temp^.Next <> nil do
  70.     begin
  71.         Temp := Temp^.Next;
  72.         InsertElementForMerge(Curr, Temp^.Value);
  73.     end;
  74.  
  75.     Temp := SecondHeader;
  76.     Curr := ResultHeader;
  77.     while Temp^.Next <> nil do
  78.     begin
  79.         Temp := Temp^.Next;
  80.         InsertElementForMerge(Curr, Temp^.Value);
  81.     end;
  82.  
  83.     MergeLists := ResultHeader;
  84. end;
  85.  
  86. procedure DisposeList (Header: TElemPointer);
  87. var
  88.     Curr, Temp: TElemPointer;
  89. begin
  90.     if Header^.Next <> nil then
  91.     begin
  92.         Curr := Header^.Next;
  93.  
  94.         while Curr^.Next <> nil do
  95.         begin
  96.             Temp := Curr^.Next;
  97.             Dispose(Curr);
  98.             Curr := Temp;
  99.         end;
  100.         Dispose(Curr);
  101.         Header^.Next := nil;
  102.     end;
  103. end;
  104.  
  105. procedure OutputListToTextFile (Header: TElemPointer; var FileOut: TextFile);
  106. var
  107.     Curr: TElemPointer;
  108. begin
  109.     Curr := Header;
  110.  
  111.     Rewrite(FileOut);
  112.     while Curr^.Next <> nil do
  113.     begin
  114.         Curr := Curr^.Next;
  115.         Writeln(FileOut, Curr^.Value);
  116.     end;
  117.     CloseFile(FileOut);
  118. end;
  119. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement