标签:nta ++ turn 查找 == oid else current data
/**
 * 链表是一种常见的数据结构,其实一种线性的数据结构
 * 对于数组来说链表的插入会更快速,但是数组的查找会更快
**/
public class T15 {
	public static void main(String []ages) {
		NodeManger nm = new NodeManger();
		nm.Add(5);
		nm.Add(4);
		nm.Add(3);
		nm.Add(2);
		nm.Add(1);
		nm.Del(6);
		nm.Updata(5, 6);
		System.out.println(nm.Find(1));
		nm.Insert(1, 9);
		nm.Print();
		
	}
}
class NodeManger{
	private Node rootNode;//根节点
	private int currentIndex = 0;//节点的序号
	//添加节点
	public  void Add(int data) {
		if(rootNode==null) {
			rootNode = new Node(data);
		}
		else {
			rootNode.nodeAdd(data);
		}
	}
	//删除节点
	public void Del(int data) {
		if(rootNode==null) return;
		if(rootNode.getData()==data) {
			rootNode = rootNode.nextNode;
		}
		else {
			rootNode.delNode(data);
		}
	}
	//打印所有
	public void Print() {
		if(rootNode!=null) {
			System.out.print(rootNode.getData()+"->");
			rootNode.printAll();
			System.out.println();
		}
		
	}
	//查找节点是否存在
	public boolean Find(int data) {
		if(rootNode==null) return false;
		if(rootNode.getData()==data) {
			return true;
		}
		else {
			return rootNode.findNode(data);
		}
	}
	//修改节点
	public boolean Updata(int oldData,int newData) {
			if(rootNode==null) {
				return false;
			}
			if(rootNode.data==oldData) {
				rootNode.setData(newData);
				return true;
			}
			else {
				return rootNode.updataNode(oldData, newData);
			}
	}
	//插入节点
	public void Insert(int index,int data) {
		if(index<0) return;
		currentIndex = 0;
		if(index==currentIndex) {
			Node newNode = new Node(data);
			newNode.nextNode = rootNode;
			rootNode = newNode;
		}else {
			rootNode.insertNod(index, data);
		}
	}
	private class Node{
		private int data;
		private Node nextNode;
		public Node(int data) {
			this.data = data;
		}
		public void setData(int data) {
			this.data = data;
		}
		public int getData() {
			return this.data;
		}
		//添加节点
		public  void nodeAdd(int data) {
			if(this.nextNode==null) {
				this.nextNode = new Node(data);
			}
			else {
				this.nextNode.nodeAdd(data);
			}
		}
		//删除节点
		public void delNode(int data) {
			if(this.nextNode!=null) {
				if(this.nextNode.data==data) {
					this.nextNode = this.nextNode.nextNode;
				}
				else {
					this.nextNode.delNode(data);
				}
			}
		}
		//打印所有
		public void printAll() {
			if(this.nextNode!=null) {
				System.out.print(this.nextNode.getData()+"->");
				this.nextNode.printAll();
			}
		}
		//查找节点是否存在
		public boolean findNode(int data) {
			if(this.nextNode!=null) {
				if(this.nextNode.data==data) {
					return true;
				}
				else {
					return this.nextNode.findNode(data);
				}
			}
			return false;
		}
		//修改节点
		public boolean updataNode(int oldData,int newData) {
			if(this.nextNode==null) {
				return false;
			}
			else if(this.nextNode.data==oldData) {
				this.nextNode.data = newData;
				return true;
			}
			else {
				return this.nextNode.updataNode(oldData, newData);
			}
		}
		//插入节点
		public void insertNod(int index,int data) {
				currentIndex++;
				if(index==currentIndex) {
					Node newNode = new Node(data);
					newNode.nextNode = this.nextNode;
					this.nextNode = newNode;
				}else {
					this.nextNode.insertNod(index, data);
				}
		}
	}
}
标签:nta ++ turn 查找 == oid else current data
原文地址:https://www.cnblogs.com/Atul/p/10994211.html