-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExchangeList.java
99 lines (91 loc) · 1.51 KB
/
ExchangeList.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
import java.util.*;
public class ExchangeList
{
Node head;
static class Node
{
Exchange exchange;
Node next, prev;
Node(Exchange e)
{
exchange = e;
next = prev = null;
}
}
public boolean IsEmpty()
{
return (head==null);
}
public boolean IsMember(Exchange e)
{
Node temp = head;
while(temp != null)
{
if(temp.exchange.equals(e))
return true;
temp = temp.next;
}
return false;
}
public void Insert(Exchange e)
{
Node tmp = new Node(e);
if(head==null)
{
head = tmp;
return;
}
Node n = head;
while(n.next != null)
n = n.next;
tmp.prev = n;
n.next = tmp;
}
public void InsertHead(Exchange e)
{
Node tmp = new Node(e);
tmp.next = head;
if(head != null)
head.prev = tmp;
head = tmp;
}
public void Delete(Exchange e)
{
Node temp = head;
while(temp != null)
{
if(temp.exchange.equals(e))
{
Node t = temp.next;
if(temp.prev != null) temp.prev.next = t;
else head = t;
if(t != null) t.prev = temp.prev;
return;
}
temp = temp.next;
}
throw new RuntimeException("Error - Exchange with identifier " + e.id + " is not present in the list");
}
public int size()
{
Node tmp = head;
int count = 0;
while(tmp != null)
{
count++;
tmp = tmp.next;
}
return count;
}
public Exchange getMember(int i)
{
if(this.size() > i && i>=0)
{
Node temp = head;
for(int j=0; j<i; j++)
temp = temp.next;
return temp.exchange;
}
throw new RuntimeException("Error - Index out of bound");
}
}