java - 单链表转双链表

标签 java linked-list

我的一个 friend 希望我把他的代码转换成双向链表,尽管我对此一点也不熟悉。我查过双向链表,但我无法通过他的代码判断如何处理它。我不是一个大师级程序员。你有什么建议吗?

import java.util.Collection;

import java.util.List;


class SinglyLinkedList<E> implements List<E> {



private class SinglyLinkedListNode<T> {



    T data;

    SinglyLinkedListNode<T> next;



    public SinglyLinkedListNode() {

        this(null, null);

    }



    public SinglyLinkedListNode(T data) {

        this(data, null);

    }



    public SinglyLinkedListNode(T d, SinglyLinkedListNode<T> n) {

        data = d;

        next = n;

    }



    public boolean equals(Object o) {

        if (data != null && o != null) {

            return data.equals(((SinglyLinkedListNode) o).data);

        } else {

            return (data == null && o == null);

        }

    }

}

private SinglyLinkedListNode<E> list, last;

private int size;



public SinglyLinkedList() {

    clear();

}



public void clear() {

    list = last = null;

    size = 0;

}



public boolean contains(Object o) {

    SinglyLinkedListNode<E> t = list;

    while (t != null) {

        if (t.data == null) {

            if (o == null) {

                return true;

            }

        } else if (t.data.equals(o)) {

            return true;

        }

        t = t.next;

    }

    return false;

}



public boolean add(E e) {

    SinglyLinkedListNode<E> n = new SinglyLinkedListNode<E>(e);

    if (isEmpty()) {

        list = last = n;

    } else {

        last = last.next = n;

    }

    size++;

    return true;

}



public void add(int index, E e) {

    int currSize = size();

    if (index < 0 || index > currSize) {

        throw new IndexOutOfBoundsException(

                "Index: " + index + ", Size: " + size());

    }

    if (isEmpty()) // index must == 0

    {

        list = last = new SinglyLinkedListNode<E>(e);

    } else {

        if (index == 0) {

            list = new SinglyLinkedListNode<E>(e, list);

        } else {

            SinglyLinkedListNode<E> n = list;

            for (int i = 0; i < index - 1; i++) {

                n = n.next;

            }

            n.next = new SinglyLinkedListNode<E>(e, n.next);

            if (index == currSize) {

                last = n.next;

            }

        }

    }

    size++;

}



public boolean equals(SinglyLinkedList<E> e) {


   SinglyLinkedListNode<E> e1 = list, e2 = e.list;

    try {

        for (int i = 1; i <= size(); i++) {

            if (!e1.equals(e2)) {

                return false;

            }

            e1 = e1.next;

            e2 = e2.next;

        }

    } catch (NullPointerException ex) {

        return false;

    }

    return true;

}



public E get(int index) {

    if (index < 0 || index >= size()) {

        throw new IndexOutOfBoundsException(

                "Index: " + index + ", Size: " + size());

    }

    SinglyLinkedListNode<E> n = list;

    int i = 0;

    for (; i < index; i++) {

        n = n.next;

    }

    return n.data;

}



@SuppressWarnings("unchecked")

public int indexOf(Object o) {

    SinglyLinkedListNode<E> n = list;

    int i = 0;

    while (n != null) {

        if ((o == null

                ? (n.data == null)

                : (((E) o).equals(n.data)))) {

            return i;

        }

        n = n.next;

        i++;

    }

    return -1;

}



public boolean isEmpty() {

    return list == null;

}



public E remove(int index) {

    if (index < 0 || index >= size()) {

        throw new IndexOutOfBoundsException(

                "Index: " + index + ", Size: " + size());

    }

    SinglyLinkedListNode<E> n = list, prevNode = null;

    int i = 0;

    while (true) {

        if (index == i) {

            if (n == list) // removing first node

            {

                list = list.next;

            } else {

                prevNode.next = n.next;

            }

            if (n == last) {

                last = prevNode;

            }

            size--;

            return n.data;

        }

        prevNode = n;

        n = n.next;

        i++;

    }

}



@SuppressWarnings("unchecked")

public boolean remove(Object o) {

    SinglyLinkedListNode<E> n = list, prevNode = null;

    while (n != null) {

        if ((o == null

                ? (n.data == null)

                : (((E) o).equals(n.data)))) {

            if (n == list) //removing first node

            {

                list = list.next;

            } else {

                prevNode.next = n.next;

            }

            if (n == last) {

                last = prevNode;

            }

            size--;

            return true;

        }

        prevNode = n;

        n = n.next;

    }

    return false;

}



public int size() {

    return size;

}



public String toString() {

    String s = "((";

    SinglyLinkedListNode<E> t = list;

    if (t != null) {

        while (t.next != null) {

            s += t.data + ", ";

            t = t.next;

        }

        s += last.data;

    }

    return s + "))";

}

最佳答案

我不明白这个问题。如果这是作业,你应该这么说——社区规则!无论如何,快速解释一下:

链表是一种具有以下结构的结构,...好吧,结构:

DATA                      |--> DATA
REFERENCE TO NEXT ITEM ---|    REFERENCE TO NEXT ITEM ---...

“链”中的每个“链接”都包含一些数据,以及定位链中下一个项目的方法。正如您所说,这是一个单链表。

双向链表是一种非常相似的结构,只是链中的每个链接都包含定位下一项的方法和定位前一项的方法。如果您需要能够双向遍历列表,则需要这种结构。

|-> DATA                      |--> DATA
|   REFERENCE TO NEXT ITEM ---|    REFERENCE TO NEXT ITEM ---...
---------------------------------- REFERENCE TO PREV ITEM

哦,这些“图画”太丑了。您可以使用 Google 查询来查找双向链表,并获得更好的信息,但转念一想,哦,好吧。

关于java - 单链表转双链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4208579/

相关文章:

java - 传递给函数的列表可以被 Java 中的另一个线程修改吗?

java - Hibernate开启session两次,需要transaction

java - 将图像添加到 JOptionPane

c - 制作一个存储 char 数组元素的指针

将链表转换为队列(移动节点)

c++ - 读取整数并将其显示回 C++ 的简单链表

java - 当我在手机上运行此代码时强制关闭

java - 通过给定的正则表达式生成一个新字符串

java - 将字符按字母顺序放入节点中 (Java)

python - Python 中的链表 - Append、Index、Insert 和 Pop 函数。不确定代码/错误