常用的数据结构解析

         程序中常用到的数据结构有数组、链表、树、堆、哈希表等等,而每一种数据结构都有各自的优缺点,了解

各种数据结构以及其优缺点,在使用的时候才能不方。

  • 数组:通过创建对象直接使用。

             优点:插入快(直接插在后面),在知道下标的情况下可以快速存取。

             缺点:查找慢(要从头到尾一个个的找),删除慢(查找到删除的时候还要把后面的数据往前面复制移动),大小固定(在创建数组的时候就固定了大小)。

  • 有序数组:对有序数组的一个延伸,在插入数据的时候按顺序排列。

              优点:比无序的数组查找快。

              缺点:删除和插入慢,大小固定。

  •  链表:数组作为一种数据存储结构存在一些弊端:无序中搜索效率低,有序中插入效率低,不管哪种数组删除效率都很低,  且创建之后大小都固定,由此有另一种数据存储结构:链表。

              优点:插入快,删除快

              缺点:查找慢。

            链表又包括单向单端链表,单向双端链表,以下是代码实现:
 

 /**
 * 链节点
 */

public class Link {
    public int data;
    public Link next;

    public void display(){
        System.out.println(data);
    }
}
/**
 * 
 * 单端链表
 */

public class SingleLinkList {
    private Link first;

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

    public void insertFirst(int data) {
        Link newLink = new Link();
        newLink.data = data;
        newLink.next = first;
        first = newLink;
    }

    public Link deleteFirst() {
        if (first != null) {
            Link temp = first;
            first = first.next;
            return temp;
        } else {
            return null;
        }
    }

    public void displayList() {
        Link current = first;
        while (current != null) {
            current.display();
            current = current.next;
        }
    }

    public Link find(int data) {
        if (first != null) {
            Link current = first;
            while (current.data != data) {
                if (current.next == null) {
                    return null;
                }
                current = current.next;
            }
            return current;

        }
        return null;
    }

    public Link delete(int data) {
        if (first == null) return null;
        Link current = first;
        Link previous = first;
        while (current.data != data) {
            if (current.next == null) return null;
            previous = current;
            current = previous.next;
        }
        if (current == first) {
            first = current.next;
        } else {
            previous.next = current.next;
        }
        return current;
    }

    public boolean isEmpty() {
        return first == null;
    }
}


/**
 * 双端链表
 */

public class FirstLastLink {

    private Link first;
    private Link last;

    public FirstLastLink() {
        first = null;
        last = null;
    }

    public boolean isEmpty() {
        return first == null;
    }

    public void insertFirst(int data) {
        Link newLink = new Link();
        newLink.data=data;
        if (first == null) {
            first = newLink;
            last = newLink;
        } else {
            first.next = first;
            first = newLink;
        }
    }

    public void insertLast(int data) {
        Link newLink = new Link();
        newLink.data=data;
        if (isEmpty()) {
            first = newLink;
            last = newLink;
        } else {
            last.next = last;
            last = newLink;
        }
    }

    public Link removeFirst() {
        if (isEmpty()) {
            return null;
        }
        Link temp = first;
        if (temp.next == null)
            last = null;
        first = temp.next;
        return temp;
    }

    public void disPlayList() {
        Link current = first;
        while (current != null) {
            current.display();
            current = current.next;
        }
    }
}

 

             

 

            

  • :栈可以由数组实现,也可以由链表实现,主要是提供一种先进后出的数据存储结构。

              优点:提供后进先出的存取方式。

              缺点:存取其它项很慢。



/**
 *以数组实现的栈
 */

public class StackX {

    private Object[] array;
    private int maxSize;
    private int top;

    public StackX(int s) {
        this.maxSize = s;
        array = new Object[maxSize];
        top = -1;
    }

    public void push(Object o) {
        array[++top] = o;
    }

    public Object pop() {
        return array[top--];
    }

    public Object peck() {
        return array[top];
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public boolean isFull() {
        return top == maxSize - 1;
    }
}


/**
 * 用链表实现的栈
 */

public class LinkStack {
    private SingleLinkList list;

    public LinkStack() {
        this.list = new SingleLinkList();
    }

    public Link pop() {
        return list.deleteFirst();
    }

    public void push(int data) {
        list.insertFirst(data);
    }


}

 

  • 队列:队列可以由数组实现,也可以由链表实现,主要是提供一种先进先出的数据存储结构。

              优点:提供先进先出的存取方式。

              缺点:存取其它项很慢。



/**
 * Created by watson on 2019/2/22.
 *
 *  数组实现的队列
 */

public class Queue {
    private int maxSize;
    private long[] queArray;
    private int front;
    private int rear;
    private int nItems;

    public Queue(int s) {
        maxSize = s;
        queArray = new long[maxSize];
        front = 0;
        rear = -1;
        nItems = 0;
    }

    public void insert(long j) {
        if (rear == maxSize - 1)
            rear = -1;
        queArray[++rear] = j;
        nItems++;
    }

    public long remove() {
        long temp = queArray[front++];
        if (front == maxSize)
            front = 0;
        nItems--;
        return temp;
    }

    public long peekFrount() {
        return queArray[front];
    }

    public boolean isEmpty() {
        return nItems == 0;
    }

    public boolean isFull() {
        return nItems == maxSize;
    }

    public int size() {
        return nItems;
    }
}


/**
 * Created by watson on 2019/2/25.
 * 链表实现队列
 */

public class LinkQueue {
    private FirstLastLink list;

    public LinkQueue() {
        list = new FirstLastLink();
    }

    public boolean isEmpty() {
        return list.isEmpty();
    }

    public void insert(int data) {
        list.insertLast(data);
    }

    public Link remove() {
        return list.removeFirst();
    }

    public void displaylist(){
        list.disPlayList();
    }
}

还有其它的一些数据结构,代码算法比较复杂,仅记录优缺点及说明:

  •   二叉树

               优点:查找,插入,删除都快
               缺点:删除算法复杂

  •    红黑树

                优点:查找,插入,删除都快
                缺点:算法复杂

  •     2-3-4树

                优点:查找删除插入快
                缺点:算法复杂

  •     哈希表

                优点:如果关键字已知则存取快,插入快
                缺点:删除慢,如果不知道关键字则存取很慢,对存储空间使用不充分

  •     堆

                优点:插入删除快,对最大数据项的存取快
                缺点:对其他数据项存取慢

 

一直都是一个消费者的姿态,突然想做一个生产者了,于是文章就产生了。欢迎大家留言!!!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值