Java该怎么遍历链表数据结构

贴上代码, 我想问一下嵌套类ReverseArrayIterator中的next() 方法该怎样写,才能完成链表

 /**
 * 下压栈链表实现
 *
 * @author maji
 * @date 2018-7-14
 */
public class Stack<Item> implements Iterable<Item> {
    private Node first;
    private int N;

    private class Node {
        Item item;
        Node next;
    }

    public boolean isEmpty() {
        return first == null;
    }

    public int size() {
        return N;
    }

    public void push(Item item) {
        Node oldFirst = first;
        first = new Node();
        first.item = item;
        first.next = oldFirst;
        N++;
    }

    public Item pop() {
        Item item = first.item;
        first = first.next;
        N--;
        return item;
    }

    @Override
    public Iterator<Item> iterator() {
        return new ReverseArrayIterator();
    }

    private class ReverseArrayIterator implements Iterator<Item> {
        private int i = N;

        @Override
        public boolean hasNext() {
            return i > 0;
        }

        @Override
        public Item next() {

                for (Node x = first; x != null; x = x.next) {
                    return x.item;

                }


        }

        @Override
        public void remove() {
        }

    }

}

3个回答

 import java.util.*;

class Untitled {    

    class Stack<Item> implements Iterable<Item> {
        private Node first;
        private int N;

        private class Node {
            Item item;
            Node next;
        }

        public boolean isEmpty() {
            return first == null;
        }

        public int size() {
            return N;
        }

        public void push(Item item) {
            Node oldFirst = first;
            first = new Node();
            first.item = item;
            first.next = oldFirst;
            N++;
        }

        public Item pop() {
            Item item = first.item;
            first = first.next;
            N--;
            return item;
        }

        @Override
        public Iterator<Item> iterator() {
            return new ReverseArrayIterator();
        }

        private class ReverseArrayIterator implements Iterator<Item> {
            private int i = N;

            @Override
            public boolean hasNext() {
                return i > 0;
            }

            @Override
            public Item next() {
                i--;
                return pop();
            }

            @Override
            public void remove() {
            }

        }

    }

    public void foo()
    {
        Untitled.Stack<Integer> list = new Untitled.Stack<Integer>();
        list.push(new Integer(1));
        list.push(new Integer(2));
        list.push(new Integer(3));
        for (int i : list)
            System.out.println(i);
    }

    public static void main(String[] args) {
        new Untitled().foo();
    }
}


caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复majishushu: 看下面的代码
大约 2 年之前 回复
majishushu
灿夏 噢 懂了 谢谢啦
大约 2 年之前 回复
majishushu
灿夏 你用的foreach方式 遍历的 用iterator方式怎么遍历
大约 2 年之前 回复

3
2
1
sandbox> exited with status 0

https://tool.lu/coderunner/ 这里测试过了

     public void foo()
    {
        Untitled.Stack<Integer> list = new Untitled.Stack<Integer>();
        list.push(new Integer(1));
        list.push(new Integer(2));
        list.push(new Integer(3));
        Iterator<Integer> itl = list.iterator();
        while (itl.hasNext()) {
            System.out.println(itl.next());
        }
    }
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问