Skip to content

Commit 116ee08

Browse files
committed
Count size of the list
1 parent 0a83068 commit 116ee08

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

DataStructures/courses/LoianeGroner/datastructure-algorithms-java/src/com/loiane/datastructure/list/LinkedList.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@
33
public class LinkedList<T> {
44

55
private Node<T> begin;
6+
private int size = 0;
67

7-
public void add(T element) {
8+
public void addFirst(T element) {
89
Node<T> cell = new Node<T>(element);
910
this.begin = cell;
11+
this.size++;
1012
}
1113

14+
public void addSequence(T element) {
15+
Node<T> cell = new Node<T>(element, begin);
16+
this.begin = cell;
17+
this.size++;
18+
}
1219

1320
@Override
1421
public String toString() {
@@ -17,4 +24,8 @@ public String toString() {
1724
return builder.toString();
1825
}
1926

27+
public int getSize() {
28+
return this.size;
29+
}
30+
2031
}

DataStructures/courses/LoianeGroner/datastructure-algorithms-java/src/com/loiane/datastructure/list/test/LinkedListTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ public class LinkedListTest {
77
public static void main(String[] args) {
88

99
LinkedList<Integer> list = new LinkedList<>();
10-
list.add(1);
10+
list.addFirst(1);
11+
list.addSequence(2);
12+
list.addSequence(3);
1113

14+
System.out.println("Size: "+list.getSize());
1215
System.out.println(list);
1316
}
1417
}

0 commit comments

Comments
 (0)