Java的单向链表和双向链表的写法

单向链表:

拥有后指针next

 

简单单向链表:

import java.util.Iterator;

public class UnidirectionalLinkedList<E> {

    //首节点
	private Node<E> first;
    //尾节点
	private Node<E> last;
    //链表长度
	private int size;
	
    //静态内部类Node,节点类
	public static class Node<E>{
		E item;
		Node<E> next;
		
		public Node(E item, Node<E> next) {
			this.item = item;
			this.next = next;
		}
	}
	
}

 

代码添加节点方法和遍历链表方法的代码:

import java.util.Iterator;

public class UnidirectionalLinkedList<E> {

    //首节点
	private Node<E> first;
    //尾节点
	private Node<E> last;
    //链表长度
	private int size;
	
    
    //添加节点方法
	public void add(E e){
		
		Node<E> node = new Node<>(e, null);
		
		if(first == null){
			first = node;
		}else{
			last.next = node;
		}
		last = node;
		size++;
	}
	
    //迭代器
	public Iterator<E> iterator(){
		return new Itr();
	}
    
    
	//实现链表的遍历
	public class Itr implements Iterator<E>{

		private int cursor;
		private Node<E> node = first;
		
		@Override
		public boolean hasNext() {
			return cursor != size;
		}

		@Override
		public E next() {
            //node节点的内容
			E item = node.item;
            //node的下一个节点
			node = node.next;
			cursor++;
			return item;
		}
		
	}
	
    //静态内部类Node,节点类
	public static class Node<E>{
		E item;
		Node<E> next;
		
		public Node(E item, Node<E> next) {
			this.item = item;
			this.next = next;
		}
	}
	
}

 

使用:

public static void main(String[] args) {
		
	UnidirectionalLinkedList<String> list = new UnidirectionalLinkedList<>();
		
		list.add("aaa");
		list.add("bbb");
		list.add("ccc");
		list.add("ddd");
		list.add("eee");
		
		Iterator<String> it = list.iterator();
		while(it.hasNext()){
			String element = it.next();
			System.out.println(element);
		}
		
	}

 
 

双向链表:

拥有前指针 prev 和后指针next

简单的双向链表:

import java.util.Iterator;

public class BidirectionalLinkedList<E> {

        //首节点
        private Node<E> first;
        //尾节点
        private Node<E> last;
        //链表长度
        private int size;
    
		//静态内部类Node,节点类
        public static class Node<E>{
            //前指针
            Node<E> prev;
            //节点内容
            E item;
            //后指针
            Node<E> next;

            //有参构造
            public Node(Node<E> prev,E item, Node<E> next) {
                this.prev = prev;
                this.item = item;
                this.next = next;
            }
        }
	
}

 

代码添加节点方法和遍历链表方法的代码:

import java.util.Iterator;

public class BidirectionalLinkedList<E> {

        //首节点
        private Node<E> first;
        //尾节点
        private Node<E> last;
        //链表长度
        private int size;
    
    
        public void add(E e){

            Node<E> l = last;

            Node<E> node = new Node<>(l,e, null);

            if(first == null){
                first = node;
            }else{
                last.next = node;

            }
            last = node;
            size++;
        }


        public Node<E> getLast() {
            return last;
        }

        public Iterator<E> iterator(){
            return new Itr();
        }

        public class Itr implements Iterator<E>{

            private int cursor;
            private Node<E> node = first;

            @Override
            public boolean hasNext() {
                return cursor != size;
            }

            @Override
            public E next() {
                E item = node.item;
                node = node.next;
                cursor++;
                return item;
            }
        }

    
    	//静态内部类Node,节点类
        public static class Node<E>{
    		//前指针
            Node<E> prev;
            //节点内容
            E item;
            //后指针
            Node<E> next;

            //有参构造
            public Node(Node<E> prev,E item, Node<E> next) {
                this.prev = prev;
                this.item = item;
                this.next = next;
            }
        }
	
}

 

使用:

public static void main(String[] args) {
		
	BidirectionalLinkedList<String> list = new BidirectionalLinkedList<>();
		
		list.add("aaa");
		list.add("bbb");
		list.add("ccc");
		list.add("ddd");
		list.add("eee");
		
		//正序遍历
		Iterator<String> it = list.iterator();
		while(it.hasNext()){
			String element = it.next();
			System.out.println(element);
		}
		
		System.out.println("-----------------------");

		//倒序遍历
		Node<String> node = list.getLast();
		while(node != null){
			System.out.println(node.item);
			node = node.prev;
		}
		
	}
  • 29
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值