Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
cs2-23wi
lectures
Commits
152e3895
Commit
152e3895
authored
2 years ago
by
Adam Blank
Browse files
Options
Download
Email Patches
Plain Diff
after lecture06
parent
42a06346
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
06/ICollection.java
+22
-0
06/ICollection.java
06/LinkedList.java
+73
-0
06/LinkedList.java
with
95 additions
and
0 deletions
+95
-0
06/ICollection.java
0 → 100644
View file @
152e3895
import
java.util.Iterator
;
public
interface
ICollection
<
E
>
extends
Iterable
<
E
>
{
public
void
add
(
E
e
);
public
void
clear
();
public
Iterator
<
E
>
iterator
();
default
public
boolean
contains
(
E
e
)
{
for
(
E
item
:
this
)
{
if
(
e
==
null
?
item
==
null
:
e
.
equals
(
item
))
{
return
true
;
}
}
return
false
;
}
public
int
size
();
default
public
boolean
isEmpty
()
{
return
this
.
size
()
==
0
;
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
06/LinkedList.java
0 → 100644
View file @
152e3895
public
class
LinkedList
<
E
>
implements
IList
<
E
>
{
private
Node
<
E
>
head
;
private
int
size
;
private
static
class
Node
<
E
>
{
public
final
E
data
;
public
Node
<
E
>
next
;
public
Node
(
E
data
)
{
this
(
data
,
null
);
}
public
Node
(
E
data
,
Node
<
E
>
next
)
{
this
.
data
=
data
;
this
.
next
=
next
;
}
}
@Override
public
int
size
()
{
return
this
.
size
;
}
@Override
public
void
set
(
int
idx
,
E
elem
)
{
if
(
idx
>=
this
.
size
||
idx
<
0
)
{
throw
new
IllegalArgumentException
();
}
}
@Override
public
E
get
(
int
idx
)
{
return
null
;
}
@Override
public
void
clear
()
{
this
.
head
=
null
;
this
.
size
=
0
;
}
@Override
public
void
add
(
int
idx
,
E
elem
)
{
if
(
idx
>
this
.
size
||
idx
<
0
)
{
throw
new
IllegalArgumentException
();
}
}
@Override
public
void
add
(
E
elem
)
{
if
(
this
.
head
==
null
)
{
this
.
head
=
new
Node
<>(
elem
);
}
else
{
// Write only the else in C16
// I AM ABOUT TO WRITE CODE THAT DOESN'T WORK
// BEWARE!!
/*
Node<E> curr = this.head;
while (curr.next != null) {
curr = curr.next;
}
curr.next = new Node<>(elem);
*/
Node
<
E
>
curr
;
for
(
curr
=
this
.
head
;
curr
.
next
!=
null
;
curr
=
curr
.
next
)
{
}
}
this
.
size
++;
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment