-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntList.java
executable file
·192 lines (160 loc) · 4.21 KB
/
IntList.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
import java.util.Random;
public class IntList {
private int[] arr;
int size = 0;
public static void main(String[] args){
//rotate Code
System.out.println("Rotate");
IntList reverse = new IntList(4);
reverse.add(1);
reverse.add(2);
reverse.add(3);
reverse.add(4);
reverse.rotate();
System.out.println(reverse.get(0));
}
public IntList(int default_size) {
arr = new int[default_size];
}
int get(int index) {
return arr[index];
}
void set(int index, int element) {
arr[index] = element;
}
void add(int element) {
arr[size++] = element;
if(size==arr.length) resize();
}
void add(int index, int element) {
for(int i = size; i>index; i--) {
arr[i] = arr[i-1];
}
arr[index] = element;
size++;
if(size==arr.length) resize();
}
void remove(int index) {
for(int i = index; i<size; i++) {
arr[i] = arr[i+1];
}
//resize here. Ask before tho. But probs
size--;
}
void resize() {
}
/*
sort — sorts a List using a merge sort algorithm, which provides a fast, stable sort. (A stable sort is one that does not reorder equal elements.)
*/
static void sort(IntList l) {
}
/*
shuffle — randomly permutes the elements in a List.
*/
static void shuffle(IntList l) {
Random gen = new Random();
IntList dumbyList = new IntList(l.size);
int x;
int spot;
for(int i = 0; i<l.size; i++){
spot = gen.nextInt(l.size);
x = l.arr[spot];
l.remove(spot);
dumbyList.add(x);
}
copy(dumbyList, l);
}
/*
reverse — reverses the order of the elements in a List.
*/
static void reverse(IntList l) {
}
/*
rotate — rotates all the elements in a List by a specified distance.
shifts it one to the right
*/
void rotate() {
int[] copylist = new int[this.size];
for (int i = 0; i < this.size; i++) {
copylist[i] = this.get(i);
}
for (int i = 0; i < copylist.length; i++) {
if (i == this.size-1) arr[0] = copylist[i];
else {
arr[i] = copylist[i + 1];
}
}
}
/*
swap — swaps the elements at specified positions in a List.
*/
static void swap(IntList l, int i, int j) {
}
/*
replaceAll — replaces all occurrences of one specified value with another.
*/
static void replaceAll(IntList l, int find, int rep) {
for (int i = 0; i < l.size; i++) {
if(l.get(i) == find){
l.set(i, rep);
}
}
}
/*
fill — overwrites every element in a List with the specified value.
iterate through
replace everything
*/
static void fill(IntList l, int rep) {
for(int i = 0; i < l.size; i ++){
l.remove(i);
l.add(i, rep);
}
}
/*
copy — copies the source List into the destination List.
*/
static void copy(IntList l1, IntList l2) {
for(int i = 0; i < l1.size; i ++){
l2.add(i);
l1.remove(i);
}
}
}
/*
search — searches for an element in an List.
*/
static boolean search(IntList l, int i) {
return false;
}
/*
indexOfSubList — returns the index of the first sublist of one List that is equal to another.
*/
static int indexOfSubList(IntList l, IntList sublist) {
return 0;
}
/*
lastIndexOfSubList — returns the index of the last sublist of one List that is equal to another.
*/
static int lastIndexOfSubList(IntList l, IntList sublist) {
return 0;
}
/*
difference returns a list of all items that are in only one of the two lists (XOR)
*/
static IntList difference(IntList l1, IntList l2) {
return null;
}
/*
return a new list with only the odd items
*/
static IntList oddList(IntList l) {
return null;
}
/*
returns true if all items in i are in list l
*/
static boolean search(IntList l, int[] i) {
return false;
}
}