From: Jérôme Benoit Date: Fri, 4 May 2018 09:44:13 +0000 (+0200) Subject: exo4: add a generic Node class and make use of it. X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=bb6a4f9a6e8fe7d7a6a08f008c63072ff2c2e460;p=Project_POO.git exo4: add a generic Node class and make use of it. Signed-off-by: Jérôme Benoit --- diff --git a/.gitignore b/.gitignore index b5705d3..885da23 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ hs_err_pid* # editor trash *.swp *~ + +.DS_Store diff --git a/exo4/Liste.java b/exo4/Liste.java index 6928b00..abf2efd 100644 --- a/exo4/Liste.java +++ b/exo4/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/exo4/Main.java b/exo4/Main.java index a83fab4..479313d 100644 --- a/exo4/Main.java +++ b/exo4/Main.java @@ -17,6 +17,7 @@ class Main { packageClasses.add(Entiers.class); packageClasses.add(Forme.class); packageClasses.add(Image.class); + packageClasses.add(Node.class); packageClasses.add(Liste.class); packageClasses.add(Piletransformations.class); packageClasses.add(Point.class); diff --git a/exo4/Node.java b/exo4/Node.java new file mode 100644 index 0000000..c2dc310 --- /dev/null +++ b/exo4/Node.java @@ -0,0 +1,35 @@ +@ClassPreamble ( + author = "Jérôme Benoit", + date = "09/11/2012" +) +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; + } + +}