I am implementing a method add(int index, E element) that is supposed to insert the specified element at the specified index in a list and shift the element currently at that position and any subsequent elements to the right. Does anyone know why my method does not work when my code is:
newNode.setNext(temp.getNext());
newNode.setPrev(temp);
newNode.getNext().setPrev(newNode);
temp.setNext(newNode);
at the end of the method but works when I have only
newNode.setNext(temp.getNext());
temp.setNext(newNode);
My code:
public class DoubleLinkedList<E> implements IDoubleLinkedList<E> {
DLLNode head;
DLLNode tail;
int size = 0;
#Override
public void add(int index, E element) throws IndexOutOfBoundsException {
if (index > size) {
throw new IndexOutOfBoundsException();
}
if (index < 0) {
throw new IndexOutOfBoundsException();
}
if (head == null) {
head = new DLLNode(element);
tail = head;
}
else if (index == 0) {
DLLNode temp = new DLLNode(element);
temp.setNext(head);
head = temp;
} else {
DLLNode temp = head;
for (int i = 1; i < index; i++) {
temp = temp.getNext();
}
DLLNode newNode = new DLLNode(element);
newNode.setNext(temp.getNext());
newNode.setPrev(temp);
newNode.getNext().setPrev(newNode);
temp.setNext(newNode);
}
size ++;
}
Node class for my DoublyLinkedlist:
public class DLLNode<E> {
private DLLNode<E> next;
private DLLNode<E> prev;
private E element;
public DLLNode(E element){
this.element=element;
prev=null;
next=null;
}
public DLLNode(E element, DLLNode prev, DLLNode next) {
this.element=element;
this.prev=prev;
this.next=next;
}
public E getData(){
return element;
}
public void setData(E element){
this.element=element;
}
public DLLNode getPrev(){
return prev;
}
public DLLNode getNext(){
return next;
}
public void setPrev(DLLNode where){
prev=where;
}
public void setNext(DLLNode where){
next=where;
}}
It doesn't work for three reasons that I can see:
In the if (index == 0) block, you never set the prev value of the head node.
In the else block, you never check if you're at the end of the list, so you'll get a NullPointerException if you are.
In both if (index == 0) block and else block, you don't set tail if the new node is the last node.
On a side note: You're using raw generics all over. Never use DLLNode without a < immediately after it. Basically, change all DLLNode to DLLNode<E>.
Here's how you test your own code.
Add this method to DLLNode:
void verifyIntegrity() {
if (this.prev != null && this.prev.next != this)
throw new AssertionError("prev.next is corrupt");
if (this.next != null && this.next.prev != this)
throw new AssertionError("next.prev is corrupt");
}
Add this method to DoubleLinkedList:
void verifyIntegrity() {
int count = 0;
DLLNode<E> last = this.head;
for (DLLNode<E> node = this.head; node != null; count++, last = node, node = node.getNext())
node.verifyIntegrity();
if (this.tail != last)
throw new AssertionError("tail is corrupt");
if (this.size != count)
throw new AssertionError("size is corrupt");
}
Now test you code like this:
DoubleLinkedList<Integer> listHead = new DoubleLinkedList<>();
DoubleLinkedList<Integer> listTail = new DoubleLinkedList<>();
DoubleLinkedList<Integer> listMid = new DoubleLinkedList<>();
listHead.verifyIntegrity();
listTail.verifyIntegrity();
listMid.verifyIntegrity();
for (int i = 0; i < 10; i++) {
listHead.add(0, i);
listTail.add(i, i);
listMid.add(i / 2, i);
listHead.verifyIntegrity();
listTail.verifyIntegrity();
listMid.verifyIntegrity();
}
Related
I have implemented the following java implementation of a linked list
public class LinkedListFromScratch {
private Node head;
private static int size;
public LinkedListFromScratch() {
this.head = null;
this.size = 0;
}
public boolean isEmpty() {
return head == null;
}
public static int getSize() {return size;}
void addToTail(int data) {
Node newNode = new Node(data);
//if list is empty, make new node the head.
if (isEmpty()) {
this.head = newNode;
size++;
return;
}
Node itterHead = head;
while (itterHead.next != null) {
itterHead = itterHead.next;
}
itterHead.next = newNode;
size++;
}
void addAtIndex(int index, int data) {
if(index < 0 || index > this.size )
throw new IllegalArgumentException("Index you entered is out of bounds");
Node newNode = new Node (data);
if(isEmpty()) {
this.head = newNode;
size++;
return;
}
//locate the obj at index and one before it
//newnode.next = obj
//prevnode.next = newnode
Node current = this.head;
Node previous = null;
for ( int i = 0; i < index; i++){
previous = current;
current = current.next;
}
previous.next = newNode;
newNode.next = current;
size++;
}
void printList() {
if (isEmpty())
System.out.print("[]");
Node itterHead = this.head;
System.out.print("[ ");
while (itterHead != null) {
System.out.print(itterHead.d + " ");
itterHead = itterHead.next;
}
System.out.print("]");
System.out.println();
}
class Node {
int d;
Node next;
Node(int d) {
this.d = d;
this.next = null;
}
}
}
The issue here is with the addAtIndex (int index, int data) method. When I try to insert a value at index zero, it throws a null pointer exception. It makes sense because the for loop never gets executed and "previous" will always be null in the index = 0 scenario. The insertion works fine for index > 0. what is the best way to handle this case?
you need to check if the index is zero, this means you have new head to the list
add this code before the for-loop
if (index == 0){
newNode.next = head;
head = newNode;
size++;
return;
}
I have some problems when implementing a remove(int index) method that is supposed to remove an element at a specified index in a list. My first problem is how to shift any subsequent elements to the left and subtract one from their indices. I tried
Node.getmNextNode() = tempNode.getmNextNode().getmNextNode();
but it is not correct. My second problem is how to return the element previously at the specified index in the end.
public E remove(int index) throws IndexOutOfBoundsException {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
} else if (index == 0) {
remove(0);
} else {
Node<E> tempNode = head;
for (int i = 0; i < index - 1; i++) {
tempNode = tempNode.getmNextNode();
}
Node.getmNextNode() = tempNode.getmNextNode().getmNextNode();
size--;
}
return ;
}
My Node class:
public class Node<E> {
private E mElement;
private Node<E> mNextNode;
Node(E data) {
this.setmElement(data);
}
public E getmElement() {
return this.mElement;
}
public void setmElement(E element) {
this.mElement = element;
}
public Node<E> getmNextNode()
{
return this.mNextNode;
}
public void setmNextNode(Node<E> node)
{
this.mNextNode = node;
}}
You could try like this:
tempNode.setmNextNode(tempNode.getmNextNode().getmNextNode());
I am implementing a cyclic DoublyLinkedList data structure. Like a singly
linked list, nodes in a doubly linked list have a reference to the next node, but unlike a singly linked list, nodes in a doubly linked list also have a reference to the previous node. Additionally, because the list is "cyclic", the "next" reference in the last node in the list points to the first node in the list, and the "prev" reference in the first node in the list points to the last node in the list.
I am having trouble with my remove method with some size usage. It's the message I'm getting when I run my tests.
Here's my code:
public class DoublyLinkedList<E>
{
private Node first;
private int size;
#SuppressWarnings("unchecked")
public void add(E value)
{
if (first == null)
{
first = new Node(value, null, null);
first.next = first;
first.prev = first;
}
else
{
first.prev.next = new Node(value, first, first.prev);
first.prev = first.prev.next;
}
size++;
}
private class Node<E>
{
private E data;
private Node next;
private Node prev;
public Node(E data, Node next, Node prev)
{
this.data = data;
this.next = next;
this.prev = prev;
}
}
#SuppressWarnings("unchecked")
public void add(int index, E value)
{
if (first.data == null)
{
throw new IndexOutOfBoundsException();
} else if (index == 0)
{
first = new Node(value, first.next, first.prev);
}
else
{
Node current = first;
for (int i = 0; i < index - 1; i++)
{
current = current.next;
}
current.next = new Node(value, current.next, current.prev);
}
}
Here is the method I need help with.
The remove method should remove the element at the specified index in the list. Be sure to address the case in which the list is empty and/or the removed element is the first in the list. If the index parameter is invalid, an IndexOutOfBoundsException should be thrown.
#SuppressWarnings("unchecked")
public void remove(int index)
{
if (first.data == null)
{
throw new IndexOutOfBoundsException();
}
else if (index == 0)
{
first = first.next;
}
else
{
Node current = first.next;
for (int i = 0; i < index - 1; i++)
{
current = current.next;
}--size;
current.next = current.next.next;
}
}
Here is the rest of the code. The get method is incorrect, but I asked that in a different question.
public E get(int index)
{
if(index >= size)
{
}
return null;
//return first.data;
}
#SuppressWarnings("unchecked")
public int indexOf(E value)
{
int index = 0;
Node current = first;
while (current != current.next)
{
if (current.data.equals(value))
{
return index;
}
index++;
current = current.next;
}
return index;
}
public boolean isEmpty()
{
if (size == 0)
{
return true;
}
else
{
return false;
}
}
public int size()
{
return size;
}
This was not easy at all, however I did find the answer to my question. This is a cyclic doubly linked list. Here it is:
#SuppressWarnings("unchecked")
public void remove(int index)
{
if(index < 0 || index > size)
{
throw new IndexOutOfBoundsException();
}
Node n = first;
for(int i = 0; i < index; i++)
{
n = n.next;
}
// n points to node to remove
n.prev.next = n.next;
n.next.prev = n.prev;
if (index == 0)
{
if(size == 1)
{
first = null;
}
else
{
first = first.next;
}
}
size--;
}
I am implementing a cyclic DoublyLinkedList data structure. Like a singly linked list, nodes in a doubly linked list have a reference to the next node, but unlike a singly linked list, nodes in a doubly linked list also have a reference to the previous node.
Additionally, because the list is "cyclic", the "next" reference in the last node in the list points to the first node in the list, and the "prev" reference in the first node in the list points to the last node in the list.
I need help starting my get method, I've been looking around and I couldn't find anything that could help me since I am working with a Generic Type . I need to return E and every other examples show me it with int as an example. Here is my code:
public class DoublyLinkedList<E>
{
private Node first;
private int size;
#SuppressWarnings("unchecked")
public void add(E value)
{
if (first == null)
{
first = new Node(value, null, null);
first.next = first;
first.prev = first;
}
else
{
first.prev.next = new Node(value, first, first.prev);
first.prev = first.prev.next;
}
size++;
}
private class Node<E>
{
private E data;
private Node next;
private Node prev;
public Node(E data, Node next, Node prev)
{
this.data = data;
this.next = next;
this.prev = prev;
}
}
#SuppressWarnings("unchecked")
public void add(int index, E value)
{
if (first.data == null)
{
throw new IndexOutOfBoundsException();
} else if (index == 0)
{
first = new Node(value, first.next, first.prev);
}
else
{
Node current = first;
for (int i = 0; i < index - 1; i++)
{
current = current.next;
}
current.next = new Node(value, current.next, current.prev);
}
}
#SuppressWarnings("unchecked")
public void remove(int index)
{
if (first.data == null)
{
throw new IndexOutOfBoundsException();
}
else if (index == 0)
{
first = first.next;
}
else
{
Node current = first.next;
for (int i = 0; i < index - 1; i++)
{
current = current.next;
}
current.next = current.next.next;
}
size--;
}
I can't think of a way to get started on this, but basically what this method should do is return the element at the specified index in the list. If the index parameter is invalid, an IndexOutOfBoundsException should be thrown.
public E get(int index)
{
}
Also, my remove method isn't accurate, but I'll figure that one out myself, I just need help with my get method.
I figured it out, I'm just shocked that I wasn't getting any responses for this question. Either way, I am going to write some comments so it kind of guides future viewers who are struggling with this.
#SuppressWarnings("unchecked")
public E get(int index)
{
if(index < 0) //The index needs to be above 0.
{
throw new IndexOutOfBoundsException();
}
if(index > size) //Since we're going to run a for loop, we need to make sure the index doesn't go past the size of the list.
{
throw new IndexOutOfBoundsException();
}
Node current = first; //We want to create another node to perform this method.
for (int i = 0; i < index; i++) //We are going to set i to 0 and loop around this for loop until we reach the index.
{
current = current.next;
}
return (E) current.data; //Since we are working with generics, we need to return a type E, and it needs to be in parenthesis so it gets that object.
}
Another problem I sort of had was that in my Node Class I had the in there, when I could've moved on without it. Lets update it to be
private class Node
{
private E data;
private Node next;
private Node prev;
public Node(E data, Node next, Node prev)
{
this.data = data;
this.next = next;
this.prev = prev;
}
}
And now my getMethod() will be as follows:
#SuppressWarnings("unchecked")
public E get(int index)
{
if(index < 0)
{
throw new IndexOutOfBoundsException();
}
if(index > size)
{
throw new IndexOutOfBoundsException();
}
Node current = first;
for (int i = 0; i < index; i++)
{
current = current.next;
}
return current.data;
}
You can also use a hash map, and you will get the data on constant time
public T get(int position){
Node<T> node = map.get(position);
T dat = node.getData();
return dat;
}
I understand the concept of LinkedList, and single/doubly linked list. I, however don't understand how to implement it to my code?
I have to convert this singly list to a doubly linked list:
public class MyLinkedList<E> extends MyAbstractList<E> {
private Node<E> head, tail;
/** Create a default list */
public MyLinkedList() {
}
/** Create a list from an array of objects */
public MyLinkedList(E[] objects) {
super(objects);
}
/** Return the head element in the list */
public E getFirst() {
if (size == 0) {
return null;
}
else {
return head.element;
}
}
/** Return the last element in the list */
public E getLast() {
if (size == 0) {
return null;
}
else {
return tail.element;
}
}
/** Add an element to the beginning of the list */
public void addFirst(E e) {
Node<E> newNode = new Node<E>(e); // Create a new node
newNode.next = head; // link the new node with the head
head = newNode; // head points to the new node
size++; // Increase list size
if (tail == null) // the new node is the only node in list
tail = head;
}
/** Add an element to the end of the list */
public void addLast(E e) {
Node<E> newNode = new Node<E>(e); // Create a new for element e
if (tail == null) {
head = tail = newNode; // The new node is the only node in list
}
else {
tail.next = newNode; // Link the new with the last node
tail = tail.next; // tail now points to the last node
}
size++; // Increase size
}
#Override /** Add a new element at the specified index
* in this list. The index of the head element is 0 */
public void add(int index, E e) {
if (index == 0) {
addFirst(e);
}
else if (index >= size) {
addLast(e);
}
else {
Node<E> current = head;
for (int i = 1; i < index; i++) {
current = current.next;
}
Node<E> temp = current.next;
current.next = new Node<E>(e);
(current.next).next = temp;
size++;
}
}
/** Remove the head node and
* return the object that is contained in the removed node. */
public E removeFirst() {
if (size == 0) {
return null;
}
else {
Node<E> temp = head;
head = head.next;
size--;
if (head == null) {
tail = null;
}
return temp.element;
}
}
/** Remove the last node and
* return the object that is contained in the removed node. */
public E removeLast() {
if (size == 0) {
return null;
}
else if (size == 1) {
Node<E> temp = head;
head = tail = null;
size = 0;
return temp.element;
}
else {
Node<E> current = head;
for (int i = 0; i < size - 2; i++) {
current = current.next;
}
Node<E> temp = tail;
tail = current;
tail.next = null;
size--;
return temp.element;
}
}
#Override /** Remove the element at the specified position in this
* list. Return the element that was removed from the list. */
public E remove(int index) {
if (index < 0 || index >= size) {
return null;
}
else if (index == 0) {
return removeFirst();
}
else if (index == size - 1) {
return removeLast();
}
else {
Node<E> previous = head;
for (int i = 1; i < index; i++) {
previous = previous.next;
}
Node<E> current = previous.next;
previous.next = current.next;
size--;
return current.element;
}
}
#Override /** Override toString() to return elements in the list */
public String toString() {
StringBuilder result = new StringBuilder("[");
Node<E> current = head;
for (int i = 0; i < size; i++) {
result.append(current.element);
current = current.next;
if (current != null) {
result.append(", "); // Separate two elements with a comma
}
else {
result.append("]"); // Insert the closing ] in the string
}
}
return result.toString();
}
#Override /** Clear the list */
public void clear() {
size = 0;
head = tail = null;
}
#Override /** Return true if this list contains the element e */
public boolean contains(E e) {
System.out.println("Implementation left as an exercise");
return true;
}
#Override /** Return the element at the specified index */
public E get(int index) {
System.out.println("Implementation left as an exercise");
return null;
}
#Override /** Return the index of the head matching element in
* this list. Return -1 if no match. */
public int indexOf(E e) {
System.out.println("Implementation left as an exercise");
return 0;
}
#Override /** Return the index of the last matching element in
* this list. Return -1 if no match. */
public int lastIndexOf(E e) {
System.out.println("Implementation left as an exercise");
return 0;
}
#Override /** Replace the element at the specified position
* in this list with the specified element. */
public E set(int index, E e) {
System.out.println("Implementation left as an exercise");
return null;
}
#Override /** Override iterator() defined in Iterable */
public java.util.Iterator<E> iterator() {
return new LinkedListIterator();
}
private void checkIndex(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException
("Index: " + index + ", Size: " + size);
}
private class LinkedListIterator
implements java.util.Iterator<E> {
private Node<E> current = head; // Current index
#Override
public boolean hasNext() {
return (current != null);
}
#Override
public E next() {
E e = current.element;
current = current.next;
return e;
}
#Override
public void remove() {
System.out.println("Implementation left as an exercise");
}
}
private static class Node<E> {
E element;
Node<E> next;
public Node(E element) {
this.element = element;
}
}
}
So I understand to add a previous pointer, I have to do this:
private static class Node<E> {
E element;
Node<E> next;
Node<E> previous;
public Node(E element) {
this.element = element;
}
Now, I am confused - how do I actually implement the necessary methods for the doubly linked list? I am not asking for someone to do the entire assignment, but can someone show me an example of a worked out method?
I also couldn't understand, how does the program know I am referencing the previous element when I do Node<E> previous?
I also dont understand, just because I do "Node previous", how does the program know I am referencing the previous element?
The program doesn't know that you are referencing the previous element. You, the programmer, need to maintain (keep up-to-date and correct) that reference.
Every node will also have a reference to the previous node as it has reference to the next node.
Change the data structure of Node to add an extra Node reference to previous node. So now when someone calls the add(E e) method you will add that element at the tail and set the previous reference of e (which will be the new tail node) to the older tail node (which was the tail before add was called).
So in this way you are managing two references to previous and next nodes.
Also add a new method to your double link list class pervious() and related ones.