//然后有点小问题还没有完善,不过就一两行代码的事情,希望大家能给出一些意见,大鸟勿喷QAQ,
package 链表;
public class _Main {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
Link link=new Link();
link.add(“Hello! world QAQ”);//add里面是添加的数据
link.add(“略略略”);
link.add(“插入的第三段数据”);
link.add(“第四段数据”);
link.add(“就很bad”);
//System.out.println(“已取出区块: “+link.check(0));
System.out.println(link.delete(1));//删除指定区块
System.out.println(“本链表有: link.length:”+link.length+” 个成员”);
link.print();
link.add(“啦啦啦啦德玛西亚”,5);
System.out.println(“\n本链表有: link.length:”+link.length+” 个成员”);
link.print();
System.out.println("\n处理完毕,共耗时: "+(System.currentTimeMillis() - startTime) + "ms");
}
}
package 链表;
public class Link { //双链表结构 包含存放对象指针的head和next
public Link head;
public Object data;
public Link next;
public int length;
public Link() {
this.head=null;
this.data=”链表头”;
this.next=null;
//System.out.println(“head:”+” data:”+data+” next:”+next);
}
private Link(Object data,Link link) {
this.head=link; //头部
this.data=data; //data区块
this.next=null; //尾部
//System.out.println(“head:”+link+” data:”+data+” next:”+next);
}
public Link add(Object data) { //每次add的this都是最开始的那个对象的地址
Link temp=this;//Link temp=this;
Link retemp=null;
//-- System.out.println("add-this:"+this);
while(temp.next!=null) {
temp=temp.next;
}
try {
Link newtemp=new Link(data,temp);
temp.next=newtemp;
newtemp.head=temp;
retemp=newtemp;
//System.out.println("temp.next:"+temp.next);
//下一组数据的头部等于上一组数据的尾部
}catch (NullPointerException e) {}
length();
return retemp;
}
public boolean add(Object data,int a) {//a为插入的位置,但不能超过下标 返回false表示a超出length,默认插在最末尾,返回true表示在指定位置插入成功
Link temp=this;
for (int i = 0; i < a; i++) {
if(temp.next!=null) {
temp=temp.next;
}
else {
add(data);
return false;//当位置为最后一位的时候 默认添加到尾部
}
}
Link newtemp=add(data);
newtemp.head.next=null;
temp.head.next=newtemp;//1
newtemp.head=temp.head;//2
temp.head=newtemp;
newtemp.next=temp;
return true;
}
public boolean print() {//从头到尾输出链表,先倒序找到没有next与之对应的指针,即为链表first
Link temp=this;
while(temp!=null) {
//temp=temp.head;
System.out.println("head: "+temp.head+" this:"+temp+" next: "+temp.next+" data:"+temp.data);
temp=temp.next;
}
//System.out.println(this);
return true;
}
public String check(int a) {//从第一个开始查找,a为下标
Link temp=this;
for (int i = 0; i < a; i++) {
if(temp.next!=null) {
temp=temp.next;
}else {
return "超出链表下标,因此仅输出存在的最后一段数据:\nhead: "+temp.head+" this:"+temp+" next: null"+" data:"+temp.data;
}
}
return "head: "+temp.head+" this:"+temp+" next: "+temp.next+" data:"+temp.data;
}
public int length() {//取出链表的下标,个数
Link temp=this;
int sum=1;
while(temp.next!=null) {
temp=temp.next;
sum++;
}
length=sum;
return sum;
}
public String delete(int a) {//删除操作
Link temp=this;
String str;
int i = 0;
for (; i <= a; i++) {
if(temp.next!=null) {
temp=temp.next;
}else {
if(i<a) {
str="超出链表下标,删除失败!";
return str;
}
if(i==a) {
str="已删除: head: "+temp.head+" this:"+temp+" next: null"+" data:"+temp.data;
temp=temp.head;
temp.next=null;
return str;
}
}
}
str="已删除: head: "+temp.head+" this:"+temp+" next: "+temp.next+" data:"+temp.data;
temp.head.next=temp.next;
temp.next.head=temp.head;
length();
return str;
}
public void updata(int a,Object data) {//按顺序修改区块的data内容
for (int i = 0; i <= a; i++) {
}
}
}