数据结构(Java)-持续更新补充

简介: 复习一下数据结构,巩固一下基础。 之后打算再学一下算法,之前刷题总感觉摸不清门道,应该是概念没彻底搞明白。

复习一下数据结构,巩固一下基础。
之后打算再学一下算法,之前刷题总感觉摸不清门道,应该是概念没彻底搞明白。

import java.util.Arrays;

public class Stack {
    private int size;
    private int[] array;
    
    public Stack() {
        this(10);
    }
    public Stack(int init) {
        if(init <= 0) {
            init = 10;
        }
        array = new int[init];
    }
    public void push(int item) {
        if(size == array.length) {
            array = Arrays.copyOf(array, size * 2);
        }
        array[size++] = item;
    }
    public int peek() {
        if(size == 0) {
            throw new ArrayIndexOutOfBoundsException("栈已经空啦");
        }
        return array[size - 1];
    }
    public int pop() {
        int item = peek();
        size--;
        return item;
    }
    public boolean isFull() {
        return size == array.length;
    }
    public boolean isEmpty() {
        return size == 0;
    }
    public int size() {
        return size;
    }
}

队列


public class ArrayQueue {
    private final Object[] items;
    private int head;
    private int tail;
    public ArrayQueue(int capacity) {
        this.items = new Object[capacity];
    }
    public boolean put(Object item) {
        if(head == (tail + 1) % items.length) {
            return false;
        }
        items[tail] = item;
        tail = (tail + 1) % items.length;
        return true;
    }
    public Object peek() {
        if(head == tail) {
            return null;
        }
        return items[head];
    }
    public Object poll() {
        if(head == tail) {
            return null;
        }
        Object item = items[head];
        items[head] = null;
        head = (head + 1) % items.length;
        return item;
    }
    public boolean isFull() {
        return head == (tail + 1) % items.length;
    }
    public boolean isEmpty() {
        return head == tail;
    }
    public int size() {
        if(head <= tail) {
            return tail - head;
        }
        return tail - head + items.length;
    }
}

用栈来模拟队列


public class Stack2Queue {
    private Stack stack1;
    private Stack stack2;
    private int maxLength;
    public Stack2Queue(int capacity) {
        maxLength = capacity;
        stack1 = new Stack(capacity);
        stack2 = new Stack(capacity);
    }
    public boolean put(int item) {
        if(stack1.isFull() || size() == maxLength) {
            return false;
        }
        stack1.push(item);
        return true;
    }
    public int poll() {
        if(!stack2.isEmpty()) {
            return stack2.pop();
        }else {
            while(!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
            return stack2.pop();
        }
    }
    public int size() {
        return stack1.size() + stack2.size();
    }
}

用队列来模拟栈


public class Queue2Stack {
    private ArrayQueue queue1;
    private ArrayQueue queue2;
    private int maxLength;
    public Queue2Stack(int capacity) {
        maxLength = capacity;
        queue1 = new ArrayQueue(capacity);
        queue2 = new ArrayQueue(capacity);
    }
    public boolean push(Object item) {
        if(size() == maxLength) {
            return false;
        }
        if(queue2.isEmpty()) {
            queue1.put(item);
        }else {
            queue2.put(item);
        }
        return true;
    }
    public Object pop() {
        if(size() == 0) {
//            return null;
            throw new IndexOutOfBoundsException("栈里空啦");
        }
        if(queue2.isEmpty()) {
            while(queue1.size() > 1) {
                queue2.put(queue1.poll());
            }
            return queue1.poll();
        }else {
            while(queue2.size() > 1) {
                queue1.put(queue2.poll());
            }
            return queue2.poll();
        }
    }
    public int size() {
        return queue1.size() + queue2.size();
    }
}

链表

public class Node {
    private int data;
    private Node next;
    public int getData() {
        return data;
    }
    public void setData(int data) {
        this.data = data;
    }
    public Node getNext() {
        return next;
    }
    public void setNext(Node next) {
        this.next = next;
    }
}

public class Link {
    private int size = 0;
    private Node first;
    private Node last;
    
    public Link() {
        
    }
    public void addFirst(int data) {
        if(size == 0) {
            fillStart(data);
        }else {
            Node node = new Node();
            node.setData(data);
            node.setNext(first);
            first = node;
            size++;
        }
    }
    public void addLast(int data) {
        if(size == 0) {
            fillStart(data);
        }else {
            Node node = new Node();
            node.setData(data);
            last.setNext(node);
            last = node;
            size++;
        }
    }
    public void add(int data, int index) {
        if(size > index) {
            if(size == 0) {
                fillStart(data);
            }else if(index == 0) {
                addFirst(data);
            }else if (index == size -1) {
                addLast(data);
            }else {
                Node node = new Node();
                node = get(index - 1);
                Node temp = new Node();
                temp.setData(data);
                temp.setNext(node.getNext());
                node.setNext(temp);
                size++;
            }
        }else {
            throw new IndexOutOfBoundsException("链表没有这么长");
        }
    }
    public void removeFirst() {
        if(size == 0) {
            throw new IndexOutOfBoundsException("链表中没有元素");
        }else if(size == 1) {
            clear();
        }else {
            Node node = first;
            first = first.getNext();
            node = null;
            size--;
        }
    }
    public void removeLast() {
        if(size == 0) {
            throw new IndexOutOfBoundsException("链表中没有元素");
        }else if(size == 1) {
            clear();
        }else {
            Node node = get(size -2);
            node.setNext(null);
            last = node;
            size--;
        }
    }
    public void removeMiddle(int index) {
        if(size == 0) {
            throw new IndexOutOfBoundsException("链表中没有元素");
        }else if(size == 1) {
            clear();
        }else {
            Node node = get(index - 1);
            Node temp = node.getNext();
            node.setNext(temp.getNext());
            size--;
        }
    }
    public int size() {
        return size;
    }
    public void clear() {
        first = null;
        last = null;
        size = 0;
    }
    public Node get(int index) {
        Node temp = first;
        for(int i = 0; i < index; i++) {
            temp = temp.getNext();
        }
        return temp;
    }
    private void fillStart(int data) {
        first = new Node();
        first.setData(data);
        last = first;
        size++;
    }
    public void printAll() {
        Node temp = first;
        System.out.println(temp.getData());
        for(int i = 0; i < size - 1; i++) {
            temp = temp.getNext();
            System.out.println(temp.getData());
        }
    }
}

暂时先复习了这么多,本文持续更新~
如果有啥觉得可以补充或者建议,可以留言噢~

参考:

  1. 《轻松学算法》赵烨
目录
相关文章
|
1月前
|
算法 搜索推荐 Java
数据结构与算法(Java篇)笔记--希尔排序
数据结构与算法(Java篇)笔记--希尔排序
|
23天前
|
存储 算法 Java
Java数据结构与算法-java数据结构与算法(二)
Java数据结构与算法-java数据结构与算法
66 1
|
1月前
|
缓存 算法 安全
Java集合框架:深入探究数据结构与算法的精华
Java集合框架:深入探究数据结构与算法的精华
|
7天前
|
Java API
编码的奇迹:Java 21引入有序集合,数据结构再进化
编码的奇迹:Java 21引入有序集合,数据结构再进化
14 0
|
23天前
|
XML 存储 算法
Java数据结构与算法-java数据结构与算法(五)
Java数据结构与算法-java数据结构与算法
47 0
|
26天前
|
缓存 安全 Java
Java并发编程中的高效数据结构 - ConcurrentHashMap
本文将深入探讨Java并发编程中的一种高效数据结构 - ConcurrentHashMap。我们将详细介绍ConcurrentHashMap的基本原理,包括其设计思路、实现方式以及如何在多线程环境下提供高效的并发访问。同时,我们还将通过实例代码演示如何使用ConcurrentHashMap来优化并发程序的性能。
|
26天前
|
Java 数据库连接 API
Java 学习路线:基础知识、数据类型、条件语句、函数、循环、异常处理、数据结构、面向对象编程、包、文件和 API
Java 是一种广泛使用的、面向对象的编程语言,始于1995年,以其跨平台性、安全性和可靠性著称,应用于从移动设备到数据中心的各种场景。基础概念包括变量(如局部、实例和静态变量)、数据类型(原始和非原始)、条件语句(if、else、switch等)、函数、循环、异常处理、数据结构(如数组、链表)和面向对象编程(类、接口、继承等)。深入学习还包括包、内存管理、集合框架、序列化、网络套接字、泛型、流、JVM、垃圾回收和线程。构建工具如Gradle、Maven和Ant简化了开发流程,Web框架如Spring和Spring Boot支持Web应用开发。ORM工具如JPA、Hibernate处理对象与数
90 3
|
1月前
|
算法 搜索推荐 Java
数据结构与算法(Java篇)笔记--快速排序
数据结构与算法(Java篇)笔记--快速排序
|
1月前
|
机器学习/深度学习 算法 搜索推荐
数据结构与算法(Java篇)笔记--归并排序
数据结构与算法(Java篇)笔记--归并排序
|
1月前
|
算法 搜索推荐 Java
数据结构与算法(Java篇)笔记--选择排序
数据结构与算法(Java篇)笔记--选择排序