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

Commit c740825

Browse files
Add CustomLinkedList as a linked list
1 parent 0403c52 commit c740825

File tree

5 files changed

+252
-0
lines changed

5 files changed

+252
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package br.com.zevolution.datastructure.linkedlist;
2+
3+
import java.util.NoSuchElementException;
4+
5+
public class CustomLinkedList {
6+
7+
private Node first = null;
8+
private Node last = null;
9+
private int totalElements = 0;
10+
11+
public void addFirst(Object element) {
12+
Node node = new Node(element, this.first);
13+
this.first = node;
14+
15+
if (this.totalElements == 0) {
16+
this.last = this.first;
17+
}
18+
19+
this.totalElements++;
20+
}
21+
22+
public void add(Object element) {
23+
if (this.totalElements == 0) {
24+
this.addFirst(element);
25+
} else {
26+
Node node = new Node(element, null);
27+
this.last.setNext(node);
28+
this.last = node;
29+
this.totalElements++;
30+
}
31+
}
32+
33+
public void add(int position, Object element) {
34+
if (!this.isValidContentPosition(position))
35+
throw new UnoccupiedPosition();
36+
37+
if (position == 0) {
38+
this.addFirst(element);
39+
} else if (position == this.totalElements) {
40+
this.add(element);
41+
} else {
42+
Node previousNode = this.getNode(position - 1);
43+
Node node = new Node(element, previousNode.getNext());
44+
previousNode.setNext(node);
45+
this.totalElements++;
46+
}
47+
}
48+
49+
public void removeFirst() {
50+
if (this.totalElements == 0)
51+
throw new NoSuchElementException();
52+
53+
this.first = this.first.getNext();
54+
this.totalElements--;
55+
56+
if (this.totalElements == 0) {
57+
this.last = null;
58+
}
59+
}
60+
61+
public Object get(int position) {
62+
return this.getNode(position).getElement();
63+
}
64+
65+
public int size() {
66+
return this.totalElements;
67+
}
68+
69+
@Override
70+
public String toString() {
71+
if (this.totalElements == 0) return "[]";
72+
73+
Node node = this.first;
74+
75+
StringBuilder builder = new StringBuilder("[");
76+
77+
for (int i = 0; i < this.totalElements -1; i++) {
78+
builder.append(node.getElement()).append(", ");
79+
node = node.getNext();
80+
}
81+
82+
builder.append(node.getElement()).append("]");
83+
84+
return builder.toString();
85+
}
86+
87+
private boolean isValidContentPosition(int position) {
88+
return position >= 0 && position < this.totalElements;
89+
}
90+
91+
private Node getNode(int position) {
92+
if (!this.isValidContentPosition(position))
93+
throw new UnoccupiedPosition();
94+
95+
Node node = this.first;
96+
for (int i = 0; i < position; i++) {
97+
node = node.getNext();
98+
}
99+
return node;
100+
}
101+
102+
103+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package br.com.zevolution.datastructure.linkedlist;
2+
3+
public class InvalidPosition extends RuntimeException {
4+
5+
private static final long serialVersionUID = -3536404610449816152L;
6+
7+
public InvalidPosition() {
8+
super("Invalid position");
9+
}
10+
11+
public InvalidPosition(String message) {
12+
super(message);
13+
}
14+
15+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package br.com.zevolution.datastructure.linkedlist;
2+
3+
public class Node {
4+
5+
private Node next;
6+
private Object element;
7+
8+
public Node(Object element, Node next) {
9+
this.element = element;
10+
this.next = next;
11+
}
12+
13+
public Node getNext() {
14+
return next;
15+
}
16+
17+
public void setNext(Node next) {
18+
this.next = next;
19+
}
20+
21+
public Object getElement() {
22+
return element;
23+
}
24+
25+
26+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package br.com.zevolution.datastructure.linkedlist;
2+
3+
public class UnoccupiedPosition extends InvalidPosition {
4+
5+
private static final long serialVersionUID = -2300538444121627252L;
6+
7+
public UnoccupiedPosition() {
8+
super("There is no value in this position");
9+
}
10+
11+
12+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package br.com.zevolution.datastructure.linkedlist;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.NoSuchElementException;
6+
7+
import org.junit.Test;
8+
9+
public class CustomLinkedListTest {
10+
11+
@Test
12+
public void should_Contain_OneRecordInCustomLinkedList() {
13+
CustomLinkedList linkedList = new CustomLinkedList();
14+
linkedList.add("José Lucas");
15+
16+
assertEquals(1, linkedList.size());
17+
}
18+
19+
@Test
20+
public void should_Contain_TwoRecordsInCustomLinkedList() {
21+
CustomLinkedList linkedList = new CustomLinkedList();
22+
linkedList.add("Beatriz");
23+
linkedList.add("José Lucas");
24+
25+
assertEquals(2, linkedList.size());
26+
}
27+
28+
@Test
29+
public void should_MakeSure_ObjectPosition() {
30+
CustomLinkedList linkedList = new CustomLinkedList();
31+
linkedList.add("Beatriz");
32+
linkedList.add("José Lucas");
33+
linkedList.add(0, "Laura");
34+
35+
assertEquals("Laura", linkedList.get(0));
36+
assertEquals("Beatriz", linkedList.get(1));
37+
assertEquals("José Lucas", linkedList.get(2));
38+
}
39+
40+
@Test
41+
public void should_MakeSure_AddFirst_ObjectPosition() {
42+
CustomLinkedList linkedList = new CustomLinkedList();
43+
linkedList.add("Beatriz");
44+
linkedList.add("José Lucas");
45+
linkedList.addFirst("Laura");
46+
47+
assertEquals("Laura", linkedList.get(0));
48+
assertEquals("Beatriz", linkedList.get(1));
49+
assertEquals("José Lucas", linkedList.get(2));
50+
}
51+
52+
@Test
53+
public void should_MakeSure_RemoveFirst_ObjectPosition() {
54+
CustomLinkedList linkedList = new CustomLinkedList();
55+
linkedList.add("Beatriz");
56+
linkedList.add("Laura");
57+
linkedList.addFirst("José Lucas");
58+
59+
linkedList.removeFirst();
60+
61+
assertNotEquals("José Lucas", linkedList.get(0));
62+
assertEquals("Beatriz", linkedList.get(0));
63+
assertEquals(2, linkedList.size());
64+
}
65+
66+
@Test
67+
public void should_Be_Empty() {
68+
CustomLinkedList linkedList = new CustomLinkedList();
69+
linkedList.add("Beatriz");
70+
linkedList.add("José Lucas");
71+
linkedList.add("Laura");
72+
73+
for (int i = linkedList.size(); i > 0; i--) {
74+
linkedList.removeFirst();
75+
}
76+
77+
assertEquals(0, linkedList.size());
78+
}
79+
80+
81+
@Test(expected = UnoccupiedPosition.class)
82+
public void should_ThrowException_When_UnoccupiedPosition() {
83+
CustomLinkedList linkedList = new CustomLinkedList();
84+
linkedList.add("zevolution");
85+
86+
linkedList.add(1, "noitulovez");;
87+
}
88+
89+
@Test(expected = NoSuchElementException.class)
90+
public void should_ThrowException_When_NoSuchElement() {
91+
CustomLinkedList linkedList = new CustomLinkedList();
92+
93+
linkedList.removeFirst();
94+
}
95+
96+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /