1

在我的秋季课程开始之前,我一直在努力观看 YouTube 视频以了解链表,但我不确定如何继续迭代以下链表。“节点”类来自一系列视频(同一作者),但“主要”方法是我编写的。我是否以不合逻辑的方式设计链表(当然,假设一个人希望使用预定义的 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

4 回答 4

4

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

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();
}
于 2013-08-15T09:34:36.463 回答
2

也许这会很有用:

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());
    }
}
于 2013-08-15T09:35:02.390 回答
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();  }
}

祝你好运!!

于 2013-08-15T11:01:14.497 回答
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;
    }

}

}

于 2016-11-04T20:06:08.623 回答