标签:count index 单向 Cell null pointer 指针
啥话都不说了,上代码吧,代码中我加了注释。
package cn.lu.linklist; //单向链表的类 public class LinkList<T> { private Cell<T> first;//链头定义 //新的空链的无参构造 public LinkList() { first=new Cell<T>(); } //带参构造的方法(尾部添加法) public LinkList(T[] data,int n){ first = new Cell<T>(); // 头结点 Cell<T> end = first; Cell<T> temp = null; //将形参传入链中 for (T t : data) { temp = new Cell<T>();//必须new,否则temp.next的最终不为空 temp.setData(t); end.setNext(temp); end = temp; } } //在链中指定位置插入数据 public boolean insert(T t, int index) { int count = 0; Cell<T> pointer = first;//定义指针变量 Cell<T> temp = null;//临时变量 //插入链的位置值不能小于1(默认链已经有了头节点) if (index < 1) throw new ArrayIndexOutOfBoundsException(); //指针移动到指定的位置(index位置的前一个,默认值是从0开始的) while (pointer != null && count < index - 1) { pointer = pointer.getNext(); count++; } //指针位置为空时就报错(插入的位置超出链长) if (pointer == null) throw new ArrayIndexOutOfBoundsException(); //在指定位置插入数据 else { temp = new Cell<T>(); temp.setData(t);//需要插入的数据赋值给临时变量 temp.setNext(pointer.getNext());//把要插入数据的next指向index位置的数据 pointer.setNext(temp);//把指针位置的next指向要插入的数据 } return true; } //在链中的指定位置删除数据 public Boolean remove(int index) { int count = 0; Cell<T> pointer = first; //定义指针变量 Cell<T> temp = null; //定义临时变量 //如果要删除链中的位置不能小于1 if (index < 1) throw new ArrayIndexOutOfBoundsException(); //指针移动到指定的位置(index位置的前一个,默认值是从0开始的) while (pointer != null && count < index - 1) { pointer = pointer.getNext(); count++; } //要删除的数据位置超过了链长度 if (pointer== null) throw new ArrayIndexOutOfBoundsException(); //删除数据的操作开始 else { temp = pointer.getNext();//临时变量赋值给要删除的数据 pointer.setNext(temp.getNext());//把当前指针数据的next指向要删除数据的下一个数据 } return true; } //替换指定位置的数据 public boolean replace(int index, T t) { int count = 0; Cell<T> pointer = first;//定义指针变量 //要替换链中的位置不能小于1 if (index < 1) throw new ArrayIndexOutOfBoundsException(); //指针移动到要替换的位置 while (pointer!= null && count < index) { pointer = pointer.getNext(); count++; } //判断当前指针位置不能超出链长度 if (pointer == null) throw new ArrayIndexOutOfBoundsException(); //替换数据开始操作 else{ //注意此时没有改变pointer的next值,只是将pointer的Data替换成t。 pointer.setData(t); } return true; } //获得指定位置的数据链值 public T get(int index) { int count = 0; Cell<T> pointer = first;//定义指针变量 //要替换链中的位置不能小于1 if (index < 1) throw new ArrayIndexOutOfBoundsException(); //指针移动到指定位置 while (pointer != null && count < index) { pointer = pointer.getNext(); count++; } //判断当前指针位置不能超出链长度 if (pointer == null) throw new ArrayIndexOutOfBoundsException(); else return pointer.getData(); } //判断数据链是否为空 public boolean isEmpty(){ if(first.getNext() == null) return true; return false; } //计算数据链长度 public int size() { int count = 0; Cell<T> pointer = first; //定义指针变量 while (pointer.getNext() != null) { pointer = pointer.getNext(); count++; } return count; } //链的toString方法 public String toString() { String str = "[";//起始符输出 Cell<T> pointer = first; //移动指针判断循环的条件 while (pointer!= null) { pointer = pointer.getNext();//移动指针 //指针位置不为能为空 if (pointer != null) { //当指针位置的下一个不为空时的输出。 if (pointer.getNext() != null){ str = str + pointer.getData() + ","; } //当指针位置的下一个为空时 else{ str = str + pointer.getData(); } } } str = str + "]";//结束符输出 return str; } }
标签:count,index,单向,Cell,null,pointer,指针 来源: https://www.cnblogs.com/shqnl/p/10988140.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。