-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleList.cs
106 lines (96 loc) · 2.7 KB
/
SimpleList.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab3
{
public class SimpleList<T> : IEnumerable<T> where T : IComparable
{
protected SimpleListItem<T> first = null;
protected SimpleListItem<T> last = null;
int _count;
public int Count
{
get { return _count; }
protected set { _count = value; }
}
public void Add(T element)
{
SimpleListItem<T> newItem = new SimpleListItem<T>(element);
this.Count++;
if (last == null)
{
this.first = newItem;
this.last = newItem;
}
else
{
this.last.next = newItem;
this.last = newItem;
}
}
public SimpleListItem<T> GetItem(int number)
{
if ((number < 0) || (number >= this.Count))
{
throw new Exception("Going out of range");
}
SimpleListItem<T> current = this.first;
int i = 0;
while (i < number)
{
current = current.next;
i++;
}
return current;
}
public T Get(int number)
{
return GetItem(number).data;
}
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
SimpleListItem<T> current = this.first;
while (current != null)
{
yield return current.data;
current = current.next;
}
}
private void Swap(int i, int j)
{
SimpleListItem<T> ci = GetItem(i);
SimpleListItem<T> cj = GetItem(j);
T temp = ci.data;
ci.data = cj.data;
cj.data = temp;
}
public void Sort()
{
Sort(0, this.Count - 1);
}
private void Sort(int low, int high)
{
int i = low;
int j = high;
T x = Get((low + high) / 2);
do
{
while (Get(i).CompareTo(x) < 0) ++i;
while (Get(j).CompareTo(x) > 0) --j;
if (i <= j)
{
Swap(i, j);
i++; j--;
}
} while (i <= j);
if (low < j) Sort(low, j);
if (i < high) Sort(i, high);
}
}
}