我的双链接列表有问题,因为控制台输出的尾部错误!我将其范围缩小到我认为是我的反向功能中的错误!如果有人可以看看并给我一些有关我做错了什么或如何设置我的尾巴的指导,以便它报告正确的变量!非常感谢你!我可能像平时一样错过了一个简单的错误!
import java.util.*;
import java.lang.*;
public class DLList<E> implements List<E> {
//Your implementation
private DNode<E> head;
private DNode<E> tail;
private int size = 0;
public DLList(E item){
tail = head = new DNode<E>(item);
size++;
}
public DLList(){
head = tail = null;
size = 0;
}
/** Remove all contents from the list, so it is once again
empty. */
public void clear(){
head = tail = null;
size = 0;
}
/** Insert an element at the given location.
* allows you to insert after the tail
* @param item The element to be inserted.
*/
public void insert(int index, E item){
if(index < 0 || index > size){
throw new IndexOutOfBoundsException(Integer.toString(index));
}
if(index == 0) {
addFirst(item);
}
else if(index == size){
add(item);
}
else{
DNode<E> node = getNode(index -1);
addAfter(node, item);
}
}
public void addFirst(E item){
if(size == 0) {
tail = head = new DNode<E>(item);
}
else{
DNode<E> newNode = new DNode<E>(item, null, head);
head.setPrev(newNode);
head = newNode;
}
size++;
}
public void addAfter(DNode<E> node, E item){
DNode<E> newNode = new DNode<E>(item, node, node.getNext());
node.getNext().setPrev(newNode);
node.setNext(newNode);
size++;
}
/** Append an element at the end of the list.
* @param item The element to be appended.
*/
public void add(E item){
if(size == 0){
tail = head = new DNode<E>(item);
}
else{
DNode<E> newNode = new DNode<E>(item, tail, null);
tail.setNext(newNode);
tail = newNode;
}
size++;
}
/**
* Remove the element at the given location.
*/
public void remove(int index){
if(index < 0 || index > size){
throw new IndexOutOfBoundsException(Integer.toString(index));
}
if(index == 0) {
removeFirst();
}
else if(index == size){
removeLast();
}
else{
DNode<E> node = getNode(index -1);
removeAfter(node);
}
}
public void removeFirst(){
/*
if(head == null){
throw new NoSuchElementException("List is empty");
}
else if(head == tail){
head = tail = null;
}
else{
head= head.getNext();
head.prev(item) = null;
}
*/
//setPrev
DNode<E> temp = head;
head = head.getNext();
}
public void removeLast(){
/*
if(head == null) {
throw new NoSuchElementException("List is empty");
}
else if(head == tail){
//RemoveFirst code
}
else{
tail = tail.prev();
tail.getNext() = null;
}
size--;
*/
DNode<E> temp = tail;
tail = tail.getNext();
}
public void removeAfter(DNode<E> node){
/*
if(size == 0) {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
else if(head == tail){
heal = tail = null;
}
else{
head = head.getNext();
head.prev = null;
}
size--;
*/
DNode<E> temp = node.getNext();
//Make sure value is valid
if (temp != null)
{
node.setNext(temp.getNext());
size--;
}
}
public DNode<E> getNode(int index){
if(index < 0 || index > size){
throw new IndexOutOfBoundsException(Integer.toString(index));
}
DNode<E> node = head;
for(int i = 0; i < index; i++) {
node = node.getNext();
}
return node;
}
/**
* Get the element in the position to one step left.
* @return element in the node to the left of the node at the index,
* null if at the head.
*/
public E prev(int index){
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
DNode<E> node = getNode(index-1);
return node.getElement();
}
/** Get the element in the position one step right.
* @return the element in the node to the right of
* the node at the index, null if at the end.
*/
public E next(int index){
if (index < 0 || index >= size) {
throw new
IndexOutOfBoundsException(Integer.toString(index));
}
DNode<E> node = getNode(index+1);
return node.getElement();
}
/** @return The number of elements in the list. */
public int length(){
return size;
}
/** Turn the contents of the Nodes to a string in order from head to end.
* @return The String representation of the
* elements in the list from head to end.
*/
//Copy and past prev but change symbol!
public String toString(){
DNode<E> node = head;
String result = "";
while(node != null) {
result = result + node.getElement().toString();
if(node.getNext() != null){
result = result + " <==> ";
}
node = node.getNext();
}
return result;
}
/** Reverse the content of the list.
* if list is A => B => C it becomes C => B => A
*/
public void reverse(){
/*
head.prev = head.getNext();
head.getNext() = null;
while(head.prev != null){
head = head.prev;
DNode<E> temp = head.getNext();
head.getNext() = head.prev;
head.prev = temp;
}
*/
DNode<E> node = head;
if(node == null || node.getNext() == null){
return;
}
DNode<E> prev = node.getNext();
DNode<E> curr = prev.getNext();
prev.setNext(node);
node.setNext(null);
while (curr != null) {
DNode<E> next = curr.getNext();
curr.setNext(prev);
prev = curr;
curr = next;
}
head = prev;
}
public E setprev(int index){
if (index < 0 || index >= size) {
throw new
IndexOutOfBoundsException(Integer.toString(index));
}
DNode<E> node = getNode(index-1);
return node.getElement();
}
/** @return The element at given position. */
public E getValue(int index){
if (index < 0 || index >= size) {
throw new
IndexOutOfBoundsException(Integer.toString(index));
}
DNode<E> node = getNode(index);
return node.getElement();
}
/**inserts the given list after the given index
*Check if getNext is null
*if(temp != null){
(then set prev)
{
else{
set tail list last value
}
*/
public void insertList (DLList list, int index){
if (list.getHead() != null && (index >= 0 && index <size)){
DNode<E> head = list.getHead();
DNode<E> tail = list.getLast();
DNode<E> position = getNode(index);
DNode<E> temp= position.getNext();
position.setNext(head);
tail.setNext(temp);
size = list.length()+ size;
}
}
public DNode<E> getHead(){
return head;
}
public DNode<E> getLast(){
return tail;
}
}
答案 0 :(得分:0)
从极客那里获得此代码,您只需要为节点的变量名和类名对其进行修改即可。
void reverse() {
Node temp = null;
Node current = head;
/* swap next and prev for all nodes of
doubly linked list */
while (current != null) {
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
/* Before changing head, check for the cases like empty
list and list with only one node */
if (temp != null) {
head = temp.prev;
}
}
如果您有任何疑问,请随时提问。