-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamischeIntegerReihung.java
222 lines (206 loc) · 4.65 KB
/
DynamischeIntegerReihung.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
class DynIntArray
{
void add(int e)
{
}
void setElementAt(int i, int e)
{
}
int getElementAt(int i)
{
return 0;
}
int getElementCount()
{
return 0;
}
void print()
{
}
public static void main(String args[])
{
for (int i = 0; i < 2; i++)
{
DynIntArray da = null;
//macht erst ein Array und gibt es aus und wiederholt dann das selbe mit einer Liste
if (i == 0)
{
da = new DIAarray();
}
else
if (i == 1)
{
da = new DIAlist();
}
da.add(4); da.add(8); da.add(10); da.add(1); da.print();
da.setElementAt(0, 0); da.add(5);
da.setElementAt(2, da.getElementAt(2) + 10); da.print();
System.out.println("DIA: elements=" + da.getElementCount() +
" da[4]=" + da.getElementAt(4) +
" da[9]=" + da.getElementAt(9));
}
}
}
class DIAarray extends DynIntArray
{
private int Array [] = new int[1] ;
private int Anzahl = 0;
private int Kapazität = 1;
void add(int e)
{
//vergrößert Array wenn es voll ist
if(Anzahl == Kapazität)
{
int temp[] = new int[Kapazität+1];
for(int i = 0; i<Kapazität; i++)
{
temp[i]= Array[i];
}
Array = temp;
Kapazität++;
}
Array[Anzahl] = e;
Anzahl++;
}
void setElementAt(int i, int e)
{
if(i<=Anzahl-1)
{
Array[i]=e;
}
}
int getElementAt(int i)
{
if(i<=Anzahl-1)
{
return Array[i];
}
else
{
return 0;
}
}
int getElementCount()
{
return Anzahl;
}
void print()
{
System.out.print("[");
for(int i = 0; i<Anzahl; i++)
{
System.out.print(Array[i]);
if(i<Anzahl-1)
{
System.out.print(", ");
}
}
System.out.print("]");
System.out.println("");
//der hätte es einfacher gemacht aber wir sollen ja keine vorgefertigten Klassen implementieren
//System.out.println(Arrays.toString(Array));
}
}
class DIAlist extends DynIntArray
{
private node head = null;
class node
{
private int data;
private node next;
//Konstruktor für Knoten
node (int data)
{
this.data = data;
this.next = null;
}
}
//an letzte Stelle einfügen
public void add(int e)
{
node newNode = new node(e);
//falls newNode das erste Element ist
if(this.head == null)
{
this.head = newNode;
}
else
{
//Knoten der die Liste bis zum Ende durchwandert
node last = this.head;
while(last.next!=null)
{
//Knoten wird auf eigenen Nachfolger gesetzt
last = last.next;
}
last.next = newNode;
}
}
void setElementAt(int i, int e)
{
node set = this.head;
int count = 0;
//geht Liste durch und ersetzt, wenn ntes Element gleich i ist
while(set!=null)
{
if(count == i)
{
set.data = e;
return;
}
set = set.next;
count++;
}
}
int getElementAt(int i)
{
node get = this.head;
int count = 0;
//basically genau der Setter
while(get!=null)
{
if(count==i)
{
return get.data;
}
count++;
get = get.next;
}
//Element nicht vorhanden
return 0;
}
int getElementCount()
{
int count = 0;
node current = this.head;
while(current!=null)
{
count++;
current = current.next;
}
return count;
}
void print()
{
if (this.head == null)
{
System.out.println("Liste leer");
}
else
{
node current = this.head;
System.out.print("[");
while (current != null)
{
System.out.print(current.data);
if(current.next != null)
{
System.out.print(", ");
}
current = current.next;
}
System.out.print("]");
System.out.println("");
}
}
}