-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLinkedBag.cs
123 lines (100 loc) · 2.13 KB
/
LinkedBag.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
namespace SedgewickWayne.Algorithms
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class Bag : IBag
{
internal sealed class BagEnumerator : IEnumerator
{
Node current;
internal Bag bag;
public BagEnumerator(Bag bag)
{
this.bag = bag;
this.current = bag.first;
}
public BagEnumerator(Bag bag, Node node)
{
this.bag = bag;
this.current = node;
}
public bool MoveNext()
{
return this.current != null;
}
public void Reset()
{
this.current = this.bag.first;
}
public void Dispose()
{
//throw new NotImplementedException();
}
/* [Signature("()object;")]*/
object IEnumerator.Current
{
get
{
return Current;
}
}
public object Current
{
get
{
if (current == null) throw new InvalidOperationException();
object result = this.current.Item;
this.current = this.current.Next;
return result;
}
}
}
internal sealed class Node
{
//[Signature("TItem;")]
public object Item { get; private set; }
//[Signature("LBag$Node<TItem;>;")]
public Node Next { get; private set; }
public Node(object item, Node next)
{
Item = item;
Next = next;
}
}
private int N;
//[Signature("LBag$Node<TItem;>;")]
private Node first;
// public bool IsEmpty
public bool IsEmpty
{
get
{
return this.first == null;
}
}
public Bag()
{
this.first = null;
this.N = 0;
}
public void Add(object obj)
{
var oldFirst = this.first;
this.first = new Node(obj, oldFirst);
this.N++;
}
public int Size { get { return this.N; } }
public IEnumerator GetEnumerator()
{
return new BagEnumerator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return new BagEnumerator(this);
}
}
}