-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyDeque.java
121 lines (116 loc) · 3.15 KB
/
MyDeque.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Queue X2
public class MyDeque implements Deque
{
private Object [] v;
private int vSize;
public MyDeque()
{
makeEmpty();
}
// inserisce l'elemento all'inizio della doppia coda
// @param elemento da inserire
public void addFirst(Object element)
{
if (vSize == v.length)
{
Object [] newV = new Object[ 2 * v.length];
for (int i = 0; i < v.length; i++)
{
v[i] = newV[i];
v = newV;
}
}
for (int i = vSize - 1; i > 0; i--)
{
v[i] = v[i + 1];
}
v[0] = element;
vSize++;
}
// inserisce l'elemento alla fine della doppia coda
// @param elemento da inserire
public void addLast(Object element)
{
if (vSize == v.length)
{
Object [] newV = new Object[ 2 * v.length];
for (int i = 0; i < v.length; i++)
{
v[i] = newV[i];
v = newV;
}
}
v[vSize] = element;
vSize++;
}
// restituisce l'elemento all'inizio della doppia coda
// @return il primo elemento della doppia coda
// @throws EmptyDequeException se la coda è vuota
public Object getFirst() throws EmptyDequeException
{
if (vSize == 0)
{
throw new EmptyDequeException();
}
return v[0];
}
// restituisce l'elemento alla fine della doppia coda
// @return l'ultimo elemento della doppia coda
// @throws EmptyDequeException se la coda è vuota
public Object getLast() throws EmptyDequeException
{
if (vSize == 0)
{
throw new EmptyDequeException();
}
return v[vSize - 1];
}
// rimuove l'elemento all'inizio della doppia coda
// @return il primo elemento della doppia coda
// @throws EmptyDequeException se la coda è vuota
public Object removeFirst() throws EmptyDequeException
{
if (vSize == 0)
{
throw new EmptyDequeException();
}
Object obj = v[0];
v[0] = null;
for ( int i = 1; i < v.length; i++)
{
v[i - 1] = v[i];
}
vSize--;
return obj;
}
// rimuove l'elemento alla fine della doppia coda
// @return l'ulimo elemento della doppia coda
// @throws EmptyDequeException se la coda è vuota
public Object removeLast() throws EmptyDequeException
{
if (vSize == 0)
{
throw new EmptyDequeException();
}
Object obj = v[vSize - 1];
v[vSize - 1] = null;
vSize--;
return obj;
}
//controlla se la doppia coda è vuota
public boolean isEmpty()
{
return (vSize == 0);
}
//crea una doppia coda vuota
public void makeEmpty()
{
Object [] v = new Object[1];
int vSize = 0;
}
//restituisce i valori all'interno della doppia coda
public int size()
{
return vSize;
}
}