首页 > 代码库 > LinkList的增删查改

LinkList的增删查改

package com.lanyuan.log;public class Test1 {    private TNode head;    private int size;    private TNode last;    public int size() {        return this.size + 1;    }    public Test1() {        head = last = null;    }    public void add(int val) {        TNode node = new TNode(val);        if (head == null) {            head = last = node;            last.next = head;        } else {            last.next = node;            last = last.next;            last.next = head;        }        size++;    }    public int deleteLast() {        if (head == null) {            throw new RuntimeException("it‘s not allow");        }        TNode node = head.next;        int val = last.val;        for (int i = 0; node != null && node.next != last; i++) {            node = node.next;        }        size--;        last.next = node;        last = last.next;        last.next = head;        return val;    }    public boolean updaetLast(int val) {        if (head == null) {            throw new RuntimeException("it‘s not allow");        }        last.val = val;        return last != null;    }    public int findLast() {        if (head == null) {            throw new RuntimeException("it‘s not allow");        }        return last.val;    }    public String toString() {        if (head == null) {            return "TLink[]";        }        StringBuilder sb = new StringBuilder();        TNode node = head.next;        sb.append("TLink[" + head.val);        for (int i = 0; i < size && node != head; i++) {            sb.append("," + node.val);            node = node.next;        }        sb.append("]");        return sb.toString();    }}class TNode {    // 为了简单这里存储int类型数据,可以改成泛型    public int val;    public TNode next;    public TNode() {        super();    }    public TNode(int val) {        this.val = val;    }}

 

LinkList的增删查改