-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayList.cs
More file actions
122 lines (122 loc) · 3.43 KB
/
ArrayList.cs
File metadata and controls
122 lines (122 loc) · 3.43 KB
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
namespace main.DataStructure
{
public class ArrayList<T> where T : IComparable
{
const int N = 10000;
private T[] a = new T[N];
private int size;
public ArrayList() {}
public ArrayList(int sz)
{
this.size = sz;
}
public T this[int indx]
{
get
{
if (indx >= size || indx < 0)
throw new IndexOutOfRangeException("out of range");
return a[indx];
}
set
{
if (indx >= size || indx < 0)
throw new IndexOutOfRangeException("out of range");
a[indx] = value;
}
}
public void push_back(T val)
{
if (size == N)
throw new Exception("the array is full size");
a[size++] = val;
}
public void pop_back()
{
if (isEmpty())
{
throw new Exception("the array is empty");
}
size--;
}
public int Size() => size;
public void print()
{
for (int i = 0; i < size; i++)
{
Console.Write(a[i] + " ");
}
Console.WriteLine();
}
public T front()
{
if (isEmpty())
throw new Exception("array is Empty");
return a[0];
}
public T back()
{
if (isEmpty())
throw new Exception("array is Empty");
return a[size - 1];
}
public void insert(T val, int indx)
{
if (size == N)
throw new Exception("the array is full size");
else if (indx < 0 || indx >= size)
throw new IndexOutOfRangeException();
for (int i = size; i > indx; i--)
{
a[i] = a[i - 1];
}
a[indx] = val;
size++;
}
public T Begin() => a[0];
public T End() => a[size - 1];
public void erase(int index)
{
if (isEmpty())
throw new Exception("array is Empty");
else if (index < 0 || index > size)
throw new IndexOutOfRangeException();
for (int i = index; i < size; i++)
{
a[i] = a[i + 1];
}
size--;
}
public int BinarySearch(int l, int r, T val)
{
if (l <= r)
{
int mid = l + (r - l) / 2;
if (object.Equals(a[mid], val))
return mid;
int cmp = (a[mid] as IComparable).CompareTo(val);
bool ok = cmp > 0;
if (ok)
return BinarySearch(l, mid - 1, val);
else
return BinarySearch(mid + 1, r, val);
}
return -1;
}
public int LinearSearch(T val)
{
for (int i = 0; i < size; i++)
{
if (object.Equals(a[i], val))
return i;
}
return -1;
}
public bool isEmpty() => size == 0;
public void clear()
{
a = null;
size = 0;
}
}
}