迭代Java中的链表?

时间:2013-08-15 09:09:22

标签: java oop data-structures

我一直在努力观看YouTube视频,以便在我的秋季课程开始之前理解链接列表,而我不确定如何继续迭代以下链接列表。 'node'类来自一系列视频(同一作者),但'main'方法是由我编写的。我是否以不合逻辑的方式设计链接列表(当然,假设 希望使用预定义的LinkedList类,因为教授希望我们每个人都编写自己的实现)?

class Node
{
    private String data;
    private Node next;

    public Node(String data, Node next)
    {
        this.data = data;
        this.next = next;
    }

    public String getData()
    {
        return data;
    }

    public Node getNext()
    {
        return next;
    }

    public void setData(String d)
    {
        data = d;
    }

    public void setNext(Node n)
    {
        next = n;
    }

    public static String getThird(Node list)
    {
        return list.getNext().getNext().getData();
    }

    public static void insertSecond(Node list, String s)
    {
        Node temp = new Node(s, list.getNext());
        list.setNext(temp);
    }

    public static int size(Node list)
    {
        int count = 0;

        while (list != null)
        {
            count++;
            list = list.getNext();
        }

        return count;
    }
}

public class LL2
{
    public static void main(String[] args)
    {
        Node n4 = new Node("Tom", null);
        Node n3 = new Node("Caitlin", n4);
        Node n2 = new Node("Bob", n3);
        Node n1 = new Node("Janet", n2);

    }
}

感谢您的帮助,

凯特琳

4 个答案:

答案 0 :(得分:3)

您的链接列表中存在一些缺陷,正如其他一些评论所述。但是你在那里有了一个良好的开端,它掌握了链表的想法并且看起来很实用。要回答关于如何循环链接列表的特定实现的基本问题,请执行此操作

Node currentNode = n1; // start at your first node
while(currentNode != null) {
    // do logic, for now lets print the value of the node
    System.out.println(currentNode.getData());
    // proceed to get the next node in the chain and continue on our loop
    currentNode = currentNode.getNext();
}

答案 1 :(得分:1)

也许这会有用:

static void iterate(Node head) {
    Node current = head;
    while (current != null) {
        System.out.println(current.getData());
        current = current.getNext();
    }
}

// or through recursion
static void iterateRecursive(Node head) {
    if (head != null) {
       System.out.println(head.getData());
       iterateRecursive(head.getNext());
    }
}

答案 2 :(得分:0)

class List {        
    Item head;

    class Item {        
        String value;  Item next;   
        Item ( String s ) { value = s; next = head; head = this; }
    }

    void print () {
        for( Item cursor = head; cursor != null; cursor = cursor.next ) 
            System.out.println ( cursor.value );
    }

    List () {
        Item one = new Item ( "one" );
        Item two = new Item ( "three" );
        Item three = new Item ( "Two" );
        Item four = new Item ( "four" );
    }
}

public class HomeWork {
    public static void main( String[] none ) {  new List().print();  }
}
祝你好运!!

答案 3 :(得分:0)

您可以让链接列表DS类实现'Iterable'接口并覆盖hasNext(),next()方法或创建内部类来为您完成。看看下面的实现:

public class SinglyLinkedList<T>{
private Node<T> head;
public SinglyLinkedList(){
    head = null;
}

public void addFirst(T item){
    head = new Node<T>(item, head);
}
public void addLast(T item){
    if(head == null){
        addFirst(item);
    }
    else{
        Node<T> temp = head;
        while(temp.next != null){
            temp = temp.next;
        }
        temp.next = new Node<T>(item, null);
    }
}

private static class Node<T>{
    private T data;
    private Node<T> next;
    public Node(T data, Node<T> next){
        this.data = data;
        this.next = next;
    }
}
private class LinkedListIterator implements Iterator<T>{
    private Node<T> nextNode;
    public LinkedListIterator(){
        nextNode = head;
    }

    @Override
    public boolean hasNext() {
        return (nextNode.next != null);
    }

    @Override
    public T next() {
        if(!hasNext()) throw new NoSuchElementException();
        T result = nextNode.data;
        nextNode = nextNode.next;
        return result;
    }

}

}