双链接列表java反向函数错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了双链接列表java反向函数错误相关的知识,希望对你有一定的参考价值。
我的双链接列表有问题,因为控制台输出错误的尾部!我把它缩小到我认为在我的反向功能中的错误!如果有人可以看一看并给我一些关于我做错了什么的指导,或者我如何设置我的尾巴,以便报告正确的变量!非常感谢你!可能是一个简单的错误,我错过了正常!
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;
}
}
答案
从geeks获得了极客的代码,你只需要为你的变量名和节点的类名修改它。
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;
}
}
如果您有任何疑问,请随时提出。
以上是关于双链接列表java反向函数错误的主要内容,如果未能解决你的问题,请参考以下文章