Yesoda Sanka 6 gadus atpakaļ
vecāks
revīzija
5c31473572

+ 2
- 0
.idea/compiler.xml Parādīt failu

@@ -7,10 +7,12 @@
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 14
       <module name="CR-MesoLabs-Collections-SinglyLinkedList" target="1.5" />
15
+      <module name="singlylinkedlist" target="1.5" />
14 16
     </bytecodeTargetLevel>
15 17
   </component>
16 18
 </project>

+ 13
- 0
.idea/libraries/Maven__junit_junit_4_12.xml Parādīt failu

@@ -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 Parādīt failu

@@ -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>

+ 1
- 0
.idea/modules.xml Parādīt failu

@@ -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>

+ 507
- 363
.idea/workspace.xml
Failā izmaiņas netiks attēlotas, jo tās ir par lielu
Parādīt failu


+ 8
- 0
pom.xml Parādīt failu

@@ -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>

+ 26
- 0
src/main/java/com/zipcodewilmington/singlylinkedlist/DataItems.java Parādīt failu

@@ -0,0 +1,26 @@
1
+package com.zipcodewilmington.singlylinkedlist;
2
+
3
+public class DataItems{
4
+
5
+    private int key;
6
+    private String value;
7
+
8
+    public DataItems(int _key, String _value){
9
+        this.key=_key;
10
+        this.value=_value;
11
+    }
12
+
13
+    public int getKey() {
14
+        return key;
15
+    }
16
+
17
+    public String getValue() {
18
+        return value;
19
+    }
20
+
21
+    public String toString() {
22
+        return "("+getKey()+","+getValue()+")";
23
+    }
24
+
25
+}
26
+

+ 17
- 0
src/main/java/com/zipcodewilmington/singlylinkedlist/HeadNode.java Parādīt failu

@@ -0,0 +1,17 @@
1
+package com.zipcodewilmington.singlylinkedlist;
2
+
3
+public class HeadNode{
4
+
5
+// immutable class representing head node of linked list
6
+
7
+    Node nextNode;
8
+
9
+    public void setNextNode(Node _nextNode) {
10
+        nextNode=_nextNode;
11
+    }
12
+
13
+    public Node getNextNode() {
14
+        return nextNode;
15
+    }
16
+
17
+}

+ 17
- 1
src/main/java/com/zipcodewilmington/singlylinkedlist/MainApplication.java Parādīt failu

@@ -1,7 +1,23 @@
1 1
 package com.zipcodewilmington.singlylinkedlist;
2 2
 
3
+import java.util.LinkedList;
4
+
3 5
 /**
4 6
  * Created by leon on 1/10/18.
5 7
  */
6 8
 public class MainApplication {
7
-}
9
+
10
+    /*public  static void main () {
11
+       SinglyLinkedList  sl1=new SinglyLinkedList();
12
+        sl1.addNode( new DataItems(1,"hello") );
13
+        sl1.addNode(new DataItems(2,"John") );
14
+        sl1.addNode(new DataItems(3,"Smith") ) ;
15
+        sl1.addNode(new DataItems(4,"jose") ) ;
16
+        sl1.displayList() ;
17
+
18
+
19
+    }*/
20
+
21
+
22
+
23
+}

+ 26
- 0
src/main/java/com/zipcodewilmington/singlylinkedlist/Node.java Parādīt failu

@@ -0,0 +1,26 @@
1
+package com.zipcodewilmington.singlylinkedlist;
2
+
3
+public class Node {
4
+    private DataItems dataItems;
5
+    private Node nextNode;
6
+
7
+    public void setNextNode(Node _nextNode){
8
+        this.nextNode=_nextNode;
9
+    }
10
+
11
+    public Node getNextNode(){
12
+        return nextNode;
13
+    }
14
+
15
+    public DataItems getDataItems(){
16
+        return dataItems;
17
+    }
18
+
19
+    public void setDataItems(DataItems _dataItems){
20
+        this.dataItems=_dataItems;
21
+    }
22
+
23
+
24
+
25
+
26
+}

+ 174
- 0
src/main/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedList.java Parādīt failu

@@ -4,4 +4,178 @@ package com.zipcodewilmington.singlylinkedlist;
4 4
  * Created by leon on 1/10/18.
5 5
  */
6 6
 public class SinglyLinkedList {
7
+
8
+    HeadNode head;
9
+    public SinglyLinkedList(){
10
+        head = new HeadNode();
11
+
12
+    }
13
+
14
+    // insert node at the beginning of the list
15
+    public void addNode(DataItems _data){
16
+        Node newNode = new Node();
17
+        newNode.setDataItems(_data);
18
+        Node nextNode = head.getNextNode();
19
+        head.setNextNode(newNode);
20
+        newNode.setNextNode(nextNode);
21
+    }
22
+
23
+    public void addtoAnotherHeadNode(DataItems _data, HeadNode hd){
24
+        Node newNode = new Node();
25
+        newNode.setDataItems(_data);
26
+        Node nextNode = hd.getNextNode();
27
+        hd.setNextNode(newNode);
28
+        newNode.setNextNode(nextNode);
29
+    }
30
+    // insert node at the end of the list
31
+ /*   public void addNodeAttheEnd(DataItems _data){
32
+        Node newNode = new Node();
33
+        newNode.setDataItems(_data);
34
+        Node nodes = head.getNextNode();
35
+        HeadNode current;
36
+        //First Node
37
+        if (nodes == null) {
38
+            head.setNextNode(newNode);
39
+        }
40
+        else {
41
+            current=head;
42
+            while (current.getNextNode().getNextNode() != null) {
43
+
44
+               current=current.
45
+                //System.out.println("Node "+i+" : "+data.toString());
46
+                nodes = nodes.getNextNode();
47
+            }
48
+            head.setNextNode(newNode);
49
+        }
50
+    }*/
51
+    //returns a new linked list containing the same values
52
+
53
+    public HeadNode copyOfLinkedlist(){
54
+        HeadNode hd = new HeadNode();
55
+        Node nodes = head.getNextNode();
56
+        int i=0;
57
+        while(nodes!=null) {
58
+            DataItems data = nodes.getDataItems();
59
+            addtoAnotherHeadNode (data, hd);
60
+            nodes = nodes.getNextNode();
61
+
62
+        }
63
+
64
+        return hd;
65
+    }
66
+
67
+    // delete node at the beginning of the list
68
+    public void removeNode(){
69
+        Node toBeDeletedNode = head.getNextNode();
70
+        if(toBeDeletedNode!=null) {
71
+            Node nextNode = toBeDeletedNode.getNextNode();
72
+            head.setNextNode(nextNode);
73
+            toBeDeletedNode.setNextNode(null);
74
+        } else {
75
+            System.out.println("No nodes to be deleted");
76
+        }
77
+
78
+    }
79
+
80
+    // display all nodes data
81
+    public void displayList(){
82
+        Node nodes = head.getNextNode();
83
+        int i=0;
84
+        while(nodes!=null) {
85
+            DataItems data = nodes.getDataItems();
86
+            System.out.println("Node "+i+" : "+data.toString());
87
+            nodes = nodes.getNextNode();
88
+            i++;
89
+
90
+        }
91
+    }
92
+
93
+    // reverse order of linked list
94
+    public void reverseLinkedList(){
95
+        int sizeOfList = size();
96
+        Node lastNode = getNodeAtIndex(sizeOfList-1);
97
+        Node snode, tnode;
98
+        for(int i=sizeOfList-2;i>=0;i--){
99
+            snode = getNodeAtIndex(i);
100
+            tnode = snode.getNextNode();
101
+            tnode.setNextNode(snode);
102
+        }
103
+        getNodeAtIndex(0).setNextNode(null);
104
+        head.setNextNode(lastNode);
105
+    }
106
+
107
+
108
+    public int findKey(int _key){
109
+        int i=0;
110
+        DataItems data = dataAtNodeIndex(i);
111
+        while(data!=null){
112
+            if(data.getKey()== _key){
113
+                System.out.println("Node at index : "+i+" has data item : "+data.toString());
114
+                break;
115
+            }
116
+            i++;
117
+            data = dataAtNodeIndex(i);
118
+
119
+        }
120
+        if(i>0) {
121
+            return i;
122
+        }else{
123
+
124
+            return -1;
125
+        }
126
+
127
+
128
+    }
129
+
130
+    public boolean contains(int _key){
131
+        int i=0;
132
+        DataItems data = dataAtNodeIndex(i);
133
+        boolean flag=false;
134
+        while(data!=null){
135
+            if(data.getKey()== _key){
136
+               flag=true;
137
+            }
138
+            i++;
139
+            data = dataAtNodeIndex(i);
140
+        }
141
+        return flag;
142
+    }
143
+    // return the size of linked list
144
+    public int size() {
145
+        int count=0;
146
+        Node nodes = getNodeAtIndex(count);
147
+        while(nodes!=null) {
148
+            nodes = getNodeAtIndex(++count);
149
+        }
150
+        return count;
151
+    }
152
+
153
+
154
+    // return data item at particular node
155
+    public DataItems dataAtNodeIndex(int _index){
156
+        Node nodes = getNodeAtIndex(_index);
157
+        if(nodes!=null) {
158
+            return nodes.getDataItems();
159
+        } else {
160
+            return null;
161
+        }
162
+    }
163
+
164
+    // return node at particular index
165
+    private Node getNodeAtIndex(int _index){
166
+        if(_index<0) {
167
+            return null;
168
+        } else {
169
+            Node nodes = head.getNextNode();
170
+            int i=0;
171
+            while(i<_index && nodes!=null) {
172
+                nodes = nodes.getNextNode();
173
+                i++;
174
+            }
175
+            return nodes;
176
+        }
177
+    }
178
+
179
+
180
+
7 181
 }

+ 126
- 1
src/test/java/com/zipcodewilmington/singlylinkedlist/SinglyLinkedListTest.java Parādīt failu

@@ -1,7 +1,132 @@
1 1
 package com.zipcodewilmington.singlylinkedlist;
2 2
 
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+import java.util.LinkedList;
7
+
3 8
 /**
4 9
  * Created by leon on 1/10/18.
5 10
  */
6 11
 public class SinglyLinkedListTest {
7
-}
12
+
13
+    @Test
14
+    public void testInsert() {
15
+        SinglyLinkedList sl = new SinglyLinkedList();
16
+
17
+        // Create to the list
18
+        DataItems data = new DataItems(1, "Data_" + 1);
19
+        sl.addNode(data);
20
+        data = new DataItems(2, "Data_" + 2);
21
+        sl.addNode(data);
22
+        data = new DataItems(4, "Data_" + 4);
23
+        sl.addNode(data);
24
+
25
+        sl.displayList();
26
+        DataItems actualData = sl.dataAtNodeIndex(0);
27
+        System.out.println("");
28
+
29
+        Assert.assertEquals(data, actualData);
30
+    }
31
+
32
+    @Test
33
+    public void testdelete() {
34
+        SinglyLinkedList sl = new SinglyLinkedList();
35
+        DataItems data = new DataItems(1, "Data_" + 1);
36
+        sl.addNode(data);
37
+        data = new DataItems(2, "Data_" + 2);
38
+        sl.addNode(data);
39
+        System.out.println("before deleting the node");
40
+        sl.displayList();
41
+        sl.removeNode();
42
+        System.out.println("after deleting the node");
43
+        sl.displayList();
44
+
45
+    }
46
+
47
+
48
+    @Test
49
+    public void testdisplay() {
50
+        SinglyLinkedList sl = new SinglyLinkedList();
51
+        DataItems data = new DataItems(3, "Data_" + 1);
52
+        sl.addNode(data);
53
+        data = new DataItems(2, "Data_" + 1);
54
+        sl.addNode(data);
55
+        sl.displayList();
56
+
57
+    }
58
+
59
+    @Test
60
+    public void testreversedLinkedlist() {
61
+        SinglyLinkedList sl = new SinglyLinkedList();
62
+        DataItems data = new DataItems(1, "Data_" + 1);
63
+        sl.addNode(data);
64
+        data = new DataItems(2, "Data_" + 2);
65
+        sl.addNode(data);
66
+        sl.reverseLinkedList();
67
+        sl.displayList();
68
+
69
+    }
70
+
71
+    @Test
72
+    public void testgetSize() {
73
+        SinglyLinkedList sl = new SinglyLinkedList();
74
+        DataItems data = new DataItems(1, "Data_" + 1);
75
+        sl.addNode(data);
76
+        data = new DataItems(2, "Data_" + 2);
77
+        sl.addNode(data);
78
+        int expected = 2;
79
+        int actual = sl.size();
80
+        Assert.assertEquals(expected, actual);
81
+        //sl.displayList() ;
82
+    }
83
+
84
+    @Test
85
+    public void testdatanodeindexy() {
86
+        SinglyLinkedList sl = new SinglyLinkedList();
87
+        DataItems data = new DataItems(1, "Data_" + 1);
88
+        sl.addNode(data);
89
+        data = new DataItems(2, "Data_" + 2);
90
+        sl.addNode(data);
91
+        sl.dataAtNodeIndex(1);
92
+
93
+    }
94
+
95
+    @Test
96
+    public void testcontains() {
97
+        SinglyLinkedList sl = new SinglyLinkedList();
98
+        DataItems data = new DataItems(1, "Data_" + 1);
99
+        sl.addNode(data);
100
+        data = new DataItems(2, "Data_" + 2);
101
+        sl.addNode(data);
102
+        sl.contains(2);
103
+    }
104
+
105
+    @Test
106
+    public void testCopyOfLinkedList() {
107
+        SinglyLinkedList sl = new SinglyLinkedList();
108
+
109
+        // Create to the list
110
+        DataItems data = new DataItems(1, "Data_" + 1);
111
+        sl.addNode(data);
112
+        data = new DataItems(2, "Data_" + 2);
113
+        sl.addNode(data);
114
+        data = new DataItems(4, "Data_" + 4);
115
+        sl.addNode(data);
116
+
117
+        sl.displayList();
118
+        System.out.println("copy oif a list") ;
119
+        HeadNode hd =  sl.copyOfLinkedlist();
120
+
121
+        Node nodes = hd.getNextNode();
122
+        int i=0;
123
+        while(nodes!=null) {
124
+             data = nodes.getDataItems();
125
+
126
+            System.out.println("Node "+i+" : "+data.toString());
127
+            nodes = nodes.getNextNode();
128
+
129
+            i++;
130
+        }
131
+    }
132
+}