Я написал следующий код, чтобы быть дважды связанный список:Проблемы тестирования с двусвязному списков
Node Класс:
public class MyDoubleNode<AnyType> {
public AnyType data; //value of the node
public MyDoubleNode<AnyType> next; //points to the next node
public MyDoubleNode<AnyType> prev; //points to the previous node
public static void main(String[] args) {
}
}
интерфейс Класс:
public interface DoublyLinkedList<AnyType> {
public void insert(AnyType x);
public void delete(AnyType x);
public boolean contains(AnyType x);
public AnyType lookup(AnyType x);
public boolean isEmpty();
public void printList();
public void printListRev();
}
двусвязному Список Класс:
public class OwnDoublyLinkedList<E> implements DoublyLinkedList {
protected MyDoubleNode head;
protected MyDoubleNode tail;
protected MyDoubleNode front = null; //checks to see that front is the first node
public OwnDoublyLinkedList(){ //sets up constructor
head = new MyDoubleNode();
tail = new MyDoubleNode();
head.prev = null;
head.next = tail;
tail.prev = head;
tail.next = null;
}
@Override
public void insert(Object obj) { //inserts a new node
MyDoubleNode newNode = new MyDoubleNode();
newNode.data = obj; //sets a new node to what the object is
if (contains(obj) == false){ //if this is not a duplicate
if (head.prev == null){ //if this is the first node
head.data = newNode;
}
else {
(newNode.prev).next = newNode; //connects the node behind it
(newNode.next).prev = newNode; //connects the node in front of it
}
}
}
@Override
public void delete(Object x) {
if (contains(x) == true){ //if it is in the list
head.prev = head.next; //sets the previous head to the next head
tail.prev = tail.next; //sets the previous tail to the next tail
} //this cuts out the node from the list
}
@Override
public boolean contains(Object x) {
MyDoubleNode front = null; //checks to see that front is the first node
if (head.prev == null){ //sets it to the value of the first node
front = head;
}
while (front!= null){
if (x.equals(front.data)){ //if the object is in the list, it returns true
return true;
}
front = front.next;
}
return false; //if the object is not in the list, it returns false
}
@Override
public Object lookup(Object x) {
if (head.prev == null){ //sets it to the value of the first node
front = head;
}
while (front!= null){
if (x.equals(front.data)){ ////if the node equals the data were looking for, return the object
return front.data;
}
front = front.next;
}
return null; //if the object isnt in the list, return null
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return head.data == null;
}
public void printList() {
if (head.data == null){
System.out.println("null");//sets it to the value of the first node
}
else if (head.data != null){
System.out.println(head.data);
}
else if (tail.prev == null){
front = head;
front.data = head.data;
}
while (front!= null){
System.out.println((front.data).toString()); //prints the data until theres none left
front = front.next;
}
}
public void printListRev() {
MyDoubleNode front = null;
if (tail.next == null){ //if this is the last node, set back to the node
front = head;
}
while (head.prev != null){
System.out.println((front.data).toString()); //prints the data until there's none left, but backwards
front = front.prev;
}
}
}
Тест er class:
public class Lab5Tester {
public static void main (String [] args){
OwnDoublyLinkedList tester = new OwnDoublyLinkedList();
tester.insert(1);
tester.insert(3);
tester.printList(); //runtime of O(n)
tester.printListRev(); //runtime of O(n)
System.out.println(tester.contains(1));
System.out.println(tester.isEmpty());
}
}
Приносим извинения, если мой код немного бессмыслен. Я только что узнал дженерики и связанные списки и не совсем понял это. Когда я запускаю свой тестовый код, я получаю местоположение в памяти узла, а не данные на узле. Как я могу исправить это, чтобы он печатал числа, которые я добавил в список?
Override 'ToString()' в 'MyDoubleNode' – davidxxx
Не используйте сырые типы. 'MyDoubleNode head' должен быть' MyDoubleNode head'. 'OwnDoublyLinkedList tester' должен быть' OwnDoublyLinkedList тестер'. 'реализует DoublyLinkedList' должен быть' реализует DoublyLinkedList '. 'insert (Object obj)' должен быть 'insert (E obj)' и т. д. –
@davidxxx, но они отображаются только для печати 'data':' System.out.println ((front.data) .toString()) ' Никогда не вызывал 'MyDoubleNode :: toString' – weston