Java language used to realize the operation of the linked list data structure

 

/ **
* the list is a common data structure, in fact, a linear data structure
* For the array is inserted in the list will be faster, but will quickly find an array
* /
public class T15 {
public static void main (String [] AGEs) {
NodeManger new new NodeManger nm = ();
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 ();

}
}

NodeManger {class
Private the rootNode the Node; // root
private int currentIndex = 0; // number of nodes
// add nodes
public void the Add (int Data) {
IF (the rootNode == null) {
the rootNode the Node new new = (Data);
}
the else {
rootNode.nodeAdd (Data);
}
}
// remove nodes
public void Del (int Data) {
IF (the rootNode == null) return;
IF (rootNode.getData () == Data) {
the rootNode = rootNode.nextNode ;
}
the else {
rootNode.delNode (Data);
}
}
// print all
public void the Print () {
IF (the rootNode = null!) {
of System.out.print (rootNode.getData () + "->");
the 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);
}
}
}
}

Guess you like

Origin www.cnblogs.com/Atul/p/10994211.html