Repository navigation
Expand file tree
/
Copy pathDeque.java
More file actions
135 lines (113 loc) · 3.69 KB
/
Copy pathDeque.java
File metadata and controls
135 lines (113 loc) · 3.69 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
//Deque.java
import java.util.*;
import java.lang.*;
public class Deque<Item> implements Iterable<Item> {
private Node<Item> first;
private Node<Item> last;
private Item item;
private int N;
private class Node<Item> {
private Item item;
private Node<Item> next;
private Node<Item> previous;
}
public Deque() {
first = null;
last = null;
N= 0;
assert check();}
public boolean isEmpty( )
{return first == null;}
public int size()
{return N; }
public void addFirst(Item item) {
if (item == null) throw new NullPointerException("Can't add null value");
Node<Item> oldfirst = first;
first = new Node<Item>();
first.item = item;
N++;
if (N==1) {last = first;
}
else {first.next= oldfirst;
first.previous = null;
oldfirst.previous = first;}
}
public void addLast(Item item) {
if (item == null) throw new NullPointerException("Can't add null value");
Node<Item> oldlast = last;
last = new Node<Item>();
last.item = item;
last.next=null;
if (isEmpty()) first = last;
else
{oldlast.next= last;
last.previous = oldlast;}
N++;
}
public Item removeFirst() {
if (isEmpty()) throw new NoSuchElementException("Stack underflow");
Item f;
f = first.item;
if (first.next == null) { last = null; };// save item to return
first = first.next; // delete first node
N--;
assert check();
return f;
}
public Item removeLast() {
if (isEmpty()) throw new NoSuchElementException("Stack underflow");
int a = 1;
Item f;
f = last.item;
if (N!=1) {
last =last.previous;
last.next =null;
}
else if (N == 1){
first = last =null;
}
N--;
return f ;
}
private boolean check() {
if (N == 0) {
if (first != null) return false;
}
else if (N == 1) {
if (first == null) return false;
if (first.next != null) return false;
}
else {
if (first.next == null) return false;
}
int numberOfNodes = 0;
for (Node<Item> x = first; x != null; x = x.next) {
numberOfNodes++;
}
if (numberOfNodes != N) return false;
return true;
}
public Iterator<Item> iterator() {return new ListIterator();}
private class ListIterator implements Iterator<Item> {
private Node<Item> current = first;
public boolean hasNext() { return current != null; }
public void remove() { throw new UnsupportedOperationException(); }
public Item next() {
if (!hasNext()) throw new NoSuchElementException();
item = current.item;
current = current.next;
return item;
}
}
public static void main(String[] args) {
Deque<Integer> d = new Deque<Integer>();
//System.out.println(d.size());
d.addFirst(1);
System.out.println(d.size());
d.addLast(3);
System.out.println(d.size());
// d.addFirst(1);
// System.out.println(d.size());
}
//java.lang.NullPointerException
}