Skip to content

Commit 595cc8f

Browse files
author
Christian Bender
authored
Merge pull request #440 from Skhwan/master
Fix #436 - CircleLinkedList malfunction
2 parents a83875a + a85f252 commit 595cc8f

File tree

1 file changed

+55
-50
lines changed

1 file changed

+55
-50
lines changed
Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,59 @@
11
public class CircleLinkedList<E>{
2-
private static class Node<E>{
3-
Node<E> next;
4-
E value;
5-
private Node(E value, Node<E> next){
6-
this.value = value;
7-
this.next = next;
8-
}
9-
}
10-
//For better O.O design this should be private allows for better black box design
11-
private int size;
12-
//this will point to dummy node;
13-
private Node<E> head;
14-
//constructer for class.. here we will make a dummy node for circly linked list implementation with reduced error catching as our list will never be empty;
15-
public CircleLinkedList(){
16-
//creation of the dummy node
17-
head = new Node<E>(null,head);
18-
size = 0;
19-
}
20-
// getter for the size... needed because size is private.
21-
public int getSize(){ return size;}
22-
// for the sake of simplistiy this class will only contain the append function or addLast other add functions can be implemented however this is the basses of them all really.
23-
public void append(E value){
24-
if(value == null){
25-
// we do not want to add null elements to the list.
26-
throw new NullPointerException("Cannot add null element to the list");
27-
}
28-
//head.next points to the last element;
29-
head.next = new Node<E>(value,head);
30-
size++;}
31-
public E remove(int pos){
32-
if(pos>size || pos< 0){
33-
//catching errors
34-
throw new IndexOutOfBoundsException("position cannot be greater than size or negative");
35-
}
36-
Node<E> iterator = head.next;
37-
//we need to keep track of the element before the element we want to remove we can see why bellow.
38-
Node<E> before = head;
39-
for(int i = 1; i<=pos; i++){
40-
iterator = iterator.next;
41-
before = before.next;
42-
}
43-
E saved = iterator.value;
44-
// assigning the next referance to the the element following the element we want to remove... the last element will be assigned to the head.
45-
before.next = iterator.next;
46-
// scrubbing
47-
iterator.next = null;
48-
iterator.value = null;
49-
return saved;
2+
private static class Node<E>{
3+
Node<E> next;
4+
E value;
5+
private Node(E value, Node<E> next){
6+
this.value = value;
7+
this.next = next;
8+
}
9+
}
10+
11+
//For better O.O design this should be private allows for better black box design
12+
private int size;
13+
//this will point to dummy node;
14+
private Node<E> head;
15+
private Node<E> tail;
16+
//constructer for class.. here we will make a dummy node for circly linked list implementation with reduced error catching as our list will never be empty;
17+
public CircleLinkedList(){
18+
head = new Node<>(null, head);
19+
tail = head;
20+
}
21+
// getter for the size... needed because size is private.
22+
public int getSize(){ return size;}
23+
// for the sake of simplistiy this class will only contain the append function or addLast other add functions can be implemented however this is the basses of them all really.
24+
public void append(E value){
25+
if(value == null){
26+
// we do not want to add null elements to the list.
27+
throw new NullPointerException("Cannot add null element to the list");
28+
}
5029

51-
}
30+
//add new node at the end of the list and update tail node to point to new node
31+
tail.next = new Node(value, head);
32+
tail = tail.next;
33+
size++;
34+
}
35+
36+
public E remove(int pos){
37+
if(pos>=size || pos< 0){
38+
//catching errors
39+
throw new IndexOutOfBoundsException("position cannot be greater than size or negative");
40+
}
41+
Node<E> iterator = head.next;
42+
//we need to keep track of the element before the element we want to remove we can see why bellow.
43+
Node<E> before = head;
44+
for(int i = 1; i<=pos; i++){
45+
iterator = iterator.next;
46+
before = before.next;
47+
}
48+
E removedValue = iterator.value;
49+
// assigning the next reference to the the element following the element we want to remove... the last element will be assigned to the head.
50+
before.next = iterator.next;
51+
// scrubbing
52+
iterator.next = null;
53+
iterator.value = null;
54+
size--;
5255

53-
}
56+
return removedValue;
57+
}
5458

59+
}

0 commit comments

Comments
 (0)