-
Notifications
You must be signed in to change notification settings - Fork 22
/
TelephoneDirectory
120 lines (107 loc) · 2.71 KB
/
TelephoneDirectory
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
/*
Problem
You will have to design a telephone directory where the following operations would be supported.
1. GET: It will provide a number which is not assigned to anyone
2. CHECK: Given a number it will tell whether it is assigned to anyone or not
3. RELEASE: It will recycle or release a number
*/
public class TelephoneDirectory
{
public static void main(String[] args)
{
Directory directory = new Directory(5);
int number1 = directory.getAvailableNumber();
System.out.println(number1);
int number2 = directory.getAvailableNumber();
System.out.println(number2);
System.out.println(directory.isAvailable(3));
int number3 = directory.getAvailableNumber();
System.out.println(number3);
System.out.println(directory.isAvailable(3));
int number4 = directory.getAvailableNumber();
System.out.println(directory.isAvailable(3));
System.out.println(number4);
int number5 = directory.getAvailableNumber();
System.out.println(number5);
int number6 = directory.getAvailableNumber();
System.out.println(number6);
int number7 = directory.getAvailableNumber();
System.out.println(number7);
directory.release(3);
System.out.println(directory.isAvailable(3));
int number8 = directory.getAvailableNumber();
System.out.println(number8);
System.out.println(directory.isAvailable(3));
}
private static class Directory
{
Node[] nodes;
Node head;
public Directory(int maxNumbers)
{
nodes = new Node[maxNumbers];
for (int i = 0; i < maxNumbers; ++i)
{
nodes[i] = new Node(i);
if (i != 0)
{
nodes[i].previous = nodes[i - 1];
nodes[i - 1].next = nodes[i];
}
}
nodes[maxNumbers - 1].next = nodes[0];
nodes[0].previous = nodes[maxNumbers - 1];
head = nodes[0];
}
public int getAvailableNumber()
{
if (head == null)
return -1;
int temp = head.number;
head.available = false;
if (head.next == head)
head = null;
else
{
head.previous.next = head.next;
head.next.previous = head.previous;
head = head.next;
}
return temp;
}
public boolean isAvailable(int number)
{
return nodes[number].available;
}
public void release(int number)
{
if (nodes[number].available == false)
{
nodes[number].available = true;
if (head == null)
{
head = nodes[number];
nodes[number].next = nodes[number];
nodes[number].previous = nodes[number];
} else
{
nodes[number].next = head.next;
nodes[number].previous = head;
head.next.previous = nodes[number];
head.next = nodes[number];
}
}
}
private static class Node
{
boolean available = true;
Node next;
Node previous;
int number;
public Node(int number)
{
this.number = number;
}
}
}
}