首页 > 代码库 > 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的增删查改
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。