aboutsummaryrefslogtreecommitdiffstats
path: root/src/datastructures/linkedlists/template
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-01-22 12:18:42 +0100
committerMattias Andrée <maandree@operamail.com>2014-01-22 12:18:42 +0100
commit3344d6da4af50522d4de0920b010e99f8377cdd0 (patch)
tree809cca612304c4fc47f316b1d043a9117873bb8a /src/datastructures/linkedlists/template
parentm doc (diff)
downloadalgorithms-and-data-structures-3344d6da4af50522d4de0920b010e99f8377cdd0.tar.gz
algorithms-and-data-structures-3344d6da4af50522d4de0920b010e99f8377cdd0.tar.bz2
algorithms-and-data-structures-3344d6da4af50522d4de0920b010e99f8377cdd0.tar.xz
preparation for circularly linked lists
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r--src/datastructures/linkedlists/template41
1 files changed, 33 insertions, 8 deletions
diff --git a/src/datastructures/linkedlists/template b/src/datastructures/linkedlists/template
index b0311b5..0a24a89 100644
--- a/src/datastructures/linkedlists/template
+++ b/src/datastructures/linkedlists/template
@@ -54,10 +54,12 @@
+£>if (( with_head )); then
/**
* The first node in the list
*/
public Node head = null;
+£>fi
£>if (( with_tail )); then
/**
@@ -67,12 +69,29 @@
£>fi
+
+£>if (( 1 - with_head )) && (( 1 - with_tail )); then
+ /**
+ * Creates the initial node in a circularly linked list
+ *
+ * @param value The value of the initial node
+ * @return The node that has been created and inserted
+ */
+ public Node create(T value)
+ {
+ Node node = new Node(value);
+£>(( with_prev )) &&
+ node.previous = node;
+ return node.next = node;
+ }
+£>fi
+£>if (( with_head )); then
/**
* Insert a value in the beginning of the list
*
* @param value The value to insert
- * @return The node that has be created and inserted
+ * @return The node that has been created and inserted
*/
public Node insertBeginning(T value)
{
@@ -110,13 +129,14 @@
£>fi
return node;
}
+£>fi
/**
* Insert a value after a specified, reference, node
*
* @param value The value to insert
* @param predecessor The reference node
- * @return The node that has be created and inserted
+ * @return The node that has been created and inserted
*/
public Node insertAfter(T value, Node predecessor)
{
@@ -163,7 +183,7 @@
*
* @param value The value to insert
* @param successor The reference node
- * @return The node that has be created and inserted
+ * @return The node that has been created and inserted
*/
public Node insertBefore(T value, Node successor)
{
@@ -173,8 +193,10 @@
node.next = successor;
if (node.previous != null)
node.previous.next = node;
+£>if (( with_tail )); then
if (this.head == successor)
this.head = node;
+£>fi
return node;
}
@@ -191,8 +213,10 @@
successor.previous = node.previous;
else if (node.previous != null)
node.previous.next = successor;
+£>if (( with_head )); then
if (this.head == node)
this.head = successor;
+£>fi
return node;
}
@@ -203,11 +227,12 @@
*/
public void remove(Node node)
{
- if (node.previous == null)
- this.head = node.next;
- else
+ if (node.previous != null)
node.previous.next = node.next;
-
+£>if (( with_head )); then
+ else
+ this.head = node.next;
+£>fi
if (node.next != null)
node.next.previous = node.previous;
£>if (( with_tail )); then
@@ -222,7 +247,7 @@
* Insert a value in the end of the list
*
* @param value The value to insert
- * @return The node that has be created and inserted
+ * @return The node that has been created and inserted
*/
public Node insertEnd(T value)
{