用java实现单链表(菜鸟出征)
发布时间:2016-03-24 14:00:06
package code;class Node{ Node next; int data; public Node(int data) { this.data=data; } }class LinkList{ Node first; //头部 public ...
package code;class Node{ Node next; int data; public Node(int data) { this.data=data; } }class LinkList{ Node first; //头部 public LinkList() { this.first=null; } public void addNode(Node no) { no.next=first; first=no;//在头部添加 } public void delectNode() { Node n=first.next; first=null; first=n;//在头部删除 } //删除指定位置 public int Number() { int count=1; //查看有多少元素 Node nd=first; while(nd.next!=null) { nd=nd.next; count++; } return count; } public void delectExact(int n) { //删除指定位置 if(n>1) { int count=1; Node de=first; while(count<n-1) { de=de.next; count++; } de.next=de.next.next; } else first=first.next; } public void addExact(int n,Node nd) { if(n>1)//添加指定位置 { int count=1; Node de=first; while(count<n-1) { de=de.next; count++; } nd.next=de.next; de.next=nd; } else first=first.next; } public int findNode(int n) { int count=1;//查找一个数对应的位置 Node de=first; while(de.data!=n) { de=de.next; count++; if(de==null) { return -1; } } return count; } public void print() { Node no=first;//打印所有 while(no!=null) { System.out.println(no.data); no=no.next; } }}public class TextNode{ public static void main(String[] args) { LinkList ll=new LinkList(); ll.addNode(new Node(12)); ll.addNode(new Node(15)); ll.addNode(new Node(18)); ll.addNode(new Node(19)); ll.addNode(new Node(20)); /*System.out.println(ll.first.data); ll.delectNode(); System.out.println(ll.first.data);*/ System.out.println(ll.Number()); ll.delectExact(3); ll.addExact(3, new Node(100)); System.out.println(ll.Number());// ll.print(); System.out.println(ll.findNode(112)); }}
原标题:用java实现单链表(菜鸟出征)
关键词:JAVA
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。
移除库存:https://www.goluckyvip.com/tag/47484.html
移动cpm广告:https://www.goluckyvip.com/tag/47485.html
移动出行:https://www.goluckyvip.com/tag/47486.html
移动电商:https://www.goluckyvip.com/tag/47487.html
移动广告:https://www.goluckyvip.com/tag/47488.html
移动广告流量观察白皮书:https://www.goluckyvip.com/tag/47489.html
从园岭新村到大梅沙海滨总站坐什么车:https://www.vstour.cn/a/363191.html
七月份适合去日本旅游吗 7月份去日本哪里好玩:https://www.vstour.cn/a/363192.html