majishushu
灿夏
2018-07-14 04:48
采纳率: 100%
浏览 2.6k
已采纳

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条回答 默认 最新

  • caozhy
    已采纳
     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

    3
    2
    1
    sandbox> exited with status 0

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

    点赞 评论
  • caozhy
         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());
            }
        }
    
    点赞 评论

相关推荐