Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit LinkedListUnit;
- interface
- uses
- System.SysUtils, Classes;
- Type
- TNodePointer = ^TNode;
- TNode = record
- Value : Integer;
- Next : TNodePointer;
- end;
- TListOperations = class
- class procedure create(var Header: TNodePointer); static;
- class procedure push(Header : TNodePointer; Data: integer); static;
- class function length(Header: TNodePointer): integer; static;
- class procedure delete(var Header: TNodePointer; Index, NumberOfElements: Integer); static;
- class function toString(Header: TNodePointer): String; static;
- class procedure deleteList(var Header: TNodePointer); static;
- class function isEmpty(var Header: TNodePointer): boolean; static;
- end;
- implementation
- class procedure TListOperations.create(var Header: TNodePointer);
- begin
- new(Header);
- Header^.Next := Header;
- end;
- class function TListOperations.isEmpty(var Header: TNodePointer): boolean;
- begin
- if Header = Header^.Next then
- isEmpty := true
- else
- isEmpty := false;
- end;
- class procedure TListOperations.push(Header : TNodePointer; Data: integer);
- var
- PCurrentNode: TNodePointer;
- begin
- PCurrentNode := Header^.Next;
- while (PCurrentnode^.next <> Header) do
- PCurrentnode := PCurrentnode^.next;
- new(PCurrentNode^.next);
- PCurrentNode := PCurrentNode^.next;
- PCurrentNode^.Value := Data;
- PCurrentNode^.next := Header;
- end;
- class function TListOperations.length(Header: TNodePointer): Integer;
- var
- i, Length: integer;
- PCurrentNode: TNodePointer;
- begin
- Length := 0;
- PCurrentNode := Header^.Next;
- while (PCurrentNode <> Header) do
- begin
- inc(Length);
- PCurrentNode := PCurrentNode^.Next;
- end;
- Result := Length;
- end;
- class procedure TListOperations.deleteList(var Header: TNodePointer);
- var
- PCurrentNode, Temp: TNodePointer;
- begin
- PCurrentNode := Header^.Next;
- while PCurrentNode <> Header do
- begin
- Temp := PCurrentNode;
- PCurrentNode := PCurrentNode^.next;
- Dispose(Temp);
- end;
- Header^.Next := Header;
- end;
- class procedure TListOperations.delete(var Header: TNodePointer; Index, NumberOfElements: Integer);
- var
- i, Counter, NumberOfDeleted: integer;
- PCurrentNode, PDeletingNode, PStart: TNodePointer;
- AreElementsDeleted: boolean;
- begin
- PCurrentNode := Header^.Next;
- NumberOfDeleted := 0;
- Counter := 1;
- if NumberOfElements >= TListOperations.length(Header) then
- TListOperations.deleteList(Header)
- else
- begin
- while (NumberOfDeleted <> NumberOfElements) do
- begin
- if (PCurrentNode^.Next = Header) then
- begin
- if ((Counter + 1) mod Index = 0) then
- begin
- PDeletingNode := Header^.Next;
- Header^.Next := Header^.Next^.Next;
- PCurrentNode := Header^.Next;
- Dispose(PDeletingNode);
- inc(NumberOfDeleted);
- inc(Counter);
- end
- else
- PCurrentNode := Header^.Next;
- inc(Counter);
- end
- else
- begin
- if ((Counter + 1) mod Index = 0) then
- begin
- PDeletingNode := PCurrentNode^.Next;
- PCurrentNode^.Next := PCurrentNode^.Next^.Next;
- PCurrentNode := PCurrentNode^.Next;
- Dispose(PDeletingNode);
- inc(NumberOfDeleted);
- inc(Counter);
- end
- else
- PCurrentNode := PCurrentNode^.Next;
- inc(Counter);
- end;
- end;
- end;
- end;
- class function TListOperations.toString(Header: TNodePointer): string;
- var
- PCurrentNode: TNodePointer;
- ListString: String;
- begin
- ListString := '';
- PCurrentNode := Header^.Next;
- while (PCurrentNode^.Next <> Header) do
- begin
- ListString := ListString + ' — ' + intToStr(PCurrentNode^.Value);
- PCurrentNode := PCurrentNode^.Next;
- end;
- ListString := ListString + ' — ' + intToStr(PCurrentNode^.Value) + ' —';
- Result := ListString;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement