Advertisement
believe_me

Untitled

Mar 27th, 2022
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.36 KB | None | 0 0
  1. unit LinkedListUnit;
  2.  
  3. interface
  4.  
  5. uses
  6.     System.SysUtils, Classes;
  7.  
  8. Type
  9.     TNodePointer = ^TNode;
  10.     TNode = record
  11.         Value : Integer;
  12.         Next : TNodePointer;
  13.     end;
  14.  
  15.     TListOperations = class
  16.         class procedure create(var Header: TNodePointer); static;
  17.         class procedure push(Header : TNodePointer; Data: integer); static;
  18.         class function length(Header: TNodePointer): integer; static;
  19.         class procedure delete(var Header: TNodePointer; Index, NumberOfElements: Integer); static;
  20.         class function toString(Header: TNodePointer): String; static;
  21.         class procedure deleteList(var Header: TNodePointer); static;
  22.         class function isEmpty(var Header: TNodePointer): boolean; static;
  23.     end;
  24.  
  25. implementation
  26.  
  27.  
  28. class procedure TListOperations.create(var Header: TNodePointer);
  29.  
  30. begin
  31.     new(Header);
  32.     Header^.Next := Header;
  33. end;
  34.  
  35.  
  36. class function TListOperations.isEmpty(var Header: TNodePointer): boolean;
  37.  
  38. begin
  39.     if Header =  Header^.Next then
  40.         isEmpty := true
  41.     else
  42.         isEmpty := false;
  43. end;
  44.  
  45.  
  46. class procedure TListOperations.push(Header : TNodePointer; Data: integer);
  47.  
  48. var
  49.     PCurrentNode: TNodePointer;
  50.  
  51. begin
  52.     PCurrentNode := Header^.Next;
  53.     while (PCurrentnode^.next <> Header) do
  54.         PCurrentnode := PCurrentnode^.next;
  55.     new(PCurrentNode^.next);
  56.     PCurrentNode := PCurrentNode^.next;
  57.     PCurrentNode^.Value := Data;
  58.     PCurrentNode^.next := Header;
  59. end;
  60.  
  61.  
  62. class function TListOperations.length(Header: TNodePointer): Integer;
  63.  
  64. var
  65.     i, Length: integer;
  66.     PCurrentNode: TNodePointer;
  67.  
  68. begin
  69.     Length := 0;
  70.     PCurrentNode := Header^.Next;
  71.     while (PCurrentNode <> Header) do
  72.     begin
  73.         inc(Length);
  74.         PCurrentNode := PCurrentNode^.Next;
  75.     end;
  76.     Result := Length;
  77. end;
  78.  
  79.  
  80. class procedure TListOperations.deleteList(var Header: TNodePointer);
  81.  
  82. var
  83.     PCurrentNode, Temp: TNodePointer;
  84.  
  85. begin
  86.     PCurrentNode := Header^.Next;
  87.     while PCurrentNode <> Header do
  88.     begin
  89.         Temp := PCurrentNode;
  90.         PCurrentNode := PCurrentNode^.next;
  91.         Dispose(Temp);
  92.     end;
  93.     Header^.Next := Header;
  94. end;
  95.  
  96.  
  97. class procedure TListOperations.delete(var Header: TNodePointer; Index, NumberOfElements: Integer);
  98.  
  99. var
  100.     i, Counter, NumberOfDeleted: integer;
  101.     PCurrentNode, PDeletingNode, PStart: TNodePointer;
  102.     AreElementsDeleted: boolean;
  103.  
  104. begin
  105.     PCurrentNode := Header^.Next;
  106.     NumberOfDeleted := 0;
  107.     Counter := 1;
  108.     if NumberOfElements >= TListOperations.length(Header) then
  109.          TListOperations.deleteList(Header)
  110.     else
  111.     begin
  112.         while (NumberOfDeleted <> NumberOfElements) do
  113.         begin
  114.             if (PCurrentNode^.Next = Header) then
  115.             begin
  116.                 if ((Counter + 1) mod Index = 0) then
  117.                 begin
  118.                     PDeletingNode := Header^.Next;
  119.                     Header^.Next := Header^.Next^.Next;
  120.                     PCurrentNode := Header^.Next;
  121.                     Dispose(PDeletingNode);
  122.                     inc(NumberOfDeleted);
  123.                     inc(Counter);
  124.                 end
  125.                 else
  126.                     PCurrentNode := Header^.Next;
  127.                 inc(Counter);
  128.             end
  129.             else
  130.             begin
  131.                 if ((Counter + 1) mod Index = 0) then
  132.                 begin
  133.                     PDeletingNode := PCurrentNode^.Next;
  134.                     PCurrentNode^.Next := PCurrentNode^.Next^.Next;
  135.                     PCurrentNode := PCurrentNode^.Next;
  136.                     Dispose(PDeletingNode);
  137.                     inc(NumberOfDeleted);
  138.                     inc(Counter);
  139.                 end
  140.                 else
  141.                     PCurrentNode := PCurrentNode^.Next;
  142.                 inc(Counter);
  143.             end;
  144.         end;
  145.     end;
  146. end;
  147.  
  148.  
  149. class function TListOperations.toString(Header: TNodePointer): string;
  150.  
  151. var
  152.     PCurrentNode: TNodePointer;
  153.     ListString: String;
  154.  
  155. begin
  156.     ListString := '';
  157.     PCurrentNode := Header^.Next;
  158.     while (PCurrentNode^.Next <> Header) do
  159.     begin
  160.         ListString := ListString +  ' — ' + intToStr(PCurrentNode^.Value);
  161.         PCurrentNode := PCurrentNode^.Next;
  162.     end;
  163.     ListString := ListString +  ' — ' + intToStr(PCurrentNode^.Value) + ' —';
  164.     Result := ListString;
  165. end;
  166.  
  167. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement