Fundamentals 8 min read

Implementing Stack, Queue, and Priority Queue in Java

This article explains the core concepts of stacks, queues, and priority queues, discusses their time complexities, and provides complete Java implementations using arrays, including methods for insertion, removal, peeking, and display, along with a circular array queue example.

Java Captain
Java Captain
Java Captain
Implementing Stack, Queue, and Priority Queue in Java

The article introduces classic data structures—stack, queue, and priority queue—highlighting their importance for developers and noting that while modern code often relies on library implementations, understanding the underlying mechanisms remains essential.

It describes the stack as a LIFO (last‑in‑first‑out) structure where only the top element can be accessed, and presents a full Java implementation using an array, covering fields, constructor, push, pop, peek, size, and helper methods.

class ArrayStack {
    private long[] a;
    private int size; // stack array size
    private int top;  // top index
    public ArrayStack(int maxSize) {
        this.size = maxSize;
        a = new long[size];
        top = -1; // empty stack
    }
    public void push(long value) { // push operation
        if (isFull()) {
            System.out.println("栈已满!");
            return;
        }
        a[++top] = value;
    }
    public long peek() { // return top without removing
        if (isEmpty()) {
            System.out.println("栈中没有数据");
            return 0;
        }
        return a[top];
    }
    public long pop() { // remove and return top
        if (isEmpty()) {
            System.out.println("栈中没有数据!");
            return 0;
        }
        return a[top--];
    }
    public int size() {
        return top + 1;
    }
    public boolean isEmpty() {
        return (top == -1);
    }
    public boolean isFull() {
        return (top == size - 1);
    }
    public void display() {
        for (int i = top; i >= 0; i--) {
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }
}

Next, the article moves to queues, explaining that a simple array‑based queue suffers from the “false full” problem when the rear reaches the end, and therefore introduces a circular array implementation called RoundQueue . The implementation tracks front , rear , size , and nItems , and provides insert , remove , peek , isEmpty , isFull , size , and display methods.

public class RoundQueue {
    private long[] a;
    private int size;      // array size
    private int nItems;    // actual number of stored items
    private int front;     // head index
    private int rear;      // tail index
    public RoundQueue(int maxSize) {
        this.size = maxSize;
        a = new long[size];
        front = 0;
        rear = -1;
        nItems = 0;
    }
    public void insert(long value) {
        if (isFull()) {
            System.out.println("队列已满");
            return;
        }
        rear = ++rear % size;
        a[rear] = value;
        nItems++;
    }
    public long remove() {
        if (isEmpty()) {
            System.out.println("队列为空!");
            return 0;
        }
        long temp = a[front];
        front = (front + 1) % size;
        nItems--;
        return temp;
    }
    public long peek() {
        if (isEmpty()) {
            System.out.println("队列为空!");
            return 0;
        }
        return a[front];
    }
    public boolean isEmpty() {
        return (nItems == 0);
    }
    public boolean isFull() {
        return (nItems == size);
    }
    public int size() {
        return nItems;
    }
    public void display() {
        for (int i = 0; i < nItems; i++) {
            System.out.print(a[(front + i) % size] + " ");
        }
        System.out.println();
    }
}

Finally, the article presents a priority queue implementation, also based on an array, where elements are kept sorted so that the smallest (or largest) key is always at the front. Insertion requires O(N) time to maintain order, while removal and peek operations are O(1). The class PriorityQueue includes methods for insert , remove , peekMin , and the usual utility methods.

public class PriorityQueue {
    private long[] a;
    private int size;
    private int nItems; // number of elements
    public PriorityQueue(int maxSize) {
        this.size = maxSize;
        nItems = 0;
        a = new long[size];
    }
    public void insert(long value) {
        if (isFull()) {
            System.out.println("队列已满!");
            return;
        }
        if (nItems == 0) {
            a[nItems++] = value;
            return;
        }
        int j;
        for (j = nItems - 1; j >= 0; j--) {
            if (value > a[j]) {
                a[j + 1] = a[j];
            } else {
                break;
            }
        }
        a[j + 1] = value;
        nItems++;
    }
    public long remove() {
        if (isEmpty()) {
            System.out.println("队列为空!");
            return 0;
        }
        return a[--nItems];
    }
    public long peekMin() {
        if (isEmpty()) {
            System.out.println("队列为空!");
            return 0;
        }
        return a[nItems - 1];
    }
    public boolean isEmpty() {
        return (nItems == 0);
    }
    public boolean isFull() {
        return (nItems == size);
    }
    public int size() {
        return nItems;
    }
    public void display() {
        for (int i = nItems - 1; i >= 0; i--) {
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }
}

The article notes that stack operations (push/pop) run in O(1) time, queue operations (insert/remove) also run in O(1) when using the circular array, and priority‑queue insertion runs in O(N) while removal runs in O(1). It concludes with a brief reminder that these data structures form the foundation of many algorithms and systems.

JavaalgorithmPriority QueueStackData StructuresQueue
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.