链表的操作
1.修改指定索引结点的数据
修改节点数据的本质是根据索引查找对应的节点然后对该节点进行修改
//内部类方法
public boolean setNode(int index,T newdata){
if (foot++==index){
this.data=newdata;
}else {
if (this.nextNode==null){
return false;
}else {
return this.nextNode.setNode(index,newdata);
}
}
return false;
}
//外部类方法
public boolean set(int index,T data){
foot = 0;
if (index>count||index<0){
return false;
}
if (this.root==null){
return false;
}
return this.root.setNode(index,data);
}
2.在指定位置插入结点
//内部类方法
public void addNode(int index,Node<T> previousNode,Node<T> newNode){
if (foot++==index){
newNode.nextNode = this;
previousNode.nextNode = newNode;
}else {
this.nextNode.addNode(index,this,newNode);
}
}
//外部类方法
public void add(int index,T data){
Node<T> newNode = new Node<>(data);
if (index==0){
//让之前的结点成为新节点的下一个结点
newNode.nextNode = this.root;
this.root = newNode;
}else {
this.root.addNode(index, this.root,newNode);
}
foot=0;
}
在外部类方法中:先是判断第一个结点的内容
内部类方法中,对当前结点和下一个结点进行判断,并利用递归算法,对下一节点进行同样的操作
综合代码
package lianbiao;
public class Link<T> {
private Node<T> root;//链表的根节点
private int foot;//表示链表结点的索引
private int count;//对添加数据进行计数
private class Node<T>{
private T data;
private Node nextNode;
public Node() {
}
public Node(T data) {
this.data = data;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node getNextNode() {
return nextNode;
}
public void setNextNode(Node nextNode) {
this.nextNode = nextNode;
}
/**
* 添加结点
*/
public void addNode(Node<T> newNode){
if (this.getNextNode()==null){
this.setNextNode(newNode);
}else {
this.getNextNode().addNode(newNode);
}
}
/**
* 输出结点数据
*/
public void printNode(){
System.out.print(this.data+" ");
if (this.nextNode!=null){
this.nextNode.printNode();
}
}
/**
* 判断是否包含某个数据
*/
public boolean containsNode(T data){
if (this.data.equals(data)){
return true;
}else {
if (this.nextNode==null){
return false;
}else {
return this.nextNode.containsNode(data);
}
}
}
/**
* 根据索引取得结点的数据,返回类型为泛型T
* index 传入的参数,表述要查找的索引
* 如果有数据返回指定结点的数据,没有就返回null
*/
public T getNodedata(int index){
//从0开始判断是否是对应的索引
if(foot++==index){
return this.data;
}else if (this.nextNode==null){
return null;
}else {
return (T)this.nextNode.getNodedata(index);
}
}
/**
* 修改某个结点的数据
* 给定索引 index,以及newdata
*/
public boolean setNode(int index,T newdata){
if (foot++==index){
this.data=newdata;
}else {
if (this.nextNode==null){
return false;
}else {
return this.nextNode.setNode(index,newdata);
}
}
return false;
}
/**
* 在指定位置添加结点,index 前结点,新结点
*/
public void addNode(int index,Node<T> previousNode,Node<T> newNode){
if (foot++==index){
newNode.nextNode = this;
previousNode.nextNode = newNode;
}else {
this.nextNode.addNode(index,this,newNode);
}
}
}
//在外部类定义主题能够访问的方法,从而调用内部类的方法
//添加结点
public void add(T data){
Node<T> newNode = new Node<>(data);//创建结点对象
if (this.root==null){
this.root = newNode;
}else {
this.root.addNode(newNode);
}
count++;
}
//输出数据
public void print(){
if (this.root == null){
return;
}else {
this.root.printNode();
}
}
//判断结点的长度,或者结点的个数
public int size(){
return this.count;
}
//判断结点是否为空
public boolean isEmpty(){
return this.count==0;
}
//判断结点中是否包含某数据
public boolean contains(T data){
if (this.root==null){
return false;
}else {
return this.root.containsNode(data);
}
}
//根据所以查找数据
public T get(int index){
if (this.root==null){
return null;
}if (index<0||index>count){
return null;
}
this.foot=0;//重新初始化下标,因为在此查询的时候还是从第一个结点开始查的,当此时foot已经发生改变,并不是第一个索引0
return this.root.getNodedata(index);
}
//修改数据,先找到指定索引,在进行修改
public boolean set(int index,T data){
foot = 0;
if (index>count||index<0){
return false;
}
if (this.root==null){
return false;
}
return this.root.setNode(index,data);
}
/**
* 指定位置添加结点
*/
public void add(int index,T data){
Node<T> newNode = new Node<>(data);
if (index==0){
//让之前的结点成为新节点的下一个结点
newNode.nextNode = this.root;
this.root = newNode;
}else {
this.root.addNode(index, this.root,newNode);
}
foot=0;
}
}
package lianbiao;
public class Test2 {
public static void main(String[] args) {
Link<String> link = new Link<>();
//添加数据
link.add("火车头");
link.add("1车厢");
link.add("2车厢");
link.add("3车厢");
link.add("4车厢");
link.add("车尾");
// System.out.println("修改前");
// link.print();
// link.set(0,"和谐号");
// System.out.println("修改后");
// link.print();
System.out.print("插入前: ");
link.print();
System.out.println();
link.add(1,"餐车");
link.add(2,"卖票车");
System.out.print("插入后: ");
link.print();
}
}