Advertisement
MaksNew

slBTR

Mar 15th, 2021
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. Unit Unit2;
  2.  
  3. Interface
  4.  
  5. Uses
  6. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7. Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
  8.  
  9. Type
  10. TFormAddElement = class(TForm)
  11. Label1: TLabel;
  12. EditAdd: TEdit;
  13. ButtonOK: TButton;
  14. Procedure ButtonOKClick(Sender: TObject);
  15. Procedure FormClose(Sender: TObject; var Action: TCloseAction);
  16. Private
  17. Public
  18. end;
  19.  
  20. Type
  21. SinglyLinkedList = class
  22. Type PNode = ^TNode; { указатель на узел }
  23. TNode = record { структура узла }
  24. Element: Integer; { элемент }
  25. Index: Word; { индекс узла }
  26. NextNode: PNode; { ссылка на следующий }
  27. End;
  28. Private
  29. FHeaderNode: PNode;
  30. FLastNode: PNode;
  31. Function IsEmpty: Boolean;
  32. Function GetSize: Word;
  33. Function NodeDataWithIndex(AIndex: Word): TNode;
  34. Public
  35. Constructor Create;
  36. Destructor Destroy;
  37. Procedure Append(ANodeElement: Integer);
  38. Procedure ShowCountElementEnter(Element: Word);
  39. Procedure Clear;
  40. Function PopLast: Integer;
  41. Procedure Remove(AIndex: Word);
  42. Procedure Debug;
  43. End;
  44.  
  45. Var
  46. FormAddElement: TFormAddElement;
  47. List: SinglyLinkedList;
  48. IsDeleteble: Boolean = False;
  49. IsAdd: Boolean = False;
  50. IsEnter: Boolean = False;
  51. Count: Integer = 1;
  52.  
  53. Implementation
  54. Uses Unit1;
  55.  
  56. Procedure SinglyLinkedList.Debug();
  57. Var
  58. CurNode: PNode;
  59. Begin
  60. CurNode := FHeaderNode^.NextNode;
  61. While CurNode <> Nil do
  62. Begin
  63. ShowMessage(IntToStr(CurNode^.Element));
  64. CurNode := CurNode^.NextNode;
  65. End;
  66. End;
  67.  
  68. Function SinglyLinkedList.IsEmpty: Boolean;
  69. Begin
  70. if FHeaderNode^.NextNode = Nil then
  71. IsEmpty := True
  72. else
  73. IsEmpty := False;
  74. End;
  75.  
  76. Procedure SinglyLinkedList.ShowCountElementEnter(Element: Word);
  77. Var
  78. Count, I: Word;
  79. Begin
  80. Count := 0;
  81. I := 0;
  82. While I < GetSize do
  83. Begin
  84. if (Element = NodeDataWithIndex(I).NextNode.Element) then
  85. Inc(Count);
  86. Inc(I);
  87. End;
  88. ShowMessage('Количество вхождений элемента ' + IntToStr(Element) + ' в список = '+ IntToStr(Count));
  89. End;
  90.  
  91. Function SinglyLinkedList.GetSize: Word;
  92. Var
  93. CurrentNode: TNode;
  94. Begin
  95. If IsEmpty then
  96. GetSize := 0
  97. Else
  98. Begin
  99. CurrentNode.NextNode := FHeaderNode^.NextNode;
  100. CurrentNode.Index := 0;
  101. While CurrentNode.NextNode <> Nil do
  102. Begin
  103. CurrentNode.NextNode := CurrentNode.NextNode^.NextNode;
  104. Inc(currentNode.Index);
  105. End;
  106. GetSize := CurrentNode.Index;
  107. End;
  108. End;
  109.  
  110. Constructor SinglyLinkedList.Create();
  111. Begin
  112. New(FHeaderNode);
  113. FHeaderNode^.NextNode := Nil;
  114. FLastNode := FHeaderNode;
  115. End;
  116.  
  117. Destructor SinglyLinkedList.Destroy;
  118. Begin
  119. Clear;
  120. Dispose(FHeaderNode);
  121. End;
  122.  
  123. Function SinglyLinkedList.NodeDataWithIndex(AIndex: Word): TNode;
  124. Var
  125. CurrentData: TNode;
  126. Begin
  127. CurrentData.NextNode := FHeaderNode^.NextNode;
  128. CurrentData.Index := 0;
  129. While CurrentData.Index <> AIndex do
  130. Begin
  131. CurrentData.NextNode := CurrentData.NextNode^.NextNode;
  132. Inc(CurrentData.Index);
  133. End;
  134. NodeDataWithIndex := CurrentData;
  135. End;
  136.  
  137. Procedure SinglyLinkedList.Append(ANodeElement: Integer);
  138. Var
  139. CurrentNode: PNode;
  140. Begin
  141. CurrentNode := FLastNode;
  142. New(CurrentNode^.NextNode);
  143. CurrentNode := CurrentNode^.NextNode;
  144. CurrentNode^.Element := ANodeElement;
  145. CurrentNode^.NextNode := Nil;
  146. FLastNode := CurrentNode;
  147. End;
  148.  
  149. Procedure SinglyLinkedList.Clear;
  150. Begin
  151. While FHeaderNode^.NextNode <> Nil do
  152. PopLast;
  153. End;
  154.  
  155. Function SinglyLinkedList.PopLast: Integer;
  156. Var
  157. I: Word;
  158. CurrentNode: PNode;
  159. Begin
  160. If FHeaderNode^.NextNode <> Nil then
  161. Begin
  162. CurrentNode := FHeaderNode;
  163. While CurrentNode^.NextNode <> FLastNode do
  164. CurrentNode := CurrentNode^.NextNode;
  165.  
  166. PopLast := FLastNode^.Element;
  167.  
  168. Dispose(CurrentNode^.NextNode);
  169. FLastNode := CurrentNode;
  170. FLastNode^.NextNode := Nil;
  171. End;
  172. End;
  173.  
  174. Procedure SinglyLinkedList.Remove(AIndex: Word);
  175. Var
  176. CurrentData: TNode;
  177. BuffLastNode: PNode;
  178. Begin
  179. If AIndex = 0 then
  180. Begin
  181. CurrentData.NextNode := FHeaderNode;
  182. CurrentData.Index := 0;
  183. End
  184. Else
  185. CurrentData := NodeDataWithIndex(AIndex - 1);
  186. If AIndex = GetSize-1 then
  187. PopLast
  188. Else
  189. Begin
  190. BuffLastNode := FLastNode;
  191. FLastNode := CurrentData.NextNode^.NextNode;
  192. CurrentData.NextNode^.NextNode := FLastNode^.NextNode;
  193. FLastNode := BuffLastNode;
  194. End;
  195. End;
  196.  
  197. {$R *.dfm}
  198.  
  199. Function IsElementCorrect(EditAdd:TEdit):Boolean;
  200. Var
  201. IsCorrect: Boolean;
  202. Begin
  203. IsCorrect := True;
  204. Try
  205. StrToInt(EditAdd.Text)
  206. Except
  207. IsCorrect := False;
  208. End;
  209. IsElementCorrect := IsCorrect;
  210. End;
  211.  
  212.  
  213. Procedure TFormAddElement.ButtonOKClick(Sender: TObject);
  214. Var
  215. Element: Integer;
  216. Begin
  217. If IsAdd then
  218. Begin
  219. If count = 1 then
  220. Begin
  221. List := SinglyLinkedList.Create();
  222. IsDeleteble := True;
  223. End;
  224. If IsElementCorrect(EditAdd) then
  225. Begin
  226. Element := StrToInt(EditAdd.Text);
  227. ListForm.StringGrid1.Cells[1,count] := EditAdd.Text;
  228. ListForm.StringGrid1.Cells[0,count] := IntToStr(Count);
  229. ListForm.StringGrid1.RowCount := ListForm.StringGrid1.RowCount + 1;
  230. List.Append(Element);
  231. Inc(Count);
  232. EditAdd.Text := '';
  233. IsAdd := False;
  234. FormAddElement.Close;
  235. End
  236. Else
  237. MessageDlg('Введите целое число в пределах +- 2*10^9!', mtError, [mbOK], 0)
  238. End;
  239. If IsEnter then
  240. Begin
  241. If IsElementCorrect(EditAdd) then
  242. Begin
  243. Element := StrToInt(EditAdd.Text);
  244. List.ShowCountElementEnter(Element);
  245. //ShowMessage(IntToStr(List.ShowCountElementEnter(Element)));
  246. IsEnter := False;
  247. FormAddElement.Close;
  248. End;
  249. End;
  250. End;
  251.  
  252. Procedure TFormAddElement.FormClose(Sender: TObject; var Action: TCloseAction);
  253. Begin
  254. IsEnter := False;
  255. IsAdd := False;
  256. EditAdd.Text := '';
  257. End;
  258.  
  259. End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement