📄 linklist.java
字号:
import java.io.*;
public class LinkList extends LinkNode{
public LinkNode firstNode;
public int LinkListLength;
public void LinkList(){
firstNode=null;
LinkListLength=0;
}
public void InsertElement(int command,int index,LinkNode value){
LinkNode NewNode;
if(firstNode==null){
firstNode=value;
LinkListLength++;
return;
}
if(command>1||command<0){
System.out.println("无效的前插后插标志。");
return;
}
if(index>LinkListLength||index<0){
System.out.println("插入位置超出范围。");
return;
}
NewNode=value;
LinkNode NextAddr=firstNode;
LinkNode PriorAddr=firstNode;
int count=1;
if(index==0){
NewNode.right=firstNode;
firstNode=NewNode;
LinkListLength++;
}else{
while(count<index){
PriorAddr=NextAddr;
NextAddr=NextAddr.right;
count++;
}
if(command==1){
NewNode.right=NextAddr;
PriorAddr.right=NewNode;
LinkListLength++;
if(index==1)
firstNode=NewNode;
}else if(command==0){
NewNode.right=NextAddr.right;
NextAddr.right=NewNode;
LinkListLength++;
if(index==1)
firstNode=NextAddr;
}
}
}
public void DeleteElement(int command,int value){
if(firstNode==null){
System.out.println("插入标志超出范围");
return;
}
LinkNode NextAddr=firstNode;
LinkNode PriorAddr=firstNode;
int count=0;
if (command==1){
if(value<0||value>LinkListLength+1){
System.out.println("指定位置超出线形表范围");
return;
}
while(count<value-1){
PriorAddr=NextAddr;
NextAddr=NextAddr.right;
count++;
if(NextAddr==null){
System.out.println("线形表中不存在指定的元素.");
return;
}
}
}else if(command==0){
while(NextAddr.id!=value&&count<=LinkListLength){
PriorAddr=NextAddr;
NextAddr=NextAddr.right;
count++;
if(NextAddr==null){
System.out.println("线形表中不存在指定的元素.");
return;
}
}
}
if(count==0)
firstNode=firstNode.right;
else PriorAddr.right=NextAddr.right;
LinkListLength--;
}
public boolean QueryElement(int value){
if(firstNode==null)
return false;
LinkNode NextAddr=firstNode;
int count=0;
while(NextAddr.id!=value){
NextAddr=NextAddr.right;
if(NextAddr==null){
count++;
break;
}
}
if(count==0)
return true;
return false;
}
public int getLinkListLength(){
return LinkListLength;
}
public void displayElements(){
System.out.println("线形表中的内容如下");
if(firstNode==null)
return;
LinkNode NextAddr=firstNode;
int count=0;
while(count<LinkListLength){
System.out.print(" "+NextAddr.id+"--"+NextAddr.name);
NextAddr=NextAddr.right;
count++;
}
System.out.println("");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -