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

Java函数实现数据结构:栈、队列、堆栈等常见数据结构的实现方式

发布时间:2023-06-22 10:04:40

Java是一门广泛应用于开发各类程序的高级编程语言,同时也是面向对象的编程语言。Java的数据结构包括了常见的栈、队列、堆栈等数据结构,可以通过Java函数来实现这些数据结构。该文章将讨论这些数据结构和如何通过Java函数进行实现。

一、栈

栈是一种后进先出(LIFO)的数据结构,它只允许在顶部进行操作。栈的基本操作包括压栈(push)、出栈(pop)、查看栈顶元素(peek)等。如下是通过Java函数实现栈的例子:

public class Stack {
    private int maxSize;
    private int[] stackArray;
    private int top;

    public Stack(int size) {
        maxSize = size;
        stackArray = new int[maxSize];
        top = -1;
    }

    public void push(int value) {
        if (isFull()) {
            System.out.println("Stack is full.");
        } else {
            top++;
            stackArray[top] = value;
        }
    }

    public int pop() {
        if (isEmpty()) {
            System.out.println("Stack is empty.");
            return -1;
        } else {
            int topValue = stackArray[top];
            top--;
            return topValue;
        }
    }

    public int peek() {
        if (isEmpty()) {
            System.out.println("Stack is empty.");
            return -1;
        } else {
            return stackArray[top];
        }
    }

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

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

    public void printStack() {
        if (isEmpty()) {
            System.out.println("Stack is empty.");
            return;
        } else {
            for (int i = top; i >= 0; i--) {
                System.out.print(stackArray[i] + " ");
            }
            System.out.println();
        }
    }
}

二、队列

队列是一种先进先出(FIFO)的数据结构,它只允许在队列尾部进行插入操作,而在队列头部进行删除操作。队列的基本操作包括入队(enqueue)、出队(dequeue)、查看队首元素(peek)等。如下是通过Java函数实现队列的例子:

public class Queue {
    private int maxSize;
    private int[] queueArray;
    private int front;
    private int rear;

    public Queue(int size) {
        maxSize = size;
        queueArray = new int[maxSize];
        front = 0;
        rear = -1;
    }

    public void enqueue(int value) {
        if (isFull()) {
            System.out.println("Queue is full.");
        } else {
            rear++;
            queueArray[rear] = value;
        }
    }

    public int dequeue() {
        if (isEmpty()) {
            System.out.println("Queue is empty.");
            return -1;
        } else {
            int frontValue = queueArray[front];
            front++;
            if (front == maxSize) {
                front = 0;
            }
            return frontValue;
        }
    }

    public int peek() {
        if (isEmpty()) {
            System.out.println("Queue is empty.");
            return -1;
        } else {
            return queueArray[front];
        }
    }

    public boolean isEmpty() {
        return (rear+1 == front) || (front+maxSize-1 == rear);
    }

    public boolean isFull() {
        return (rear+2 == front) || (front+maxSize-2 == rear);
    }

    public void printQueue() {
        if (isEmpty()) {
            System.out.println("Queue is empty.");
            return;
        } else {
            int i = front;
            while (i != rear) {
                System.out.print(queueArray[i] + " ");
                i++;
                if (i == maxSize) {
                    i = 0;
                }
            }
            System.out.println(queueArray[i]);
        }
    }
}

三、堆栈

堆栈是一种可以在任意位置插入和删除元素的线性数据结构。堆栈的基本操作包括插入(insert)、删除(delete)、查看堆栈顶端元素(top)等。如下是通过Java函数实现堆栈的例子:

public class Stack {
    private int maxSize;
    private int[] stackArray;
    private int top;

    public Stack(int size) {
        maxSize = size;
        stackArray = new int[maxSize];
        top = -1;
    }

    public void insert(int value) {
        if (isFull()) {
            System.out.println("Stack is full.");
        } else {
            top++;
            stackArray[top] = value;
        }
    }

    public int delete() {
        if (isEmpty()) {
            System.out.println("Stack is empty.");
            return -1;
        } else {
            int topValue = stackArray[top];
            top--;
            return topValue;
        }
    }

    public int top() {
        if (isEmpty()) {
            System.out.println("Stack is empty.");
            return -1;
        } else {
            return stackArray[top];
        }
    }

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

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

    public void printStack() {
        if (isEmpty()) {
            System.out.println("Stack is empty.");
            return;
        } else {
            for (int i = top; i >= 0; i--) {
                System.out.print(stackArray[i] + " ");
            }
            System.out.println();
        }
    }
}

综上所述,我们可以看到,Java函数实现栈、队列、堆栈等常见数据结构的方式十分简单,这些数据结构的实现方式对于Java程序员是十分重要的。程序员可以根据需求,在开发过程中灵活选择使用这些数据结构,从而更好地解决问题。