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
|
public class RecursionLinkedList
{
private Node firstNode;
private int numberOfEntries;
public RecursionLinkedList()
{
firstNode = null;
numberOfEntries = 0;
}
public void add(int aData)
{
if(numberOfEntries == 0)
{
firstNode = new Node(aData);
}
else
{
firstNode = new Node(aData, firstNode);
}
numberOfEntries++;
}
/**
* boolean contains(int aData)
*
* See whether this RecursionLinkedList contains aData
* @param aData a data to be located
* @return true if this RecursionLinkedList contains aData,
* or false otherwise.
*/
public boolean contains(int aData)
{
return containsHelper(firstNode,aData);
}
private boolean containsHelper(Node thisnode, int aData)
{
if(thisnode == null)
{
return false;
}
else if(thisnode.data == aData)
{
return true;
}
else
{
return containsHelper(thisnode.next, aData);
}
}
/**
* int getFrequencyOf(int aData)
*
* Counts the number of times a given data appears in this
* RecursionLinkedList.
*
* @param aData the data to be counted
* @return the number of times aData appears in this RecursionLinkedList
*/
public int getFrequencyOf(int aData)
{
return getFrequencyOfHelper(firstNode,aData);
}
private int getFrequencyOfHelper(Node thisnode, int aData)
{
if(thisnode == null)
{
return 0;
}
else if(thisnode.data == aData)
{
return getFrequencyOfHelper(thisnode.next, aData) + 1;
}
else
{
return getFrequencyOfHelper(thisnode.next, aData);
}
}
/**
* String toString()
*
* Return a string representation of this RecursionLinkedList. For example,
* if this RecursionLinkedList contains 1, 2, 3, 5, 2 and 3 from the first
* index to the last index, the returned string should be
* "[1,2,3,5,2,3]"
* @return the string representation of this RecursionLinkedList.
*/
public String toString()
{
return "[" + firstNode.data + toStringHelper(firstNode.next) + "]";
}
private String toStringHelper(Node thisnode)
{
if(thisnode == null)
{
return "";
}
else
{
return "," + thisnode.data + toStringHelper(thisnode.next);
}
}
/**
* int getIndexOf(int aData)
*
* Return the index of the first aData where the first index of
* the first item in this RecursionLinkedList is 0.
*
* @param aData the data to be located
* @return the index of the first aData.
*/
public int getIndexOf(int aData)
{
int p = getIndexOfHelper(firstNode,aData);
/*
if(p==numberOfEntries+1)
{
return -1;
}
else
{
return p;
}
*/
return p==numberOfEntries+1?-1:p;
}
private int getIndexOfHelper(Node thisnode, int aData)
{
if(thisnode == null)
{
return 1;
}
else if(thisnode.data == aData)
{
return 0;
}
else
{
return getIndexOfHelper(thisnode.next,aData) + 1;
}
}
private class Node
{
private int data;
private Node next;
private Node(int aData, Node nextNode)
{
data = aData;
next = nextNode;
}
private Node(int aData)
{
this(aData, null);
}
}
}
|