📄 linklist.java
字号:
//import java.awt.*;
//import java.lang.*;
class LinkList implements List{
private Node head;
private int length=0;
public LinkList(){
head=new Node(null);
}
public void clear() //置空表
{
Node p=head;
p.next=null;
length=0;
}
public int getLength()//求表长
{
return length;
}
public Object getEntry(int i) //取出第i个元素
{
if(length==0||i<1||i>length)
return null;
else
{
Node p=head;
for(int count=0;count<i;count++)
p=p.next;
return p.data;
}
}
public boolean isEmpty() //判表空运算
{
boolean isEmpty=false;
Node p=head;
if(p.next==null||length==0)
isEmpty=true;
return isEmpty;
}
public boolean add(int i, Object newEntry)//将数据元素插入线性表中第i个元素之前
{
boolean isSuccessful=true;
int count=0;
Node p=head;
while(p!=null&&count<i-1){
p=p.next;
count++;
}
if(p==null||count>i-1){
isSuccessful=false;
System.out.println("It is !");
}
else{
Node s=new Node(newEntry);
s.next=p.next;
p.next=s;
length++;
}
return isSuccessful;
}
public Object remove(int i) //删除第i个元素,并将被删除的元素返回
{
Object s=null;
int count=0;
Node p=head;
while(p.next!=null&&count<i-1)
{
p=p.next;
count++;
}
if(p==null||count>i-1)
System.out.println("sha chu wei zhi fei fa!");
else
{
s=p.next.data;
p.next=p.next.next;
length--;
}
return s;
}
public boolean add(Object newEntry) //将数据元素插入线性表的表头
{
Node p=head;
Node s=new Node(newEntry);
s.next=p.next;
p.next=s;
length++;
return true;
}
public boolean reverse()
{
Node p=head.next;
head.next=null;
Node q;
for(int count=0;count<length;count++){
q=p;
p=p.next;
q.next=head.next;
head.next=q;
}
return true;
}
public void display()
{
Node p=head;
for(int i=0;i<length;i++)
{
p=p.next;
System.out.print(p.data+" ");
}
System.out.println("");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -