数据结构与算法

20 篇文章 0 订阅
7 篇文章 0 订阅

数据结构与算法

数据结构

数据结构的概念

数据结构的存储

顺序存储

连续的存储空间

链式存储

非连续的存储空间 数据

数据的逻辑机构

集合结构

线性结构 一对一

数组 长度固定
数组元素的添加

step1: 创建一个新的数组

step2: 遍历新的数组

step3: 遍历到指定位置将元素添加进去

数组元素的删除

step1:创建一个数组

step2:遍历旧的数组

step3:遍历到指定位置将元素删去

数组元素的查找
顺序查找

按照数组顺序

二分查找

二分查找的前提条件:数组是有序的

Step1:记录数组中间的位置,开始位置,结束位置

Step2:如果开始位置,小于等于结束位置,开始遍历

Step3:如果找到返回当前位置,否则继续当前遍历

特点:先进后出

栈是常用数据结构,是线性数据结构的一种,具有先进后出的特点,以下是根据栈的特点进行Mock的代码,希望对看到的人有所帮助。

package program.design;

import java.util.Arrays;
import java.util.EmptyStackException;

/**
 * mock
 *
 * @author : cuantianhou 2019/8/20
 */
public class MockStack<T> {

    private Object[] element;

    private int capacity;

    private int index = 0;

    private static final int GROW_FACTORY = 2;

    /**
     * 构造函数
     */
    public MockStack(){
        this(10);
    }

    /**
     * 有参构造函数
     * @param capacity 容量
     */
    public MockStack(int capacity){
        if (capacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+ capacity);
        this.capacity = capacity;
        this.element = new Object[capacity];
    }


    /**
     * 扩容
     */
    public void ensureCapacity(){
        int newCapacity = capacity * GROW_FACTORY;
        element = Arrays.copyOf(element,newCapacity);
        capacity = newCapacity;
    }

    /**
     * 入栈
     * @param value
     */
    public void push(T value) {
        if (index == capacity) {
            ensureCapacity();
        }
        element[index++] = value;
    }

    /**
     * 出栈 返回元素并且删除
     */
    public T pop(){
        T var = peek();
        index--;
        return var;
    }

    /**
     * 取栈中元素
     */
    public T peek(){
        if(index == 0)   {
            throw new EmptyStackException();
        }
        return (T)element[index-1];
    }

    /**
     * 判断栈中的元素是否为空
     * @return 结果
     */
    public Boolean isEmpty(){
        return index == 0;
    }

    /**
     * 栈中元素个数
     * @return 栈中元素个数
     */
    public int size(){
        return index;
    }
}
测试程序

package program.design;

/**
 * 主程序
 *
 * @author : cuantianhou 2019/8/20
 */
public class MainApplication {

    public static void main(String[] args) {
        MockStack<Integer> myStack = new MockStack<>(3);
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        myStack.push(4);
        myStack.push(5);
        myStack.push(6);
        myStack.push(7);
        myStack.push(8);

        for (int i = 0; i < 8; i++) {
            System.out.println(myStack.pop());
        }
     
    }
}
参考文章:1https://github.com/Snailclimb/JavaGuide/blob/master/docs/java/Java%E7%A8%8B%E5%BA%8F%E8%AE%BE%E8%AE%A1%E9%A2%98.md2Jdk源码
队列

特点:先进先出

单链表
package ds.link;

/**
 * @author : cuantianhou 2019/12/15
 */
public class Node<T> {

    private T data;
    private  Node next;

    public Node(T data){
        this.data = data;
    }


    public Node appendHead(Node node){
        node.next = this;
        return node;
    }

    public Node appendTail(Node node){
        Node currentNode  = this;
        while (null!=currentNode.next){
            Node nextNode  = currentNode.next;
            currentNode = nextNode;
        }
        currentNode.next = node;
        return this;
    }

    public Node getNode(){
        if(this.isLast()){
            System.out.println("this.is.last");
        }
        return this.next;
    }

    public T getData(){
        return this.data;
    }

    public boolean isLast(){
        return this.next == null;
    }
}
循环链表
双向链表
递归

排序算法

插入排序
希尔排序
简单选择排序
堆排序
快排序
合并排序
基数排序

树形结构 一对多

图形结构 多对多

算法

算法的特性

输入
输出
有穷性
确定性
可行性

算法的基本要求

正确性
可读性
健壮性
时间复杂度
空间复杂度
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值