Create a generic Node class and make use of it.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 4 May 2018 09:22:04 +0000 (11:22 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 4 May 2018 09:22:04 +0000 (11:22 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
Listes/Node.java [new file with mode: 0644]
Structure/Liste.java
Structure/Makefile
Structure/Node.java [new file with mode: 0644]
TP2/Liste.java
TP2/Makefile
TP2/Node.java [new file with mode: 0644]

diff --git a/Listes/Node.java b/Listes/Node.java
new file mode 100644 (file)
index 0000000..782b719
--- /dev/null
@@ -0,0 +1,31 @@
+public class Node<T> {
+    private T data;
+    private Node<T> next;
+
+    Node(T value) {
+        setData(value);
+        setNext(null);
+    }
+
+    Node(T value, Node<T> nextNode) {
+        setData(value);
+        setNext(nextNode);
+    }
+
+    public T getData() {
+        return data;
+    }
+
+    public void setData(T value) {
+        data = value;
+    }
+
+    public Node<T> getNext() {
+        return next;
+    }
+
+    public void setNext(Node<T> nextNode) {
+        next = nextNode;
+    }
+
+}
index 448878f8c63d4eea071132881798b8ab1233bd94..046267e6b211a385d1275071dcec81b690215d27 100644 (file)
@@ -1,40 +1,7 @@
 
 
 public class Liste extends Structure {
 
 
 public class Liste extends Structure {
-
-    private class IntNode {
-        private int data;
-        private IntNode next;
-
-        IntNode(int value) {
-            setData(value);
-            setNext(null);
-        }
-
-        IntNode(int value, IntNode nextNode) {
-            setData(value);
-            setNext(nextNode);
-        }
-
-        public int getData() {
-            return data;
-        }
-
-        public void setData(int value) {
-            data = value;
-        }
-
-        public IntNode getNext() {
-            return next;
-        }
-
-        public void setNext(IntNode nextNode) {
-            next = nextNode;
-        }
-
-    }
-
-    private IntNode headNode;
+    private Node<Integer> headNode;
     private int list_counter;
 
     Liste() {
     private int list_counter;
 
     Liste() {
@@ -55,25 +22,25 @@ public class Liste extends Structure {
         list_counter = size;
     }
 
         list_counter = size;
     }
 
-    public void setHeadNode(IntNode node) {
+    public void setHeadNode(Node<Integer> node) {
         headNode = node;
     }
 
         headNode = node;
     }
 
-    public IntNode getHeadNode() {
+    public Node<Integer> getHeadNode() {
         return headNode;
     }
 
     public boolean inserer(int value) {
         boolean found = false;
         if (isEmpty()) {
         return headNode;
     }
 
     public boolean inserer(int value) {
         boolean found = false;
         if (isEmpty()) {
-            headNode = new IntNode(value);
+            headNode = new Node<Integer>(value);
             list_counter++;
             return true;
         } else if (value == headNode.getData()) {
             found = true;
             return true;
         } else {
             list_counter++;
             return true;
         } else if (value == headNode.getData()) {
             found = true;
             return true;
         } else {
-            IntNode nodeCursorNext = headNode.getNext();
+            Node<Integer> nodeCursorNext = headNode.getNext();
             while (nodeCursorNext != null) {
                 if (value == nodeCursorNext.getData()) {
                     found = true;
             while (nodeCursorNext != null) {
                 if (value == nodeCursorNext.getData()) {
                     found = true;
@@ -83,7 +50,7 @@ public class Liste extends Structure {
                 }
             }
             if (!found) {
                 }
             }
             if (!found) {
-                headNode = new IntNode(value, headNode);
+                headNode = new Node<Integer>(value, headNode);
                 list_counter++;
             }
             // Insertion in a linked list can't fail
                 list_counter++;
             }
             // Insertion in a linked list can't fail
@@ -100,8 +67,8 @@ public class Liste extends Structure {
             deleted = true;
             list_counter--;
         } else {
             deleted = true;
             list_counter--;
         } else {
-            IntNode nodeCursor = headNode;
-            IntNode nodeCursorNext = headNode.getNext();
+            Node<Integer> nodeCursor = headNode;
+            Node<Integer> nodeCursorNext = headNode.getNext();
             while (nodeCursorNext != null) {
                 if (value == nodeCursorNext.getData()) {
                     nodeCursor.setNext(nodeCursorNext.getNext());
             while (nodeCursorNext != null) {
                 if (value == nodeCursorNext.getData()) {
                     nodeCursor.setNext(nodeCursorNext.getNext());
@@ -126,7 +93,7 @@ public class Liste extends Structure {
         } else if (headNode.getNext() == null) {
             System.out.println("element " + i + " : " + headNode.getData());
         } else {
         } else if (headNode.getNext() == null) {
             System.out.println("element " + i + " : " + headNode.getData());
         } else {
-            IntNode nodeCursorNext = headNode.getNext();
+            Node<Integer> nodeCursorNext = headNode.getNext();
             System.out.println("element " + i + " : " + headNode.getData());
             i++;
             while (nodeCursorNext != null) {
             System.out.println("element " + i + " : " + headNode.getData());
             i++;
             while (nodeCursorNext != null) {
index c4447fc6e5d170178dc062cccee24a594f8e5b5d..1731ce66a5ccdbdf60df0d68f7596eb05607c90f 100644 (file)
@@ -47,7 +47,8 @@ JVM = java
 
 CLASSES = \
                Structure.java \
 
 CLASSES = \
                Structure.java \
-        Entiers.java \
+               Entiers.java \
+               Node.java \
                Liste.java \
                Main.java
 
                Liste.java \
                Main.java
 
diff --git a/Structure/Node.java b/Structure/Node.java
new file mode 100644 (file)
index 0000000..782b719
--- /dev/null
@@ -0,0 +1,31 @@
+public class Node<T> {
+    private T data;
+    private Node<T> next;
+
+    Node(T value) {
+        setData(value);
+        setNext(null);
+    }
+
+    Node(T value, Node<T> nextNode) {
+        setData(value);
+        setNext(nextNode);
+    }
+
+    public T getData() {
+        return data;
+    }
+
+    public void setData(T value) {
+        data = value;
+    }
+
+    public Node<T> getNext() {
+        return next;
+    }
+
+    public void setNext(Node<T> nextNode) {
+        next = nextNode;
+    }
+
+}
index 6928b00737e0656548f61f6b49cc791812f80b77..abf2efd58ac91f824e8930f949c8238b633c72db 100644 (file)
@@ -4,40 +4,7 @@
     date = "05/12/2011"
 )
 public class Liste extends Structure {
     date = "05/12/2011"
 )
 public class Liste extends Structure {
-
-    private class IntNode {
-        private int data;
-        private IntNode next;
-
-        IntNode(int value) {
-            setData(value);
-            setNext(null);
-        }
-
-        IntNode(int value, IntNode nextNode) {
-            setData(value);
-            setNext(nextNode);
-        }
-
-        public int getData() {
-            return data;
-        }
-
-        public void setData(int value) {
-            data = value;
-        }
-
-        public IntNode getNext() {
-            return next;
-        }
-
-        public void setNext(IntNode nextNode) {
-            next = nextNode;
-        }
-
-    }
-
-    private IntNode headNode;
+    private Node<Integer> headNode;
     private int list_counter;
 
     Liste() {
     private int list_counter;
 
     Liste() {
@@ -58,25 +25,25 @@ public class Liste extends Structure {
         list_counter = size;
     }
 
         list_counter = size;
     }
 
-    public void setHeadNode(IntNode node) {
+    public void setHeadNode(Node<Integer> node) {
         headNode = node;
     }
 
         headNode = node;
     }
 
-    public IntNode getHeadNode() {
+    public Node<Integer> getHeadNode() {
         return headNode;
     }
 
     public boolean inserer(int value) {
         boolean found = false;
         if (isEmpty()) {
         return headNode;
     }
 
     public boolean inserer(int value) {
         boolean found = false;
         if (isEmpty()) {
-            headNode = new IntNode(value);
+            headNode = new Node<Integer>(value);
             list_counter++;
             return true;
         } else if (value == headNode.getData()) {
             found = true;
             return true;
         } else {
             list_counter++;
             return true;
         } else if (value == headNode.getData()) {
             found = true;
             return true;
         } else {
-            IntNode nodeCursorNext = headNode.getNext();
+            Node<Integer> nodeCursorNext = headNode.getNext();
             while (nodeCursorNext != null) {
                 if (value == nodeCursorNext.getData()) {
                     found = true;
             while (nodeCursorNext != null) {
                 if (value == nodeCursorNext.getData()) {
                     found = true;
@@ -86,7 +53,7 @@ public class Liste extends Structure {
                 }
             }
             if (!found) {
                 }
             }
             if (!found) {
-                headNode = new IntNode(value, headNode);
+                headNode = new Node<Integer>(value, headNode);
                 list_counter++;
             }
             // Insertion in a linked list can't fail
                 list_counter++;
             }
             // Insertion in a linked list can't fail
@@ -103,8 +70,8 @@ public class Liste extends Structure {
             deleted = true;
             list_counter--;
         } else {
             deleted = true;
             list_counter--;
         } else {
-            IntNode nodeCursor = headNode;
-            IntNode nodeCursorNext = headNode.getNext();
+            Node<Integer> nodeCursor = headNode;
+            Node<Integer> nodeCursorNext = headNode.getNext();
             while (nodeCursorNext != null) {
                 if (value == nodeCursorNext.getData()) {
                     nodeCursor.setNext(nodeCursorNext.getNext());
             while (nodeCursorNext != null) {
                 if (value == nodeCursorNext.getData()) {
                     nodeCursor.setNext(nodeCursorNext.getNext());
@@ -129,7 +96,7 @@ public class Liste extends Structure {
         } else if (headNode.getNext() == null) {
             System.out.println("element " + i + " : " + headNode.getData());
         } else {
         } else if (headNode.getNext() == null) {
             System.out.println("element " + i + " : " + headNode.getData());
         } else {
-            IntNode nodeCursorNext = headNode.getNext();
+            Node<Integer> nodeCursorNext = headNode.getNext();
             System.out.println("element " + i + " : " + headNode.getData());
             i++;
             while (nodeCursorNext != null) {
             System.out.println("element " + i + " : " + headNode.getData());
             i++;
             while (nodeCursorNext != null) {
@@ -148,7 +115,7 @@ public class Liste extends Structure {
             headNode = null;
         } else {
             // We have at least 2 nodes in the linked list
             headNode = null;
         } else {
             // We have at least 2 nodes in the linked list
-            IntNode nodeCursor = headNode;
+            Node<Integer> nodeCursor = headNode;
             int i = 0;
             // Go to the node at the nElements place
             while (i < nElements - 1 && nodeCursor.getNext() != null && nElements > 1) {
             int i = 0;
             // Go to the node at the nElements place
             while (i < nElements - 1 && nodeCursor.getNext() != null && nElements > 1) {
index 849e82a46fa7f130e2172acacb1791a8457cd723..e72fbed71721e152eeb503db16be448c2e3d1e35 100644 (file)
@@ -56,6 +56,7 @@ CLASSES = \
                Segment.java \
                Cercle.java \
                Entiers.java \
                Segment.java \
                Cercle.java \
                Entiers.java \
+               Node.java \
                Liste.java \
                Piletransformations.java \
                Main.java
                Liste.java \
                Piletransformations.java \
                Main.java
diff --git a/TP2/Node.java b/TP2/Node.java
new file mode 100644 (file)
index 0000000..782b719
--- /dev/null
@@ -0,0 +1,31 @@
+public class Node<T> {
+    private T data;
+    private Node<T> next;
+
+    Node(T value) {
+        setData(value);
+        setNext(null);
+    }
+
+    Node(T value, Node<T> nextNode) {
+        setData(value);
+        setNext(nextNode);
+    }
+
+    public T getData() {
+        return data;
+    }
+
+    public void setData(T value) {
+        data = value;
+    }
+
+    public Node<T> getNext() {
+        return next;
+    }
+
+    public void setNext(Node<T> nextNode) {
+        next = nextNode;
+    }
+
+}