From 4d9f4a5860c2dffb988982e330039d09874e22d5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 4 May 2018 11:22:04 +0200 Subject: [PATCH] Create a generic Node class and make use of it. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- Listes/Node.java | 31 ++++++++++++++++++++++++++ Structure/Liste.java | 51 ++++++++---------------------------------- Structure/Makefile | 3 ++- Structure/Node.java | 31 ++++++++++++++++++++++++++ TP2/Liste.java | 53 +++++++++----------------------------------- TP2/Makefile | 1 + TP2/Node.java | 31 ++++++++++++++++++++++++++ 7 files changed, 115 insertions(+), 86 deletions(-) create mode 100644 Listes/Node.java create mode 100644 Structure/Node.java create mode 100644 TP2/Node.java diff --git a/Listes/Node.java b/Listes/Node.java new file mode 100644 index 0000000..782b719 --- /dev/null +++ b/Listes/Node.java @@ -0,0 +1,31 @@ +public class Node { + private T data; + private Node next; + + Node(T value) { + setData(value); + setNext(null); + } + + Node(T value, Node nextNode) { + setData(value); + setNext(nextNode); + } + + public T getData() { + return data; + } + + public void setData(T value) { + data = value; + } + + public Node getNext() { + return next; + } + + public void setNext(Node nextNode) { + next = nextNode; + } + +} diff --git a/Structure/Liste.java b/Structure/Liste.java index 448878f..046267e 100644 --- a/Structure/Liste.java +++ b/Structure/Liste.java @@ -1,40 +1,7 @@ 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 headNode; private int list_counter; Liste() { @@ -55,25 +22,25 @@ public class Liste extends Structure { list_counter = size; } - public void setHeadNode(IntNode node) { + public void setHeadNode(Node node) { headNode = node; } - public IntNode getHeadNode() { + public Node getHeadNode() { return headNode; } public boolean inserer(int value) { boolean found = false; if (isEmpty()) { - headNode = new IntNode(value); + headNode = new Node(value); list_counter++; return true; } else if (value == headNode.getData()) { found = true; return true; } else { - IntNode nodeCursorNext = headNode.getNext(); + Node nodeCursorNext = headNode.getNext(); while (nodeCursorNext != null) { if (value == nodeCursorNext.getData()) { found = true; @@ -83,7 +50,7 @@ public class Liste extends Structure { } } if (!found) { - headNode = new IntNode(value, headNode); + headNode = new Node(value, headNode); list_counter++; } // Insertion in a linked list can't fail @@ -100,8 +67,8 @@ public class Liste extends Structure { deleted = true; list_counter--; } else { - IntNode nodeCursor = headNode; - IntNode nodeCursorNext = headNode.getNext(); + Node nodeCursor = headNode; + Node nodeCursorNext = headNode.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 { - IntNode nodeCursorNext = headNode.getNext(); + Node nodeCursorNext = headNode.getNext(); System.out.println("element " + i + " : " + headNode.getData()); i++; while (nodeCursorNext != null) { diff --git a/Structure/Makefile b/Structure/Makefile index c4447fc..1731ce6 100644 --- a/Structure/Makefile +++ b/Structure/Makefile @@ -47,7 +47,8 @@ JVM = java CLASSES = \ Structure.java \ - Entiers.java \ + Entiers.java \ + Node.java \ Liste.java \ Main.java diff --git a/Structure/Node.java b/Structure/Node.java new file mode 100644 index 0000000..782b719 --- /dev/null +++ b/Structure/Node.java @@ -0,0 +1,31 @@ +public class Node { + private T data; + private Node next; + + Node(T value) { + setData(value); + setNext(null); + } + + Node(T value, Node nextNode) { + setData(value); + setNext(nextNode); + } + + public T getData() { + return data; + } + + public void setData(T value) { + data = value; + } + + public Node getNext() { + return next; + } + + public void setNext(Node nextNode) { + next = nextNode; + } + +} diff --git a/TP2/Liste.java b/TP2/Liste.java index 6928b00..abf2efd 100644 --- a/TP2/Liste.java +++ b/TP2/Liste.java @@ -4,40 +4,7 @@ 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 headNode; private int list_counter; Liste() { @@ -58,25 +25,25 @@ public class Liste extends Structure { list_counter = size; } - public void setHeadNode(IntNode node) { + public void setHeadNode(Node node) { headNode = node; } - public IntNode getHeadNode() { + public Node getHeadNode() { return headNode; } public boolean inserer(int value) { boolean found = false; if (isEmpty()) { - headNode = new IntNode(value); + headNode = new Node(value); list_counter++; return true; } else if (value == headNode.getData()) { found = true; return true; } else { - IntNode nodeCursorNext = headNode.getNext(); + Node nodeCursorNext = headNode.getNext(); while (nodeCursorNext != null) { if (value == nodeCursorNext.getData()) { found = true; @@ -86,7 +53,7 @@ public class Liste extends Structure { } } if (!found) { - headNode = new IntNode(value, headNode); + headNode = new Node(value, headNode); list_counter++; } // Insertion in a linked list can't fail @@ -103,8 +70,8 @@ public class Liste extends Structure { deleted = true; list_counter--; } else { - IntNode nodeCursor = headNode; - IntNode nodeCursorNext = headNode.getNext(); + Node nodeCursor = headNode; + Node nodeCursorNext = headNode.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 { - IntNode nodeCursorNext = headNode.getNext(); + Node nodeCursorNext = headNode.getNext(); 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 - IntNode nodeCursor = headNode; + Node nodeCursor = headNode; int i = 0; // Go to the node at the nElements place while (i < nElements - 1 && nodeCursor.getNext() != null && nElements > 1) { diff --git a/TP2/Makefile b/TP2/Makefile index 849e82a..e72fbed 100644 --- a/TP2/Makefile +++ b/TP2/Makefile @@ -56,6 +56,7 @@ CLASSES = \ Segment.java \ Cercle.java \ Entiers.java \ + Node.java \ Liste.java \ Piletransformations.java \ Main.java diff --git a/TP2/Node.java b/TP2/Node.java new file mode 100644 index 0000000..782b719 --- /dev/null +++ b/TP2/Node.java @@ -0,0 +1,31 @@ +public class Node { + private T data; + private Node next; + + Node(T value) { + setData(value); + setNext(null); + } + + Node(T value, Node nextNode) { + setData(value); + setNext(nextNode); + } + + public T getData() { + return data; + } + + public void setData(T value) { + data = value; + } + + public Node getNext() { + return next; + } + + public void setNext(Node nextNode) { + next = nextNode; + } + +} -- 2.34.1