Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf82bf1a

Browse files
committed
Add singly linked list
1 parent9086252 commitf82bf1a

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
packagecom.dataStructures;
2+
3+
4+
5+
6+
publicclassSinglyLinkedList<T> {
7+
8+
/**
9+
* Define a node in the singly linked list
10+
*/
11+
privatestaticclassNode<T> {
12+
Tdata;
13+
Node<T>next;
14+
15+
Node(Tdata) {
16+
this.data =data;
17+
}
18+
}
19+
20+
/**
21+
* Reference to the first node of the singly linked list
22+
*/
23+
privateNode<T>head;
24+
25+
/**
26+
* Method to return the size of the singly linked list
27+
*/
28+
publicintsize() {
29+
intsize =0;
30+
Node<T>node =head;
31+
while (node !=null) {
32+
++size;
33+
node =node.next;
34+
}
35+
returnsize;
36+
}
37+
38+
/**
39+
* Method to add a node at the end of the singly linked list
40+
*/
41+
publicvoidadd(Tdata) {
42+
Node<T>newNode =newNode<>(data);
43+
if (head ==null) {
44+
head =newNode;
45+
}else {
46+
Node<T>last =head;
47+
while (last.next !=null) {
48+
last =last.next;
49+
}
50+
last.next =newNode;
51+
}
52+
}
53+
54+
/**
55+
* Method to add a node at the beginning of the singly linked list
56+
*/
57+
publicvoidinsertAtStart (Tdata) {
58+
Node<T>node =newNode<>(data);
59+
node.data =data;
60+
node.next =null;
61+
node.next =head;
62+
head =node;
63+
64+
}
65+
66+
/**
67+
* Method to remove a node at any position in the singly linked list
68+
*/
69+
publicvoidremove(intposition) {
70+
Node<T>prev =null,node =head;
71+
while (position >0 &&node !=null) {
72+
--position;
73+
prev =node;
74+
node =node.next;
75+
}
76+
if (node !=null) {
77+
if (prev ==null) {
78+
head =node.next;
79+
}else {
80+
prev.next =node.next;
81+
}
82+
}
83+
}
84+
85+
/**
86+
* Method to print the content of the singly linked list for better
87+
* understanding
88+
*/
89+
publicvoidprintContent() {
90+
StringBuildersb =newStringBuilder();
91+
Node<T>node =head;
92+
while (node !=null) {
93+
sb.append(node.data).append('-').append('>');
94+
node =node.next;
95+
}
96+
intlastComma =sb.lastIndexOf(",");
97+
if (lastComma != -1) {
98+
sb.deleteCharAt(lastComma);
99+
}
100+
System.out.println(sb);
101+
}
102+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
packagecom.dataStructures;
2+
3+
importorg.junit.Test;
4+
importorg.junit.jupiter.api.Assertions;
5+
6+
7+
publicclassSinglyLinkedListTest {
8+
9+
privateSinglyLinkedList<Integer>MySinglyLinkedList =newSinglyLinkedList<>();
10+
11+
@Test
12+
publicvoidemptyTest() {
13+
Assertions.assertEquals(0,MySinglyLinkedList.size());
14+
MySinglyLinkedList.printContent();
15+
}
16+
17+
@Test
18+
publicvoidaddTest() {
19+
MySinglyLinkedList.add(1);
20+
Assertions.assertEquals(1,MySinglyLinkedList.size());
21+
MySinglyLinkedList.printContent();
22+
23+
MySinglyLinkedList.add(42);
24+
Assertions.assertEquals(2,MySinglyLinkedList.size());
25+
MySinglyLinkedList.printContent();
26+
27+
MySinglyLinkedList.add(2);
28+
MySinglyLinkedList.add(3);
29+
Assertions.assertEquals(4,MySinglyLinkedList.size());
30+
MySinglyLinkedList.printContent();
31+
}
32+
33+
@Test
34+
publicvoidremoveTest() {
35+
MySinglyLinkedList.add(1);
36+
MySinglyLinkedList.add(2);
37+
MySinglyLinkedList.add(3);
38+
MySinglyLinkedList.add(4);
39+
MySinglyLinkedList.printContent();
40+
41+
MySinglyLinkedList.remove(3);
42+
Assertions.assertEquals(3,MySinglyLinkedList.size());
43+
MySinglyLinkedList.printContent();
44+
45+
MySinglyLinkedList.remove(0);
46+
Assertions.assertEquals(2,MySinglyLinkedList.size());
47+
MySinglyLinkedList.printContent();
48+
49+
MySinglyLinkedList.remove(0);
50+
Assertions.assertEquals(1,MySinglyLinkedList.size());
51+
MySinglyLinkedList.printContent();
52+
53+
MySinglyLinkedList.remove(0);
54+
Assertions.assertEquals(0,MySinglyLinkedList.size());
55+
MySinglyLinkedList.printContent();
56+
}
57+
58+
59+
60+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp