ZBLOG

public class SinglyList extends Object { public Node head; public SinglyList() { this.head=new Node(); } public SinglyList(T[] values) { this(); Node rear=this.head; for(int i=0;i

以下是将单链表改为双链表的代码:

public class DoublyList<T> extends Object {
    public DNode<T> head;
    
    public DoublyList() {
        this.head = new DNode<>();
    }
    
    public DoublyList(T[] values) {
        this();
        DNode<T> rear = this.head;
        for (int i = 0; i < values.length; i++) {
            if (values[i] != null) {
                rear.next = new DNode<>(values[i], null, rear);
                rear = rear.next;
            }
        }
    }
    
    public boolean isEmpty() {
        return this.head.next == null;
    }
    
    public T get(int i) {
        DNode<T> p = this.head.next;
        for (int j = 0; p != null && j < i; j++) {
            p = p.next;
        }
        return (i >= 0 && p != null) ? p.data : null;
    }
    
    public void set(int i, T x) {
        if (x == null) throw new NullPointerException("x==null");
        
        DNode<T> p = this.head.next;
        for (int j = 0; p != null && j < i; j++) {
            p = p.next;
        }
        
        if (i >= 0 && p != null) {
            p.data = x;
        } else {
            throw new IndexOutOfBoundsException(i + "");
        }
    }

	public String toString() {
		String str=this.getClass().getName()+"(";
		for(DNode<T> p=this.head.next;p!=null;p=p.next)
			str +=p.data.toString()+(p.next!=null?",":"");
		return str+")";
	}
	
	public DNode<T> insert(int i,T x) {
		if(x==null)
			return null;
		
		DNode<T> front=this.head;
		for(int j=0;front.next!=null&&j<i;j++)
			front=front.next;
		
		DNode<T> newNode = new DNode<>(x, front.next, front);
		
		if (front.next != null) {
		    front.next.prev = newNode;
		}
		
		front.next = newNode;
		
		return newNode;
	}
	
	public DNode<T> insert(T x) {
        return insert(Integer.MAX_VALUE, x);
    }
    
    public T remove(int i) {
        DNode<T> front = this.head;

        for (int j = 0; front.next != null && j < i; j++) {
            front = front.next;
        }

        if (i >= 0 && front.next != null) {
            T x = front.next.data;

            if (front.next.next != null) {
                front.next.next.prev = front;
            }

            front.next = front.next.next;

            return x;
        }
        
        return null;
    }
    
    public void clear() {
        this.head.next = null;
    }
}

class DNode<T> {
    public T data;
    public DNode<T> next, prev;

    public DNode() {}

    public DNode(T data, DNode<T> next, DNode<T> prev) {
        this.data = data;
        this.prev = prev;
        this. next= next;
    }
}

这是一个简单的双链表实现,包含了插入、删除、获取等基本操作。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=8556

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?