Guest User

Untitled

a guest
Jun 22nd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. //---------------------------------------------------------------------------
  2.  
  3. #include <vcl.h>
  4. #pragma hdrstop
  5.  
  6. #include "Unit1.h"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma resource "*.dfm"
  10. TForm1 *Form1;
  11. //---------------------------------------------------------------------------
  12. __fastcall TForm1::TForm1(TComponent* Owner)
  13. : TForm(Owner)
  14. {
  15. }
  16.  
  17. //typedef struct {char Name[20]; char Vname[20]; int Alter;} DatenTp;
  18. typedef int DatenTp;
  19.  
  20.  
  21. struct KnotenTp{DatenTp Data; KnotenTp* Next;} ;
  22.  
  23. class Liste
  24. {
  25. private:
  26. KnotenTp* Head;
  27. KnotenTp* UL; //Unterliste
  28. int count;
  29. void Init(DatenTp); //erschafft Liste
  30. void Pfeil(KnotenTp* Von, KnotenTp* Nach);//setzt Pfeil von -> nach
  31. KnotenTp* Bilden(DatenTp); //erschafft neue "Kiste"
  32.  
  33. public:
  34. Liste(); //liste wird initialisiert
  35. ~Liste(); //liste wird aus speicher gelöscht
  36. bool NotEoL(); //schaut ob noch in der Liste
  37. bool end();
  38. int GetCount();
  39. DatenTp operator[](int); //siehe NotEoL
  40. KnotenTp* Weiter(); //setzt UL ein weiter
  41. void Anfuegen(DatenTp);//fügt neuen kasten an
  42. DatenTp HoleDaten(); //holt daten, die dort stehen
  43. void Begin(); //setzt UL auf Head
  44. void Entfernen();
  45. };
  46. //---------------------------------------------------------------------------
  47. KnotenTp* Liste::Weiter()
  48. {
  49. if(NotEoL())
  50. UL=UL->Next;
  51. return UL;
  52. }
  53. //---------------------------------------------------------------------------
  54. void Liste::Begin()
  55. {
  56. UL=Head;
  57. }
  58. //---------------------------------------------------------------------------
  59. DatenTp Liste::operator[](int s)
  60. {
  61. Begin();
  62. for(int g=0;g<s-1;g++)
  63. {
  64. Weiter();
  65. }
  66. return HoleDaten();
  67. }
  68. //---------------------------------------------------------------------------
  69. void Liste::Pfeil(KnotenTp* Von, KnotenTp* Nach)
  70. {
  71. Von->Next=Nach;
  72. }
  73. //---------------------------------------------------------------------------
  74. bool Liste::NotEoL()
  75. {
  76. return UL->Next->Next!=0;
  77. }
  78. //---------------------------------------------------------------------------
  79. KnotenTp* Liste::Bilden(DatenTp D)
  80. {
  81. KnotenTp* NeuKnoten;
  82. NeuKnoten=new KnotenTp;
  83. NeuKnoten->Data=D;
  84. NeuKnoten->Next=0;
  85. return NeuKnoten;
  86. }
  87. //---------------------------------------------------------------------------
  88. int Liste::GetCount()
  89. {
  90. return count;
  91. }
  92. //---------------------------------------------------------------------------
  93. void Liste::Init(DatenTp Data)
  94. {
  95. KnotenTp* Tail;
  96. Head=Bilden(Data);
  97. Tail=Bilden(Data);
  98. Pfeil(Head,Tail);
  99. UL=Head;
  100. }
  101. //---------------------------------------------------------------------------
  102. DatenTp Liste::HoleDaten()
  103. {
  104. return UL->Next->Data;
  105. }
  106. //---------------------------------------------------------------------------
  107. void Liste::Anfuegen(DatenTp D)
  108. {
  109. KnotenTp* NeuKnoten;
  110. NeuKnoten=Bilden(D);
  111. Pfeil(NeuKnoten,UL->Next);
  112. Pfeil(UL,NeuKnoten);
  113. count++;
  114. Weiter();
  115. }
  116. //---------------------------------------------------------------------------
  117. bool Liste::end()
  118. {
  119. return NotEoL();
  120. }
  121. //---------------------------------------------------------------------------
  122. void Liste::Entfernen()
  123. {
  124. KnotenTp* LoeschKnoten;
  125. if (NotEoL())
  126. {
  127. LoeschKnoten=UL->Next;
  128. Pfeil(UL,LoeschKnoten->Next);
  129. delete LoeschKnoten;
  130. }
  131. }
  132. //---------------------------------------------------------------------------
  133. Liste::Liste()
  134. {
  135. DatenTp k=0;//{"NIX","NIX",0};
  136. count=0;
  137. Init(k);
  138. }
  139. //---------------------------------------------------------------------------
  140. Liste::~Liste()
  141. {
  142. }
  143. //---------------------------------------------------------------------------
  144. Liste L;
  145. //---------------------------------------------------------------------------
  146. void __fastcall TForm1::Button1Click(TObject *Sender)
  147. {
  148. L.Begin();
  149. for ( int i = 0; i < 5 ; i++)
  150. L.Anfuegen(random(20));
  151. }
  152. //---------------------------------------------------------------------------
  153. void __fastcall TForm1::Button2Click(TObject *Sender)
  154. {
  155. Memo1->Lines->Add("Hier kommt die Liste");
  156. L.Begin();
  157. while ( L.NotEoL())
  158. {
  159. Memo1->Lines->Add(L.HoleDaten());
  160. L.Weiter();
  161. }
  162. }
  163. //---------------------------------------------------------------------------
  164. void __fastcall TForm1::Button5Click(TObject *Sender)
  165. {
  166. L.Weiter();
  167. Label2->Caption = L.HoleDaten();
  168. }
  169. //---------------------------------------------------------------------------
  170. void __fastcall TForm1::Button6Click(TObject *Sender)
  171. {
  172. L.Begin();
  173. Label2->Caption = L.HoleDaten();
  174. }
  175. //---------------------------------------------------------------------------
  176. void __fastcall TForm1::Button7Click(TObject *Sender)
  177. {
  178. L.Anfuegen(Edit1->Text.ToInt());
  179. L.Entfernen();
  180. }
  181. //---------------------------------------------------------------------------
  182.  
  183. void __fastcall TForm1::Button3Click(TObject *Sender)
  184. {
  185. Edit2->Text=IntToStr(L.GetCount());
  186. }
  187. //---------------------------------------------------------------------------
  188. void __fastcall TForm1::Button4Click(TObject *Sender)
  189. {
  190. int w=StrToInt(Edit3->Text);
  191. Memo1->Lines->Add("Wert an Stelle "+IntToStr(w)+": "+IntToStr(L[w]));
  192. }
  193. //---------------------------------------------------------------------------
Add Comment
Please, Sign In to add comment