数据结构(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. 《轻松学算法》赵烨
目录
相关文章
|
8天前
|
存储 人工智能 算法
数据结构与算法细节篇之最短路径问题:Dijkstra和Floyd算法详细描述,java语言实现。
这篇文章详细介绍了Dijkstra和Floyd算法,这两种算法分别用于解决单源和多源最短路径问题,并且提供了Java语言的实现代码。
34 3
数据结构与算法细节篇之最短路径问题:Dijkstra和Floyd算法详细描述,java语言实现。
|
6天前
|
存储 算法 Java
Java常用的数据结构
【10月更文挑战第3天】 在 Java 中,常用的数据结构包括数组、链表、栈、队列、树、图、哈希表和集合。每种数据结构都有其特点和适用场景,如数组适用于快速访问,链表适合频繁插入和删除,栈用于实现后进先出,队列用于先进先出,树和图用于复杂关系的表示和查找,哈希表提供高效的查找性能,集合用于存储不重复的元素。合理选择和组合使用这些数据结构,可以显著提升程序的性能和效率。
|
12天前
|
存储 Java
数据结构第二篇【关于java线性表(顺序表)的基本操作】
数据结构第二篇【关于java线性表(顺序表)的基本操作】
24 6
|
1月前
|
Java
java数据结构,双向链表的实现
文章介绍了双向链表的实现,包括数据结构定义、插入和删除操作的代码实现,以及双向链表的其他操作方法,并提供了完整的Java代码实现。
java数据结构,双向链表的实现
|
1月前
|
存储 Java
java数据结构,线性表链式存储(单链表)的实现
文章讲解了单链表的基本概念和Java实现,包括头指针、尾节点和节点结构。提供了实现代码,包括数据结构、接口定义和具体实现类。通过测试代码演示了单链表的基本操作,如添加、删除、更新和查找元素,并总结了操作的时间复杂度。
java数据结构,线性表链式存储(单链表)的实现
|
12天前
|
Java 语音技术 容器
java数据结构泛型
java数据结构泛型
23 5
|
12天前
|
存储 安全 Java
【用Java学习数据结构系列】探索顺序表和链表的无尽秘密(附带练习唔)pro
【用Java学习数据结构系列】探索顺序表和链表的无尽秘密(附带练习唔)pro
18 3
|
12天前
|
存储 安全 Java
【用Java学习数据结构系列】探索栈和队列的无尽秘密
【用Java学习数据结构系列】探索栈和队列的无尽秘密
25 2
|
12天前
|
存储 Java 编译器
【用Java学习数据结构系列】初识泛型
【用Java学习数据结构系列】初识泛型
16 2
|
12天前
|
存储 缓存 Java
【用Java学习数据结构系列】HashMap与TreeMap的区别,以及Map与Set的关系
【用Java学习数据结构系列】HashMap与TreeMap的区别,以及Map与Set的关系
27 1