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());
Related
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();
}
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--;
}
On my count method, i am trying to compare the object being sent with the object calling it, and I have no idea why i can not cal o.get(i) to get the value for Object o being passed. It tells me the method cannot be found even though i can call get(i) normally. Any ideas how i can get the value for the object getting passed so i can compare them?
public class GenericLinkedList<E> implements List<E> {
private class Node<E>{
private E data;
private Node<E> next;
}
private E data;
private Node<E> head = null;
private int size = 0;
private Node<E> nodeAt(int index){
Node<E> curNode = head;
int curIndex = 0;
while(curIndex < index){
curNode = curNode.next;
curIndex++;
}
return curNode;
}
#Override
public E get(int index) {
return nodeAt(index).data;
}
#Override
public void add(E value) {
Node<E> node = new Node<E>();
node.data = value;
if(size == 0){
head = node;
}else{
Node<E> curNode = nodeAt(size - 1);
curNode.next = node;
}
size++;
}
public void add(int index, E value){
Node<E> node = new Node<E>();
node.data = value;
if(size == 0){
head = node;
}else if(index == 0){
node.next = head;
head = node;
}else{
Node<E> curNode = nodeAt(index - 1);
node.next = curNode.next;
curNode.next = node;
}
size++;
}
#Override
public void remove(int index) {
if(index == 0){
head = head.next;
}else{
Node<E> curNode = nodeAt(index - 1);
curNode.next = curNode.next.next;
}
size--;
}
#Override
public void set(int index, E value) {
nodeAt(index).data = value;
}
public String toString(){
String s = "";
Node<E> curNode = head;
s += curNode.data +" -> ";
while(curNode.next != null){
curNode = curNode.next;
s += curNode.data +" -> ";
}
s += "null";
return s;
}
public void clear() {
for (int i = 0; i < size; i++)
set(i, null);
size = 0;
}
#Override
public void removeAll(E o)//Clears out the array object by setting everything to null
{
for(int i = 0; i < this.size; i++)
{
}
}
#Override
public int count(Object o)
{
System.out.println(o);
int count = 0;
for(int i = 0; i< size;i++)
{
if(get(i) == o.get(i))
{
count++;
}
}
return count;
/*int count = 0;
for(int i = 0;i<this.size;i++)
{
for(int j = 0;i<size;j++)
{
}
}
return count;*/
}
public void reverse()
{
int x = size;
for(int i = 0; i < this.size; i++)
{
E temp = this.get(x);
this.set(x, get(i));
this.set(i, temp);
x++;
}
}
public Object subList(int beginIndex, int endIndex)
{
int j = 0;
GenericLinkedList<E> LinkedList = new GenericLinkedList<E>();
if(beginIndex == endIndex)
return LinkedList;
else if(beginIndex > endIndex||endIndex>size||beginIndex<0)
{
return null;
}
else
{
for(int i = beginIndex; i <= endIndex;i++)
{
LinkedList.add(get(i));
j++;
}
}
return LinkedList;
}
public static void main(String[] args) {
GenericLinkedList<Integer> myList = new GenericLinkedList<Integer>();
myList.add(4);
myList.add(7);
myList.add(123123);
System.out.println(myList.get(2));
GenericLinkedList<Integer> myList2 = new GenericLinkedList<Integer>();
myList2.add(4);
myList2.add(7);
myList2.add(8);
myList2.add(3,999);
System.out.println(myList2);
System.out.println(myList.count(myList2));
//System.out.println(myList2);
// myList2.clear();//Testing our clear class
// System.out.println(myList2);
//System.out.println(myList2.subList(1, 3));//Testing our clear class
// System.out.println(myList2);
}
}
You have a problem when trying to call o.get() because it is an object of the base type Object which has no method get defined. To get the proper result you have two possible solutions....
The short solution is to type cast.
((GenericLinkedList<Object>)o).get(i)
Or a more easily readable solution is to change the signature of your count method:
public int count(GenericLinkList<E> list) {
...
}
Change your count method to public int GenericLinkedList(Object o)
If you use Object as the type of the parameter, you can only call the methods which are available on Object.
I'm writing this code to implement a stack as a linked list but I keep getting an error when I compile this part of the code and can't figure out why
I definitely know that it shouldn't be giving me this error since there are enough brackets and none of them are out of place I hope
The Error:
C:\Users\Michelle\Desktop\ITB\Semester 5\Data Structures & Algorithms\Assignment\ListReference.java:108: error: reached end of file while parsing
}
^
1 error
My Code:
public class ListReference implements StackInterface {
private Node top;
//private Object item;
private int NumItems;
public ListReference() {
NumItems = 0;
top = new Node(null);
}
public boolean isEmpty() {
return NumItems == 0;
}
public int size() {
return NumItems;
}
/*public Node find(int index) {
Node curr = tail;
for(int skip = 1; skip < index; skip++) {
curr.getNext();
}
return curr;
}*/
public Object get(int index) {
if(index <= 0)
return null;
Node ListReference = top.getNext();
for(int i=1; i < index; i++) {
if (ListReference.getNext() == null)
return null;
ListReference = ListReference.getData();
}
}
public void add(int index, Object item) {
Node ListReferenceTemp = new Node(item);
Node ListRefCurr = top;
while(ListRefCurr.getNext() != null) {
ListRefCurr = ListRefCurr.getNext();
}
ListRefCurr.setNext(ListReferenceTemp);
NumItems++;
}
public void add(Object data) {
Node ListReferenceTemp = new Node(item);
Node ListRefernce = top;
for(int i=1; i < index && ListReference.getNext() != null; i++) {
ListReference = ListReference.getNext();
}
ListReferenceTemp.setNext(ListReference.getNext());
ListReference.setNext(ListReferenceTemp);
NumItems++;
}
// removes the element at the specified position in this list.
public boolean remove(int index) {
// if the index is out of range, exit
if (index < 1 || index > size())
return false;
Node ListRefCurr = top;
for (int i = 1; i < index; i++) {
if (ListRefCurr.getNext() == null) {
return false;
}
ListRefCurr = ListRefCurr.getNext();
}
ListRefCurr.setNext(ListRefCurr.getNext().getNext());
NumItems--; // decrement the number of elements variable
return true;
}
public String toString() {
Node ListReference = top.getNext();
String output = "";
while (ListREference != null) {
output += "[" + ListRefCurr.getData().toString() + "]";
ListRefCurr = ListRefCurr.getNext();
}
return output;
}
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.