java 动态单向链表_Java动态单向链表

川长思鸟来 2022-11-09 14:27 321阅读 0赞

class link { class node { private String data; private node next; public node(String data) //设置节点信息 { this.data=data; } public void add(node newnode) //增加一个add操作 { if(this.next==null) { this.next=newnode; } else { this.next.add(newnode); //运用递归 } } public void print() //打印节点信息 { System.out.print(this.data+”\t”); if(this.next!=null) { this.next.print(); } } public boolean search(String data) { if(data.equals(this.data)) { return true; } else { if(this.next!=null) { return this.next.search(data); } else { return false; } } } public void delete(node previous,String data) { if(data.equals(this.data)) { previous.next=this.next; } else { if(this.next!=null) { this.next.delete(this, data); } } } }; private node root; public void addnode(String data) //增加根节点信息 { node newnode=new node(data); if(this.root==null) { this.root=newnode; } else { this.root.add(newnode); } } public void printnode() { if(this.root!=null) { this.root.print(); } } public boolean contains(String name) { return this.root.search(name); } public void deletenode(String data) { if(this.contains(data)) { if(this.root.data.equals(data)) { this.root=this.root.next; } else { this.root.next.delete(root,data); } } } }; public class test28 { public static void main(String args[]) { link l=new link(); l.addnode(“A”); l.addnode(“B”); l.addnode(“C”); l.addnode(“D”); l.addnode(“E”); l.printnode(); l.deletenode(“B”); l.deletenode(“C”); System.out.println(“”); l.printnode(); System.out.println(“”); System.out.println(“查询节点:”+l.contains(“E”)); } }

发表评论

表情:
评论列表 (有 0 条评论,321人围观)

还没有评论,来说两句吧...

相关阅读

    相关 单向

    单向链表也叫单链表,是链表中最简单的一种形式,它的每个节点包含两个域,一个信息域(元素域)和一个链接域。这个链接指向链表中的下一个节点,而最后一个节点的链接域则指向一个空值。

    相关 单向

    一:单向链表 单向链表中的节点包含数据域和next指针域。 ![20210306162342140.png][] 单向链表的增删查改代码实现 Hero

    相关 单向动态

    单向动态链表 什么是链表 链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结