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

使用Java函数实现常见数据结构如链表、栈和队列等

发布时间:2023-09-09 13:35:11

Java是一种面向对象编程语言,它提供了丰富的数据结构来解决不同的问题。常见的数据结构包括链表、栈和队列等。在Java中,我们可以使用类和方法来实现这些数据结构。

首先是链表。链表是一种线性数据结构,它由一系列节点组成,每个节点包含数据和指向下一个节点的引用。我们可以通过定义一个Node类来表示链表中的每个节点,然后定义一个LinkedList类来实现链表的操作。

class Node {
    int data;
    Node next;
    
    public Node(int data) {
        this.data = data;
        this.next = null;
    }
}

class LinkedList {
    Node head;
    
    public LinkedList() {
        this.head = null;
    }
    
    // 在链表末尾添加一个节点
    public void append(int data) {
        Node newNode = new Node(data);
        
        if (head == null) {
            head = newNode;
        } else {
            Node current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = newNode;
        }
    }
    
    // 在链表头部添加一个节点
    public void prepend(int data) {
        Node newNode = new Node(data);
        
        if (head == null) {
            head = newNode;
        } else {
            newNode.next = head;
            head = newNode;
        }
    }
    
    // 删除指定值的节点
    public void delete(int data) {
        if (head == null) {
            return;
        }
        
        if (head.data == data) {
            head = head.next;
            return;
        }
        
        Node current = head;
        while (current.next != null && current.next.data != data) {
            current = current.next;
        }
        
        if (current.next != null) {
            current.next = current.next.next;
        }
    }
    
    // 打印链表中的所有节点值
    public void printList() {
        Node current = head;
        while (current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
        System.out.println();
    }
}

接下来是栈。栈是一种先进后出(LIFO)的数据结构,可以通过数组或链表来实现。这里我们使用数组来实现一个简单的栈。

class Stack {
    private int maxSize;
    private int[] stackArray;
    private int top;
    
    public Stack(int maxSize) {
        this.maxSize = maxSize;
        this.stackArray = new int[maxSize];
        this.top = -1;
    }
    
    // 入栈
    public void push(int value) {
        if (top < maxSize - 1) {
            stackArray[++top] = value;
        }
    }
    
    // 出栈
    public int pop() {
        if (top >= 0) {
            return stackArray[top--];
        }
        return -1;
    }
    
    // 返回栈顶元素
    public int peek() {
        if (top >= 0) {
            return stackArray[top];
        }
        return -1;
    }
    
    // 判断栈是否为空
    public boolean isEmpty() {
        return (top == -1);
    }
    
    // 打印栈中的所有元素
    public void printStack() {
        for (int i = 0; i <= top; i++) {
            System.out.print(stackArray[i] + " ");
        }
        System.out.println();
    }
}

最后是队列。队列是一种先进先出(FIFO)的数据结构,也可以通过数组或链表来实现。这里我们使用数组来实现一个简单的队列。

class Queue {
    private int maxSize;
    private int[] queueArray;
    private int front;
    private int rear;
    private int itemCount;
    
    public Queue(int maxSize) {
        this.maxSize = maxSize;
        this.queueArray = new int[maxSize];
        this.front = 0;
        this.rear = -1;
        this.itemCount = 0;
    }
    
    // 入队
    public void enqueue(int value) {
        if (rear == maxSize - 1) {
            rear = -1;
        }
        queueArray[++rear] = value;
        itemCount++;
    }
    
    // 出队
    public int dequeue() {
        int temp = queueArray[front++];
        if (front == maxSize) {
            front = 0;
        }
        itemCount--;
        return temp;
    }
    
    // 返回队头元素
    public int peek() {
        return queueArray[front];
    }
    
    // 判断队列是否为空
    public boolean isEmpty() {
        return (itemCount == 0);
    }
    
    // 打印队列中的所有元素
    public void printQueue() {
        int currentIndex = front;
        for (int i = 0; i < itemCount; i++) {
            System.out.print(queueArray[currentIndex++] + " ");
            if (currentIndex == maxSize) {
                currentIndex = 0;
            }
        }
        System.out.println();
    }
}

通过以上的实现,我们可以使用Java中的类和方法来创建和操作链表、栈和队列等常见的数据结构。这些数据结构在编程中非常常用,能够帮助我们解决各种问题。