-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenericList.java
More file actions
165 lines (135 loc) · 3.21 KB
/
genericList.java
File metadata and controls
165 lines (135 loc) · 3.21 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.util.Iterator;
public class GenericList <iType> implements Iterable<iType>
{
private class Node
{
iType val; // reference -- same as pointer
Node next;
Node prev;
public Node(iType v) //constructor
{
val = v;
next = null;
prev = null;
}
}
private Node head; // instance variable
private Node tail;
private int curr_size;
public GenericList()
{
head = null;
tail = null;
}
public void print() {
Node walker = head;
StdOut.print("head ---> ");
print(head); // calling our recursive function
/* while (walker != null)
{
StdOut.printf("%d ---> ", walker.val);
walker = walker.next;
}
*/
StdOut.print("null\n");
}
private void print(Node n) // overloaded
{
if(n == null)
{
return;
}
// recursive print function
StdOut.print(n.val);
StdOut.print(" ---> ");
print(n.next);
}
public int getSize()
{
return curr_size;
}
public void insertFront(iType val) {
Node n = new Node(val);
if (head != null) {
n.next = head;
}
else
{
tail = n; // head an tail point to same if list empty
}
head = n;
}
public void insertBack(iType val)
{
Node n = new Node(val);
if(head == null) // list only has one element
{
head = n;
tail = head;
}
else
{
tail.next = n;
tail = n;
}
}
public boolean inList(iType val)
{
Node walker = head;
while(walker != null)
{
if(walker.val == val)
{
return true;
}
else
{
walker = walker.next;
}
}
return false;
}
public Iterator<iType> iterator() // iterator = interface to provide access to private items but not allow modification
{
return new GenericListIterator();
}
private class GenericListIterator implements Iterator<iType>
{
private Node curr = head;
public boolean hasNext()
{
return curr != null;
}
public void remove()
{
throw new UnsupportedOperationException();
}
public iType next()
{
iType val = curr.val;
curr = curr.next;
return val;
}
}
public static void main(String[] args)
{
GenericList<Integer> ll = new GenericList<Integer>(); // Integer, String, etc = wrapper
/* while (StdIn.isEmpty() == false)
{
ll.insertFront(StdIn.readString());
ll.print();
if (ll.inList("20")) {
StdOut.print("found it!\n");
}
}
*/
for (int i = 0; i < 25; i++)
{
ll.insertFront(i);
}
for (Integer i : ll) // Syntatic Sugar for (Iterator i = ll.iterator(); i.hasNext() )
{
StdOut.println(i);
}
}
}