//设置节点 public class LinkNode { private Object obj;// 节点内数据对象 private LinkNode child;// 对下一个节点的引用 private LinkNode parent;// 对下一个节点的引用 public LinkNode(Object obj) { this.obj = obj; } public void setObject(Object obj) { this.obj = obj; } public Object getObject() { return obj; } public void setChild(LinkNode child){ this.child =child ; } public LinkNode getChild() { return child; } public void setParent(LinkNode parent){ this.parent =parent ; } public LinkNode getParent() { return parent; } } //链表实现队列 public class NodeQueue { public static LinkNode front = null;// 第一个节点 public static LinkNode last = null;// 最后一个节点 /** * 程序入口 * * @param args */ public static void main(String[] args) { NodeQueue nq = new NodeQueue(); nq.add("aa"); nq.add("bb"); nq.add("cc"); nq.ModiFyLinkNode(0, "更改"); // nq.inserLinkNode(2, "插入"); nq.deleteLinkNode(2); nq.printLinkNode(front); } // 添加节点 public void add(Object object) { // 创建一个新的节点 LinkNode node = new LinkNode(object); if (null == front) {// 如果链表为空 front = node; last = front; } else { last.setChild(node); node.setParent(last); last = node; } } // 根据索引删除元素 public void deleteLinkNode(int index) { if (this.getLinkLenght() < index || index < 0) { throw new RuntimeException("下标越界:" + index + "size:" + this.getLinkLenght()); } else { // 得到当前索引位置的节点 LinkNode node = this.getLinkNode(index); // 得到父节点 LinkNode f_node = node.getParent(); // 得到子节点 LinkNode c_node = node.getChild(); if (f_node == null) { front = c_node; } else if (c_node == null) { f_node.setChild(null); } else { f_node.setChild(c_node); c_node.setParent(f_node); } } } // 根据索引和对象名修改节点 public void ModiFyLinkNode(int index, Object object) { if (this.getLinkLenght() < index || index < 0) { throw new RuntimeException("下标越界 : " + index + " size: " + this.getLinkLenght()); } LinkNode node = this.getLinkNode(index); node.setObject(object); } // 获取节点对象 public LinkNode getLinkNode(int index) { if (this.getLinkLenght() < index || index < 0) { throw new RuntimeException("下标越界:" + index + "size:" + this.getLinkLenght()); } else { int num = 0; LinkNode node = front; while (num != index) { node = node.getChild(); num++; } return node; } } // 插入节点 public void inserLinkNode(int index, Object object) { if (this.getLinkLenght() < index || index < 0) { throw new RuntimeException("下标越界" + index + "size:" + this.getLinkLenght()); } else { // 创建新节点 LinkNode newnode = new LinkNode(object); // 得到当前节点 LinkNode node = this.getLinkNode(index); if (index == 0) {// 如果链表没有节点 front = newnode; } else if (index == this.getLinkLenght()) {// 如果为最后一个节点 node.setChild(newnode); newnode.setParent(node); } else { // 得到父节点 LinkNode fnode = node.getParent(); fnode.setChild(newnode); newnode.setChild(node); } // 新的引用关系 newnode.setParent(front); node.setParent(newnode); } } // 获取节点数,返回链表长度 public int getLinkLenght() { int count = 0; if (front == null) { return count; } LinkNode node = front.getChild(); while (null != node) { count++; node = node.getChild(); } return count + 1; } // 遍历链表方法 public void printLinkNode(LinkNode root) { if (null != root) { Object data = root.getObject(); System.out.println(data); LinkNode temp = root.getChild(); printLinkNode(temp); } } }
链表实现对列
猜你喜欢
转载自zhengaihua.iteye.com/blog/2342186
今日推荐
周排行