java学习之—链表

Posted 花儿为何那样红

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java学习之—链表相关的知识,希望对你有一定的参考价值。

/**
 * 单链表操作
 * 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学习之—链表的主要内容,如果未能解决你的问题,请参考以下文章

Redis学习之列表类型详解

c学习之单向链表的删除与清除操作

java学习之集合家族2

python学习之链表实现有序表

iOS学习之代码块(Block)

Redis学习之数据类型List详解