I am beginning to study Java. As an assigment I have to implement a doubly linked list with given interfaces which i included in the code. My method insertAtTheEnd()
seems not to work properly, since I have the value null
on elements after inserting several ones. I checked similar questions on that topic and tried to apply the answers to my problem, but could not get any further. So any help is appreciated how i can setup this method to make it work. Thanks!
public interface IValueElement
{
public String getName();
public void setName(String paramName);
public int getValue();
public void setValue(int paramValue);
}
public interface IListElement
{
public IValueElement getValueElement();
public void setValueElement(IValueElement value);
public IListElement getPredecessor();
public void setPredecessor(IListElement predecessor);
public IListElement getSuccessor();
public void setSuccessor(IListElement successor);
}
public interface IList
{
public IListElement getHead();
public void insertAtTheEnd(IValueElement value);
//...
}
public class List implements IList
{
public List()
{
if (head == null)
{
head = new ListElement(null);
}
else
{
return;
}
}
private IListElement head;
public IListElement getHead()
{
return head;
}
public void insertAtTheEnd(IValueElement value)
{
if (head.getSuccessor() != null)
{
IListElement l = head;
while (l.getSuccessor() != null)
l = l.getSuccessor();
IListElement q = new ListElement(value);
l.setPredecessor(q);
}
else
{
IListElement q = new ListElement(value);
q.setPredecessor(head);
q.setSuccessor(null);
head.setSuccessor(q);
head.setPredecessor(q);
}
}
}
Additionally here are my implementations of ValueElement and ListElement:
//ListElement.java
public class ListElement implements IListElement
{
public ListElement(IValueElement value)
{
this.valueElement = checkValueElementAttribute(value);
}
private IValueElement checkValueElementAttribute(IValueElement value)
{
return (value == null) ? new ValueElement(null, 0) : value;
}
private IValueElement valueElement;
public IValueElement getValueElement()
{
return this.valueElement;
}
public void setValueElement(IValueElement value)
{
if (value != null)
{
this.valueElement = value;
}
}
private IListElement predecessor;
public IListElement getPredecessor()
{
return this.predecessor;
}
public void setPredecessor(IListElement predecessor)
{
this.predecessor = predecessor;
}
private IListElement successor;
public IListElement getSuccessor()
{
return this.successor;
}
public void setSuccessor(IListElement successor)
{
this.successor = successor;
}
}
// ValueElement.java
public class ValueElement implements IValueElement
{
private String name;
public String getName()
{
return this.name;
}
public void setName(String paramName)
{
if (paramName != null)
{
this.name = paramName;
}
}
public ValueElement(String name, int value)
{
if (name == null || name.equals(""))
{
name = "default";
}
else
{
this.name = name;
}
this.value = value;
}
private int value;
public int getValue()
{
return this.value;
}
public void setValue(int paramValue)
{
if (paramValue != 0)
{
this.value = paramValue;
}
}
public String toString()
{
return "Name: " + this.name + " - Value: " + this.value;
}
}
please provide the complete code so that we can run that and help you as of now we can only think of these problems:
1) you might not have called setValueElement on those nodes 2)your else block contains setting of both predecessor and successor to the same element so its a definite wrong line of code
shouldn't this be your code
public void insertAtTheEnd(IValueElement value)
{
if (head.getSuccessor() != null)
{
IListElement l = head;
while (l.getSuccessor() != null)
l = l.getSuccessor();
IListElement q = new ListElement(value);
l.setSuccessor(q);
}
else
{
IListElement q = new ListElement(value);
q.setPredecessor(head);
q.setSuccessor(null);
head.setSuccessor(q);
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments