简体   繁体   English

单链表到双链表

[英]Singly linked list to doubly linked list

A friend of mine wants me to convert his code into a doubly linked list, though I'm not familiar with it at all. 我的一个朋友要我将他的代码转换为双向链接列表,尽管我一点都不熟悉。 I've looked up doubly linked lists but I can't tell by his code what to do with it. 我查找了双链表,但我无法通过他的代码来判断该怎么做。 I'm not a master programmer. 我不是程序员。 Do you have any suggestions? 你有什么建议吗?

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 + "))";

}

I don't understand the problem. 我不明白这个问题。 If this is homework, you should say so -- community rules! 如果这是家庭作业,则应这样说-社区规则! A quick explanation, regardless: 快速说明,无论:

A linked list is a structure with the following,... well, structure: 链表是一个结构,其结构如下:

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

Each "link" in the "chain" contains some data, and a way to locate the next item in the chain. “链”中的每个“链接”都包含一些数据,以及在链中定位下一项的方法。 That's a singly linked list, as you said. 正如您所说,这是一个单链表。

A doubly linked list is a very similar structure, only each link in the chain contains both a way of locating the next item, and a way of locating the previous item. 双链表的结构非常相似,只有链中的每个链接都包含查找下一项的方法和查找上一项的方法。 If you need to be able to walk the list both ways, you'll need this kind of structure. 如果您需要能够双向浏览列表,则需要这种结构。

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

Ooookay the "drawings" are hideous. 糟糕,“图纸”是可怕的。 You can look up what a doubly linked list is with a Google query and get better information, on second thought, but oh well. 您可以通过Google查询来查找双向链接列表,并重新获得更好的信息,但是很好。

Take a look at this other question on SO. 看看关于SO的另一个问题 It should help you to better understand the doubly linked list. 它应该可以帮助您更好地理解双向链表。

每个节点都需要一个next还有一个previous占位符,它是一个双向链表。

LinkedList in Java is a doubly-linked list. Java中的LinkedList是一个双向链接列表。 Why would you want to create one by yourself? 您为什么要自己创建一个?

sounds like homework. 听起来像功课。 This will get you started. 这将使您入门。

http://en.wikipedia.org/wiki/Doubly_linked_list http://en.wikipedia.org/wiki/Doubly_linked_list

basically, in a singly linked list each node has a pointer to the next node. 基本上,在单链列表中,每个节点都有一个指向下一个节点的指针。 In a doubly linked list there are pointers to both next and previous. 在双向链接列表中,有指向下一个和上一个的指针。 Beware how the pointers work at the beginning and the end of the list. 当心指针在列表的开头和结尾如何工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM