Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Snowleopard_wu
Java
提交
f82bf1a1
J
Java
项目概览
Snowleopard_wu
/
Java
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
Java
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
f82bf1a1
编写于
4月 28, 2020
作者:
M
MohamedBechir
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add singly linked list
上级
9086252d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
162 addition
and
0 deletion
+162
-0
src/main/java/com/dataStructures/SinglyLinkedList.java
src/main/java/com/dataStructures/SinglyLinkedList.java
+102
-0
src/test/java/com/dataStructures/SinglyLinkedListTest.java
src/test/java/com/dataStructures/SinglyLinkedListTest.java
+60
-0
未找到文件。
src/main/java/com/dataStructures/SinglyLinkedList.java
0 → 100644
浏览文件 @
f82bf1a1
package
com.dataStructures
;
public
class
SinglyLinkedList
<
T
>
{
/**
* Define a node in the singly linked list
*/
private
static
class
Node
<
T
>
{
T
data
;
Node
<
T
>
next
;
Node
(
T
data
)
{
this
.
data
=
data
;
}
}
/**
* Reference to the first node of the singly linked list
*/
private
Node
<
T
>
head
;
/**
* Method to return the size of the singly linked list
*/
public
int
size
()
{
int
size
=
0
;
Node
<
T
>
node
=
head
;
while
(
node
!=
null
)
{
++
size
;
node
=
node
.
next
;
}
return
size
;
}
/**
* Method to add a node at the end of the singly linked list
*/
public
void
add
(
T
data
)
{
Node
<
T
>
newNode
=
new
Node
<>(
data
);
if
(
head
==
null
)
{
head
=
newNode
;
}
else
{
Node
<
T
>
last
=
head
;
while
(
last
.
next
!=
null
)
{
last
=
last
.
next
;
}
last
.
next
=
newNode
;
}
}
/**
* Method to add a node at the beginning of the singly linked list
*/
public
void
insertAtStart
(
T
data
)
{
Node
<
T
>
node
=
new
Node
<>(
data
);
node
.
data
=
data
;
node
.
next
=
null
;
node
.
next
=
head
;
head
=
node
;
}
/**
* Method to remove a node at any position in the singly linked list
*/
public
void
remove
(
int
position
)
{
Node
<
T
>
prev
=
null
,
node
=
head
;
while
(
position
>
0
&&
node
!=
null
)
{
--
position
;
prev
=
node
;
node
=
node
.
next
;
}
if
(
node
!=
null
)
{
if
(
prev
==
null
)
{
head
=
node
.
next
;
}
else
{
prev
.
next
=
node
.
next
;
}
}
}
/**
* Method to print the content of the singly linked list for better
* understanding
*/
public
void
printContent
()
{
StringBuilder
sb
=
new
StringBuilder
();
Node
<
T
>
node
=
head
;
while
(
node
!=
null
)
{
sb
.
append
(
node
.
data
).
append
(
'-'
).
append
(
'>'
);
node
=
node
.
next
;
}
int
lastComma
=
sb
.
lastIndexOf
(
","
);
if
(
lastComma
!=
-
1
)
{
sb
.
deleteCharAt
(
lastComma
);
}
System
.
out
.
println
(
sb
);
}
}
\ No newline at end of file
src/test/java/com/dataStructures/SinglyLinkedListTest.java
0 → 100644
浏览文件 @
f82bf1a1
package
com.dataStructures
;
import
org.junit.Test
;
import
org.junit.jupiter.api.Assertions
;
public
class
SinglyLinkedListTest
{
private
SinglyLinkedList
<
Integer
>
MySinglyLinkedList
=
new
SinglyLinkedList
<>();
@Test
public
void
emptyTest
()
{
Assertions
.
assertEquals
(
0
,
MySinglyLinkedList
.
size
());
MySinglyLinkedList
.
printContent
();
}
@Test
public
void
addTest
()
{
MySinglyLinkedList
.
add
(
1
);
Assertions
.
assertEquals
(
1
,
MySinglyLinkedList
.
size
());
MySinglyLinkedList
.
printContent
();
MySinglyLinkedList
.
add
(
42
);
Assertions
.
assertEquals
(
2
,
MySinglyLinkedList
.
size
());
MySinglyLinkedList
.
printContent
();
MySinglyLinkedList
.
add
(
2
);
MySinglyLinkedList
.
add
(
3
);
Assertions
.
assertEquals
(
4
,
MySinglyLinkedList
.
size
());
MySinglyLinkedList
.
printContent
();
}
@Test
public
void
removeTest
()
{
MySinglyLinkedList
.
add
(
1
);
MySinglyLinkedList
.
add
(
2
);
MySinglyLinkedList
.
add
(
3
);
MySinglyLinkedList
.
add
(
4
);
MySinglyLinkedList
.
printContent
();
MySinglyLinkedList
.
remove
(
3
);
Assertions
.
assertEquals
(
3
,
MySinglyLinkedList
.
size
());
MySinglyLinkedList
.
printContent
();
MySinglyLinkedList
.
remove
(
0
);
Assertions
.
assertEquals
(
2
,
MySinglyLinkedList
.
size
());
MySinglyLinkedList
.
printContent
();
MySinglyLinkedList
.
remove
(
0
);
Assertions
.
assertEquals
(
1
,
MySinglyLinkedList
.
size
());
MySinglyLinkedList
.
printContent
();
MySinglyLinkedList
.
remove
(
0
);
Assertions
.
assertEquals
(
0
,
MySinglyLinkedList
.
size
());
MySinglyLinkedList
.
printContent
();
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录