Parcourir la source

Test work, except find() and get() bc add is copying first value in multiple times

Nathan Hall il y a 6 ans
Parent
révision
a2209e5064

+ 2
- 1
.idea/compiler.xml Voir le fichier

@@ -7,10 +7,11 @@
7 7
         <sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
8 8
         <outputRelativeToContentRoot value="true" />
9 9
         <module name="CR-MesoLabs-Collections-SinglyLinkedList" />
10
+        <module name="singlylinkedlist" />
10 11
       </profile>
11 12
     </annotationProcessing>
12 13
     <bytecodeTargetLevel target="1.8">
13
-      <module name="CR-MesoLabs-Collections-SinglyLinkedList" target="1.5" />
14
+      <module name="singlylinkedlist" target="8" />
14 15
     </bytecodeTargetLevel>
15 16
   </component>
16 17
 </project>

+ 13
- 0
.idea/libraries/Maven__junit_junit_4_12.xml Voir le fichier

@@ -0,0 +1,13 @@
1
+<component name="libraryTable">
2
+  <library name="Maven: junit:junit:4.12">
3
+    <CLASSES>
4
+      <root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
5
+    </CLASSES>
6
+    <JAVADOC>
7
+      <root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12-javadoc.jar!/" />
8
+    </JAVADOC>
9
+    <SOURCES>
10
+      <root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12-sources.jar!/" />
11
+    </SOURCES>
12
+  </library>
13
+</component>

+ 13
- 0
.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml Voir le fichier

@@ -0,0 +1,13 @@
1
+<component name="libraryTable">
2
+  <library name="Maven: org.hamcrest:hamcrest-core:1.3">
3
+    <CLASSES>
4
+      <root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
5
+    </CLASSES>
6
+    <JAVADOC>
7
+      <root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3-javadoc.jar!/" />
8
+    </JAVADOC>
9
+    <SOURCES>
10
+      <root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3-sources.jar!/" />
11
+    </SOURCES>
12
+  </library>
13
+</component>

+ 0
- 61
.idea/misc.xml Voir le fichier

@@ -26,65 +26,4 @@
26 26
   <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
27 27
     <output url="file://$PROJECT_DIR$/out" />
28 28
   </component>
29
-  <component name="masterDetails">
30
-    <states>
31
-      <state key="GlobalLibrariesConfigurable.UI">
32
-        <settings>
33
-          <splitter-proportions>
34
-            <option name="proportions">
35
-              <list>
36
-                <option value="0.2" />
37
-              </list>
38
-            </option>
39
-          </splitter-proportions>
40
-        </settings>
41
-      </state>
42
-      <state key="JdkListConfigurable.UI">
43
-        <settings>
44
-          <last-edited>1.8</last-edited>
45
-          <splitter-proportions>
46
-            <option name="proportions">
47
-              <list>
48
-                <option value="0.2" />
49
-              </list>
50
-            </option>
51
-          </splitter-proportions>
52
-        </settings>
53
-      </state>
54
-      <state key="ProjectJDKs.UI">
55
-        <settings>
56
-          <last-edited>1.8</last-edited>
57
-          <splitter-proportions>
58
-            <option name="proportions">
59
-              <list>
60
-                <option value="0.2" />
61
-              </list>
62
-            </option>
63
-          </splitter-proportions>
64
-        </settings>
65
-      </state>
66
-      <state key="ProjectLibrariesConfigurable.UI">
67
-        <settings>
68
-          <splitter-proportions>
69
-            <option name="proportions">
70
-              <list>
71
-                <option value="0.2" />
72
-              </list>
73
-            </option>
74
-          </splitter-proportions>
75
-        </settings>
76
-      </state>
77
-      <state key="ScopeChooserConfigurable.UI">
78
-        <settings>
79
-          <splitter-proportions>
80
-            <option name="proportions">
81
-              <list>
82
-                <option value="0.2" />
83
-              </list>
84
-            </option>
85
-          </splitter-proportions>
86
-        </settings>
87
-      </state>
88
-    </states>
89
-  </component>
90 29
 </project>

+ 1
- 0
.idea/modules.xml Voir le fichier

@@ -3,6 +3,7 @@
3 3
   <component name="ProjectModuleManager">
4 4
     <modules>
5 5
       <module fileurl="file://$PROJECT_DIR$/CR-MesoLabs-Collections-SinglyLinkedList.iml" filepath="$PROJECT_DIR$/CR-MesoLabs-Collections-SinglyLinkedList.iml" />
6
+      <module fileurl="file://$PROJECT_DIR$/singlylinkedlist.iml" filepath="$PROJECT_DIR$/singlylinkedlist.iml" />
6 7
     </modules>
7 8
   </component>
8 9
 </project>

+ 535
- 399
.idea/workspace.xml
Fichier diff supprimé car celui-ci est trop grand
Voir le fichier


+ 8
- 0
pom.xml Voir le fichier

@@ -7,6 +7,14 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>singlylinkedlist</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <dependencies>
11
+        <dependency>
12
+            <groupId>junit</groupId>
13
+            <artifactId>junit</artifactId>
14
+            <version>RELEASE</version>
15
+            <scope>test</scope>
16
+        </dependency>
17
+    </dependencies>
10 18
 
11 19
 
12 20
 </project>

+ 153
- 1
src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java Voir le fichier

@@ -3,5 +3,157 @@ package com.zipcodewilmington.singlylinkedlist;
3 3
 /**
4 4
  * Created by leon on 1/10/18.
5 5
  */
6
-public class SinglyLinkedList {
6
+public class SinglyLinkedList<N> {
7
+    private Node firstNode;
8
+    private Node lastNode;
9
+    private int nodeCount;
10
+
11
+    public SinglyLinkedList() {
12
+        firstNode = null;
13
+        lastNode = null;
14
+    }
15
+
16
+    public int size() {
17
+        return nodeCount;
18
+    }
19
+
20
+    public void add(N item) {
21
+
22
+        if (firstNode == null) {
23
+            firstNode = new Node(item);
24
+            System.out.println(firstNode.getNodeItem());
25
+        }
26
+
27
+        while (firstNode.getNextNode() != null) {
28
+            Node newLastNode = new Node(item);
29
+            lastNode.setNextNode(newLastNode);
30
+            lastNode = newLastNode;
31
+            System.out.println(newLastNode.nodeItem);
32
+
33
+        }
34
+
35
+        nodeCount++;
36
+
37
+    }
38
+
39
+    public void insert(N item, int position) {
40
+        if (size() < position) {
41
+            throw new IllegalStateException("LinkedList is smaller than position.");
42
+        }
43
+
44
+        Node currentNode = firstNode;
45
+
46
+        for (int i = 1; i < position && currentNode != null; i++) {
47
+            currentNode = currentNode.getNextNode();
48
+        }
49
+
50
+        Node newNode = new Node(item);
51
+        Node nextNode = currentNode.getNextNode();
52
+        currentNode.setNextNode(newNode);
53
+        newNode.setNextNode(nextNode);
54
+        nodeCount++;
55
+
56
+
57
+    }
58
+
59
+    public void remove(N item) {
60
+        if (firstNode == null) {
61
+            throw new IllegalStateException("Linked List is empty!");
62
+        }
63
+
64
+        N nodeItem = firstNode.getNodeItem();
65
+
66
+        firstNode = firstNode.getNextNode();
67
+        nodeCount--;
68
+
69
+    }
70
+
71
+
72
+    public boolean contains(N item) {
73
+
74
+        if (firstNode.getNodeItem().equals(item)) {
75
+            return true;
76
+        } else {
77
+            Node currentNode = firstNode;
78
+            for (int i = 1; i < nodeCount; i++) {
79
+                currentNode = currentNode.getNextNode();
80
+                if (currentNode.getNodeItem().equals(item)) {
81
+                    return true;
82
+                }
83
+            }
84
+        }
85
+        return false;
86
+    }
87
+
88
+    public Integer find(N item) {
89
+        if (firstNode.getNodeItem().equals(item)) {
90
+            return (Integer) firstNode.nodeItem;
91
+        }
92
+
93
+        System.out.println(firstNode.nextNode);
94
+
95
+        while (firstNode.nextNode != null) {
96
+            Node currentNode = firstNode;
97
+
98
+//            System.out.println(currentNode.nodeItem);
99
+
100
+            currentNode = currentNode.getNextNode();
101
+            if (currentNode.getNodeItem().equals(item)) {
102
+                return (Integer) currentNode.nodeItem;
103
+            }
104
+            System.out.println("Current Item: " + currentNode.getNodeItem());
105
+            nodeCount++;
106
+        }
107
+
108
+
109
+        return -1;
110
+    }
111
+
112
+    public Integer get(N num) {
113
+        int count = 0;
114
+
115
+        if (firstNode.nextNode == null){
116
+            System.out.println(("There is only 1 number"));
117
+            return count;
118
+        }
119
+
120
+        count = 1;
121
+        while (firstNode.nextNode != null){
122
+            System.out.println(("There is more than 1 number"));
123
+
124
+            Node currentNode = firstNode.nextNode;
125
+            if (currentNode.getNodeItem() == num){
126
+                return count;
127
+            }
128
+            count++;
129
+
130
+        }
131
+        return null;
132
+    }
133
+
134
+
135
+    private class Node {
136
+        private Node nextNode;
137
+        private N nodeItem;
138
+
139
+        private Node(N item) {
140
+            this.nextNode = nextNode;
141
+            this.nodeItem = item;
142
+        }
143
+
144
+
145
+        public Node getNextNode() {
146
+            return nextNode;
147
+        }
148
+
149
+        public void setNextNode(Node nextNode) {
150
+            this.nextNode = nextNode;
151
+        }
152
+
153
+        public N getNodeItem() {
154
+            return nodeItem;
155
+        }
156
+
157
+    }
158
+
7 159
 }

+ 99
- 1
src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java Voir le fichier

@@ -1,7 +1,105 @@
1 1
 package com.zipcodewilmington.singlylinkedlist;
2 2
 
3
+
4
+import org.junit.Assert;
5
+import org.junit.Test;
6
+
3 7
 /**
4 8
  * Created by leon on 1/10/18.
5 9
  */
6
-public class SinglyLinkedListTest {
10
+public class SinglyLinkedListTest<N> {
11
+
12
+    public Integer num = 8;
13
+    public Integer num1 = 5;
14
+    SinglyLinkedList<Integer> sll = new SinglyLinkedList<>();
15
+
16
+
17
+    @Test
18
+    public void addTest() {
19
+        sll.add(num);
20
+
21
+
22
+        int expected = 1;
23
+        int actual = sll.size();
24
+
25
+        Assert.assertEquals(expected, actual);
26
+    }
27
+    @Test
28
+    public void add3Test() {
29
+        sll.add(num);
30
+        sll.add(num1);
31
+        sll.add(5);
32
+
33
+
34
+        int expected = 3;
35
+        int actual = sll.size();
36
+
37
+        Assert.assertEquals(expected, actual);
38
+    }
39
+
40
+    @Test
41
+    public void removeTest() {
42
+        sll.add(num);
43
+        sll.remove(num);
44
+
45
+
46
+        int expected = 0;
47
+        int actual = sll.size();
48
+
49
+        Assert.assertEquals(expected, actual);
50
+    }
51
+
52
+    @Test
53
+    public void containsTest() {
54
+        sll.add(num);
55
+
56
+
57
+        boolean expected = true;
58
+        boolean actual = sll.contains(num);
59
+
60
+        Assert.assertEquals(expected, actual);
61
+    }
62
+
63
+    @Test
64
+    public void findTest() {
65
+        sll.add(num);
66
+        sll.add(num1);
67
+        sll.add(8);
68
+
69
+
70
+
71
+        Integer expected = 5;
72
+        Integer actual = sll.find(num1);
73
+
74
+        Assert.assertEquals(expected, actual);
75
+    }
76
+
77
+    @Test
78
+    public void sizeTest() {
79
+        sll.add(num);
80
+        sll.add(num1);
81
+        sll.add(8);
82
+
83
+
84
+
85
+        Integer expected = 3;
86
+        Integer actual = sll.size();
87
+
88
+        Assert.assertEquals(expected, actual);
89
+    }
90
+
91
+    @Test
92
+    public void getTest() {
93
+        sll.add(num);
94
+        sll.add(num1);
95
+        sll.add(8);
96
+
97
+
98
+
99
+        Integer expected = 5;
100
+        Integer actual = sll.get(num1);
101
+
102
+        Assert.assertEquals(expected, actual);
103
+    }
104
+
7 105
 }