码迷,mamicode.com
首页 > 编程语言 > 详细

java学习之—链表(1)

时间:2018-06-14 18:05:15      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:第一个   nbsp   链表   out   一个   admin   res   str   对象   

/**
 * 单链表操作
 * Create by Administrator
 * 2018/6/14 0014
 * 下午 2:05
 **/
public class Link {

    public int iData;
    public double dData;
    public Link next;

    public Link(int id, double dd) {
        this.iData = id;
        this.dData = dd;
    }

    public void displayLink() {
        System.out.print("{" + iData + " , " + dData + "} ");
    }
}

class LinkList {

    private Link first;

    public LinkList() {
        this.first = null;
    }

    public boolean isEmpty(){           //判断链表是否为空
        return (first == null);
    }

    public Link find(int key){          //查找指定的对象
        Link current = first;           //拿到第一个点
        while (current.iData != key){   //重第一个开始遍历
            if(current.next == null){
                return null;            //未找到结果
            }else {
                current = current.next; //获取下一个
            }
        }
        return current;                 //找到结果
    }

    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 insertFirst(int id,double dd){
        Link link = new Link(id, dd);
        link.next = first;          //储存对象的引用
        first = link;               //把当前对象赋值给first
    }

    public Link deleteFrst() {      //删除首链表,正好和添加相反
        if(!isEmpty()){
            Link temp = first;
            first = first.next;
            return temp;
        }
        return null;
    }

    public void displayList(){
        System.out.print("List (first-->last): ");
        Link current = first;
        while(current != null){
            current.displayLink();
            current = current.next;
        }
        System.out.println("");
    }

    public static void main(String[] args) {
        LinkList theList = new LinkList();

        theList.insertFirst(22,2.99);
        theList.insertFirst(44,4.99);
        theList.insertFirst(66,6.99);
        theList.insertFirst(88,8.99);
技术分享图片

 

        theList.displayList();

//        while (!theList.isEmpty()){
//            Link aLink = theList.deleteFrst();
//            System.out.print("Deleted");
//            aLink.displayLink();
//            System.out.println("");
//        }

        Link f = theList.find(44);
        if(f != null){
            System.out.print("find Link: ");
            f.displayLink();
            System.out.println("");
            Link result = theList.delete(f.iData);
            if(result != null){
                System.out.print("delete success ");
                result.displayLink();
            }
        }
        System.out.println("");
        theList.displayList();
    }
}

  技术分享图片

技术分享图片

 

java学习之—链表(1)

标签:第一个   nbsp   链表   out   一个   admin   res   str   对象   

原文地址:https://www.cnblogs.com/chancy/p/9183288.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!