1.链表定义
public class LNode
{
//1、单链表结点定义(链表=节点—>节点—>节点—>节点—>... 节点=数据域+节点域)
public int data;//数据域
public LNode nextLNode;//节点域
public LNode() {}
//构造头节点(不存储data)
public LNode(LNode nextNode) {
this.nextLNode=nextNode;
}
public LNode(int data, LNode nextLNode) {
this.data = data;
this.nextLNode = nextLNode;
}
}
2.链表创建
单链表的创建(创建后返回值一定为最初头节点,不然会丢失链表)
头插法
//(1)头插法创建(生成的链表是逆序的,速度快)
public LNode CreatLinkByHead(int[] datas) {
LNode lNode = new LNode(null);//头节点,头节点不存储信息
for(int i=0;i<datas.length;i++) {
LNode newLNode = new LNode(datas[i],null);//新节点
if(lNode.nextLNode!=null) {
newLNode.nextLNode=lNode.nextLNode; //将链表头节点后面的第一个节点挂在新节点后
}
lNode.nextLNode=newLNode;//新节点插入到头节点后面
}
return lNode;
}
尾插法
//(2)尾插法创建(生成的链表是顺序的)
public LNode CreatLinkByTail(int[] datas) {
LNode lNode=new LNode(null);//头节点,头节点不存储信息
LNode tempLNode=lNode;//指向头节点
for(int i=0;i<datas.length;i++) {
LNode newlNode=new LNode(datas[i], null);//新节点
while(tempLNode.nextLNode!=null) {//后移,遍历 寻找尾部
tempLNode=tempLNode.nextLNode;
}
tempLNode.nextLNode=newlNode; //插到尾部
}
return lNode;
}
3.链表的CRUD
//(1)查询节点个数
public int GetSize(LNode lNode) {
int count=0;
while (lNode.nextLNode!=null) {
count++;
lNode=lNode.nextLNode;
}
return count;
}
//(2)链表中查找某结点(位置找值)
public int GetValue(int index,LNode lNode) {
int size=GetSize(lNode);
int value=0;
if(index<=0||index>size) {
return -1;
}
for(int i=0;i<=size;i++) {
if(i<index) {
lNode=lNode.nextLNode;
}else if(i==index){
value=lNode.data;
i=size+1;
}
}
return value;
}
//(2)链表中查找某结点(值找位置,返回节点和index)
public Map<String, Object> GetLNodeAndIndex(int value,LNode lNode){
Map< String, Object> map=new HashMap<String, Object>();
int size = GetSize(lNode);
int index=1;
while(lNode.nextLNode.data!=value&&index<size) {
index++;
lNode=lNode.nextLNode;
}
if (lNode.nextLNode.data==value) {
map.put("根据值查到位置是:", index);
map.put("根据值查到前一个节点是:", lNode);
}else {
map.put("不存在", null);
}
return map;
}
//(3)修改某结点的数据域
public boolean UpdatLNode(int index,LNode lNode,int data) {
int size=GetSize(lNode);
if(index<=0||index>size) {
return false;
}
for(int i=0;i<=size;i++) {
if(i<index) {
lNode=lNode.nextLNode;
}else if(i==index){
lNode.data=data;
i=size+1;
}
}
return true;
}
//(4)往链表中插入结点
public boolean InsertLNode(int index,LNode lNode,int data) {
int size=GetSize(lNode);
if(index<=0||index>size) {
return false;
}
for(int i=0;i<=size;i++) {
if(i<index-1) {
lNode=lNode.nextLNode;
}else if(i==index-1){
LNode newLNode=new LNode(data, null);
newLNode.nextLNode=lNode.nextLNode;
lNode.nextLNode=newLNode;
i=size+1;
}
}
return true;
}
//(5)删除链表结点
public boolean DeleteLNode(int index,LNode lNode) {
int size=GetSize(lNode);
if(index<=0||index>size) {
return false;
}
for(int i=0;i<=size;i++) {
if(i<index-1) {
lNode=lNode.nextLNode;
}else if(i==index-1){
lNode.nextLNode=lNode.nextLNode.nextLNode;
i=size+1;
}
}
return true;
}
测试类
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
public class LNodeTest
{
private LNode linkNode=new LNode();
//打印单链表
public void printLink(LNode linkLNode) {
LNode lNode=linkLNode;//指向头节点
while(lNode.nextLNode!=null) {
System.out.print(lNode.nextLNode.data+" ");
lNode=lNode.nextLNode;//后移一位
}
System.out.println("");
}
@Test
public void TestForLNode() {
int[] arr= {5,4,6};
System.out.print("头节点构造单链表,结果反向:");
LNode creatLinkByHead = linkNode.CreatLinkByHead(arr);
printLink(creatLinkByHead);
System.out.print("尾节点构造单链表,结果正向:");
LNode creatLinkByTail = linkNode.CreatLinkByTail(arr);
printLink(creatLinkByTail);
System.out.println("======头插法crud=========");
System.out.println("链表元素个数:"+linkNode.GetSize(creatLinkByHead));
int getValue = linkNode.GetValue(4, creatLinkByHead);
if(getValue==-1) {
System.out.println("根据下标查到值:不存在!");
}else {
System.out.println("根据下标查到值:"+getValue);
}
Map<String, Object> map = linkNode.GetLNodeAndIndex(5, creatLinkByHead);
for (Entry<String, Object> m : map.entrySet()) {
System.out.println(m);
}
if(linkNode.UpdatLNode(1, creatLinkByHead, 1)) {
System.out.print("修改后链表:");
printLink(creatLinkByHead);
}
if(linkNode.InsertLNode(2, creatLinkByHead, 3)) {
System.out.print("新增后链表:");
printLink(creatLinkByHead);
}
if(linkNode.DeleteLNode(1, creatLinkByHead)) {
System.out.print("删除后链表:");
printLink(creatLinkByHead);
}
}
}