链表-B-单链表:针对key查询,或者删除

package e_link.B;
/**
 * 单链表:针对key查询,或者删除
 * @author Administrator
 *
 */
public class LinkListApp {
public static void main(String[] args) {
LinkList theList=new LinkList();
theList.insertFirst(1, 10);
theList.insertFirst(2, 20);
theList.insertFirst(3, 30);
theList.insertFirst(4, 40);
theList.insertFirst(5, 50);
theList.insertFirst(6, 60);
theList.insertFirst(7, 70);

theList.displaysList();
theList.find(1).displaysLink();
theList.delete(7).displaysLink();
System.out.println();
theList.displaysList();
}


}package e_link.B;


public class LinkList {
private Link first;


public LinkList() {
first = null;
}


public void insertFirst(int id, double dd) {
Link newLink = new Link(id, dd);
newLink.next = first;
first = newLink;
}


/*
* 根据指定key查询连接点
*/
public Link find(int key) {
Link current = first;
while (current.iDate != key) {
if (current.next == null)
return null;
else
current = current.next;
}
return current;


}



/*
* 根据key删除指定的连接点
*/
public Link delete(int key) {
Link current = first;// 当前
Link previous = first;// 前一个
while (current.iDate != key) {
if (current.next == null)
return null;
else {
previous = current;
current = current.next;
}
}
if (current == first)
first = current.next;
else
previous.next = current.next;
return current;


}
public void displaysList(){
Link current=first;
while(current.next!=null){
current.displaysLink();
current=current.next;
System.out.println();
}
}
}package e_link.B;


public class Link {
public int iDate;
public double dDate;
public Link next;
public Link(int id,double dd){
iDate=id;
dDate=dd;
}
public void displaysLink(){
System.out.print("{"+iDate+","+dDate+"}");
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值