欢迎访问宙启技术站
智能推送

Java函数实现常见的数据结构与算法

发布时间:2023-07-04 05:36:17

Java是一种面向对象的编程语言,可以用来实现各种数据结构和算法。下面是一些常见的数据结构和算法的Java函数实现。

1. 数组:Java中的数组是一种基本的数据结构,可以用来存储一组相同类型的元素。以下是一个简单的数组的Java函数实现:

public class MyArray {
    private int[] array;
    private int size;

    public MyArray(int capacity) {
        array = new int[capacity];
        size = 0;
    }

    public boolean insert(int element) {
        if (size >= array.length) {
            return false;
        }
        array[size++] = element;
        return true;
    }

    public boolean delete(int index) {
        if (index < 0 || index >= size) {
            return false;
        }
        for (int i = index; i < size - 1; i++) {
            array[i] = array[i + 1];
        }
        size--;
        return true;
    }

    public int get(int index) {
        if (index < 0 || index >= size) {
            return -1;
        }
        return array[index];
    }

    public int getSize() {
        return size;
    }
}

2. 链表:链表是一种常见的数据结构,可以动态地插入和删除元素。以下是一个简单的链表的Java函数实现:

public class MyLinkedList {
    private Node head;
    private int size;

    private class Node {
        private Object data;
        private Node next;

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

    public void add(Object element) {
        Node newNode = new Node(element);
        if (head == null) {
            head = newNode;
        } else {
            Node current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = newNode;
        }

        size++;
    }

    public boolean remove(Object element) {
        if (head == null) {
            return false;
        }
        if (head.data.equals(element)) {
            head = head.next;
            size--;
            return true;
        }

        Node current = head;
        while (current.next != null) {
            if (current.next.data.equals(element)) {
                current.next = current.next.next;
                size--;
                return true;
            }
            current = current.next;
        }

        return false;
    }

    public int getSize() {
        return size;
    }
}

3. 栈:栈是一种后进先出(LIFO)的数据结构,可以用数组或链表实现。以下是一个基于数组的栈的Java函数实现:

public class MyStack {
    private int[] array;
    private int top;
    private int size;

    public MyStack(int capacity) {
        array = new int[capacity];
        top = -1;
        size = 0;
    }

    public boolean push(int element) {
        if (size >= array.length) {
            return false;
        }
        array[++top] = element;
        size++;
        return true;
    }

    public int pop() {
        if (top == -1) {
            throw new IllegalStateException("Stack is empty");
        }
        int element = array[top--];
        size--;
        return element;
    }

    public int peek() {
        if (top == -1) {
            throw new IllegalStateException("Stack is empty");
        }
        return array[top];
    }

    public int getSize() {
        return size;
    }
}

4. 队列:队列是一种先进先出(FIFO)的数据结构,可以用数组或链表实现。以下是一个基于链表的队列的Java函数实现:

public class MyQueue {
    private Node head;
    private Node tail;
    private int size;

    private class Node {
        private Object data;
        private Node next;

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

    public void enqueue(Object element) {
        Node newNode = new Node(element);
        if (head == null) {
            head = tail = newNode;
        } else {
            tail.next = newNode;
            tail = newNode;
        }

        size++;
    }

    public Object dequeue() {
        if (head == null) {
            throw new IllegalStateException("Queue is empty");
        }
        Object element = head.data;
        head = head.next;
        if (head == null) {
            tail = null;
        }
        size--;
        return element;
    }

    public int getSize() {
        return size;
    }
}

5. 排序算法:排序算法可以对一组元素进行排序,常见的排序算法有冒泡排序、插入排序、选择排序、快速排序、归并排序等。以下是一个简单的冒泡排序的Java函数实现:

public class BubbleSort {
    public static void bubbleSort(int[] array) {
        int n = array.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }
}

这些是一些常见的数据结构和算法的Java函数实现。通过学习和理解这些实现,可以更好地掌握Java编程语言,并应用于实际开发中。