单链表的基本操作


class Link{
    public int iData;
    public double dData;
    public Link next;//指向下一个数据指针
    
    public Link(int id,double dd) {
        iData=id;
        dData=dd;
    }
    
    public void displayLink() {
        System.out.println("{"+iData+","+dData+"}");
    }
}

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;
    }
    
    //查找包含指定关键字的链表结点,时间复杂度为o(n)
    public Link find(int key) {
        Link current=first;
        while(current.iData!=key) {
            if(current.iData!=key) {
                if(current.next==null) {
                    return null;
                }else {
                    current=current.next;
                }
            }
        }
        return current;

    }

   //查找包含指定关键字的链表结点,时间复杂度为o(n)
    public Link find(double key) {
        Link current=first;
        while(current.dData!=key) {
            if(current.dData!=key) {
                if(current.next==null) {
                    return null;
                }else {
                    current=current.next;
                }
            }
        }
        return current;
    }
    
    //删除指定某个链结点,时间复杂度为o(n)
    public Link delete(int key) {
        Link current=first;
        Link previous=first;
        while(current.iData!=key) {
            if(current.next==null) {
                return null;
            }else {
                previous=current;
                current=current.next;
            }
        }
        if(current==first) {
            first=first.next;
        }else {
            previous.next=current.next;
        }
        return current;
    }
    
    //输出链表
    public void displayList() {
        System.out.println("List(first-》last):");
        Link current=first;
        while(current!=null) {
            current.displayLink();
            current=current.next;
        }
        System.out.println();
    }

}

public class LinkListApp {
    public static void main(String[] args) {
        LinkList theList=new LinkList();
        theList.insertFirst(22, 2.1);
        theList.insertFirst(23, 2.3);
        theList.insertFirst(25, 2.5);
        theList.insertFirst(28, 2.8);
        theList.displayList();
        
        Link f=theList.find(22);
        Link g=theList.find(2.1);
        if(f!=null||g!=null) {
            System.out.println("found the data:"+f.iData);
            System.out.println("found the data:"+g.dData);
        }else {
            System.out.println("don't find link");
            
        }
        
        Link d=theList.delete(25);
        if(d!=null) {
            System.out.println("delete the date:"+d.iData);
            
        }else {
            System.out.println("can't find link");
            
        }
        
        theList.displayList();
        
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灰太狼_cxh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值