Skip to content

Fix #436 - CircleLinkedList malfunction #440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 7, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix null value issue stated in #436
  • Loading branch information
Skhwan committed May 19, 2018
commit c4a8e1e18c0ea36bf38c6572bdf782606e546114
17 changes: 11 additions & 6 deletions DataStructures/Lists/CircleLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ private Node(E value, Node<E> next){
this.next = next;
}
}

//For better O.O design this should be private allows for better black box design
private int size;
//this will point to dummy node;
private Node<E> head;
private Node<E> tail;
//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;
public CircleLinkedList(){
//creation of the dummy node
head = new Node<E>(null,head);
size = 0;
head = new Node<>(null, head);
tail = head;
}
// getter for the size... needed because size is private.
public int getSize(){ return size;}
Expand All @@ -25,9 +26,13 @@ public void append(E value){
// we do not want to add null elements to the list.
throw new NullPointerException("Cannot add null element to the list");
}
//head.next points to the last element;
head.next = new Node<E>(value,head);
size++;}

//add new node at the end of the list and update tail node to point to new node
tail.next = new Node(value, head);
tail = tail.next;
size++;
}

public E remove(int pos){
if(pos>size || pos< 0){
//catching errors
Expand Down