首页 > 代码库 > java对链表增删查改操作API
java对链表增删查改操作API
从c++方向搞点java副业。结果在静态链表这里绊了一跤。感谢巨人们的肩膀重见天日。
希望后来人少走些弯路。
废话少说,直接贴代码:
1.创建链表的节点类
public class Node {
private Node node;
private Object eleme;
public void setEleme(Object eleme) {
this.eleme = eleme;
}
public Node(){
this.eleme = null;node = null;
}
public Node(Object ele)
{
this.eleme = ele;node = null;
}
public Node(Object ele,Node nd)
{
this.eleme = ele;
this.node = nd;
}
public Object getEleme()
{
return this.eleme;
}
public Node getNextNode()
{
return this.node;
}
public void setNextNode(Node next)
{
this.node = next;
}
}
2.编写对外接口API
方便扩展用到泛型,实际调用的时候记得重写第三方类的equals(),toString()2个方法。
public class Link<E> {
private Node headNode;
private int nodeSize = 0;
private static String SPLITE_STRING = "-";
public Node getHeadNode() {
return headNode;
}
public int getSize()
{
return this.nodeSize;
}
/*
* 在末尾增加节点
*/
public void addNode(E ele) {
if(headNode !=null)
{
headNode = new Node(ele, headNode);
}else {
headNode = new Node(ele);
}
nodeSize++;
}
/*
* 在确定位置增加节点
*/
public void addNodeIndex(E ele,int index)
{
if(index==0){//如果从0位置插入,即从头部插入
addNode(ele);
}else {
if(index > nodeSize)
{
System.err.println("插入索引超过链表长度");
}else{
int count = 0;
for(Node nd = headNode;nd!=null;nd=nd.getNextNode()){
count++;
if(count == index)
{
Node nextNode =new Node(ele, nd.getNextNode());
nd.setNextNode(nextNode);
nodeSize++;
}
}
}
}
}
/*
* 删除确定位置index节点
*/
public void deleteNodeIndex(int index)
{
if(index >= nodeSize)
System.err.println("要删除的索引超过链表长度");
else{
if(index == 0)
{
headNode = headNode.getNextNode();
nodeSize--;
}else {
int count = 0;
Node nd = headNode;
while(++count < index){
nd = nd.getNextNode();
}
if(index == nodeSize - 1){
nd.setNextNode(null);
}else{
nd.setNextNode(nd.getNextNode().getNextNode());
}
}
}
}
/*
* 删除匹配元素的节点
* boolean toEnd:控制只删除第一匹配项还是全部匹配项
*/
public void deleteNodeEleme(E ele, boolean toEnd){
int count = 0;
for(Node ndNode = headNode;ndNode!=null;ndNode=ndNode.getNextNode(),count++){
if(ele.equals(ndNode.getEleme())){
deleteNodeIndex(count);
if(toEnd)continue;
else break;
}
}
}
/*
* 删除所有节点
*/
public void deleteAll()
{
headNode = null;
}
/*
* 查找指定index索引的节点
*/
public Node searchNodeIndex(int index){
if(index > nodeSize)
{
System.err.println("插入索引超过链表长度");
}else{
int count = 0;
Node nd = headNode;
while(++count < index){
nd = nd.getNextNode();
}
if(index == 0)return nd;
else return nd.getNextNode();
}
return null;
}
/*
* 查找相等元素的第一个节点
*/
public Node searchNodeEleme(E ele){
int count = 0;
for(Node ndNode = headNode;ndNode!=null;ndNode=ndNode.getNextNode(),count++){
if(ele.equals(ndNode.getEleme())){
return searchNodeIndex(count);
}
}
return null;
}
/*
* 得到包含该元素的节点集合
*/
@SuppressWarnings("finally")
public Node[] searchNodeEleme(E ele, Node[] node){
try{
int count = 0;int idex = 0;
for(Node ndNode = headNode;ndNode!=null;ndNode=ndNode.getNextNode(),count++){
if(ele.equals(ndNode.getEleme())){
node[idex++] = searchNodeIndex(count);
}
}
}catch(Exception ex)
{
node = null;
ex.printStackTrace();
}finally{
return node;
}
}
/*
* 改确定索引index的值为newEle
*/
public boolean modifyNodeIndex(int index,E newEle)
{
if(index > nodeSize)
{
System.err.println("修改索引超过链表长度");
}else{
int count = 0;
Node nd = headNode;
while(count++ < index){
nd = nd.getNextNode();
}
nd.setEleme(newEle);
}
return true;
}
/*
* 将匹配的旧元素oldEle改成新的newEle
* Boolean toEnd : 控制修改的范围是第一个匹配项还是全部匹配项
*/
public boolean modifyNodeEleme(E oldEle,E newEle,Boolean toEnd)
{
int count = 0;
for(Node ndNode = headNode;ndNode!=null;ndNode=ndNode.getNextNode(),count++){
if(oldEle.equals(ndNode.getEleme())){
modifyNodeIndex(count,newEle);
if(toEnd) continue;
else break;
}
}
return true;
}
/*
* 输出全部节点的元素
*/
public String printNode()
{
Node nextNode = headNode;
String dataString = "";
while(nextNode!=null){
dataString +=nextNode.getEleme().toString();
dataString += SPLITE_STRING;
nextNode = nextNode.getNextNode();
}
return dataString;
}
}
3.编写测试用例
public class Main {
public static void main(String[] args)
{
@SuppressWarnings("rawtypes")
Link<String> link = new Link<String>();
String string = "";
link.addNode("A");
link.addNode("B");
link.addNode("A");
link.addNode("C");
link.addNodeIndex("KK", 2);
System.out.println( link.printNode());
System.out.println(link.getSize());
link.addNode("D");
link.addNodeIndex("GG", 0);
System.out.println( link.printNode());
link.addNode("A");
System.out.println(link.getSize());
link.addNode("E");
System.out.println( link.printNode());
/*查找测试
Node nnd = link.searchNodeIndex(0);
System.out.println(nnd.getEleme().toString());
Node nnd1 = link.searchNodeEleme("GG");
System.out.println(nnd1.getEleme().toString());
Node[] nodes = new Node[5];
nodes = link.searchNodeEleme("A", nodes);
for(int i = 0;nodes[i] != null;i++)
{
System.out.println(i + nodes[i].getEleme().toString());
}
*/
/*测试修改
link.modifyNodeIndex(0, "A");
Node nnd = link.searchNodeIndex(0);
System.out.println(nnd.getEleme().toString());
//link.modifyNodeEleme("A", "66666", true);
//System.out.println(link.printNode());
link.modifyNodeEleme("A", "66666", false);
System.out.println(link.printNode());
*/
/*测试删除
link.deleteNodeIndex(link.getSize() -1);
System.out.println(link.printNode());
link.deleteNodeEleme("E",true);
System.out.println(link.printNode());
link.deleteAll();
System.out.println(link.printNode());
*/
}
-----------------------------------------完--------------------------------------------
求大神点评,指正不足和错误,推荐一下java的进阶书籍,视频,网站,社区等。拜谢,跪谢。
本文出自 “诗意生活” 博客,请务必保留此出处http://whish.blog.51cto.com/10679902/1896378
java对链表增删查改操作API