Fundamentals 13 min read

Queue and Stack Concepts, Implementations, and Applications in Java

This article explains the definitions, characteristics, and overflow cases of sequential and circular queues and stacks, provides Java implementations for these data structures, demonstrates their usage with sample code and results, and discusses practical applications of queue thinking in database-driven workflows.

政采云技术
政采云技术
政采云技术
Queue and Stack Concepts, Implementations, and Applications in Java

Definition and Concepts

Sequential Queue

A queue is a special linear list that only allows deletion at the front (front) and insertion at the rear (rear), following the FIFO (first‑in‑first‑out) principle.

Queue characteristics: FIFO.

Three overflow situations:

Underflow: the queue is empty and a dequeue operation is performed (used for logical checks).

True overflow: the queue is full and an enqueue operation is attempted (error, must be avoided).

False overflow: the queue appears full because the head pointer only increments and never decrements, leaving unused space; this can be solved with a circular queue.

Circular Queue

A circular queue wraps the last storage position back to the first, forming a logical ring so that the queue can reuse space.

When the queue is empty, front == rear . When the queue is full, front == (rear + 1) % MaxSize . To distinguish empty from full, the maximum number of elements is MaxSize‑1 .

Example operations: enqueue a,b,c,d,e; dequeue a,b; enqueue f causing false overflow; after wrap‑around the queue can continue without loss of space.

Stack

A stack is a linear structure that allows insertion and deletion only at one end (the top), following LIFO (last‑in‑first‑out).

Empty condition: top == -1 . Full condition: top == maxSize‑1 .

Overflow situations:

Underflow: stack is empty and a pop operation is performed (used for logical checks).

True overflow: stack is full and a push operation is attempted (error).

Algorithm Implementation

Circular Queue (Java)

import java.util.Arrays;
import java.util.stream.Collectors;

public class CircleQueue {
    /**
     * array length
     */
    private int maxSize;
    /**
     * front pointer
     */
    private int front;
    /**
     * rear pointer
     */
    private int rear;
    private String[] queue;

    /**
     * Initialize circular queue
     * @param objSize number of elements
     */
    public CircleQueue(int objSize) {
        // reserve one extra slot to distinguish empty from full
        this.maxSize = objSize + 1;
        this.front = 0;
        this.rear = 0;
        this.queue = new String[this.maxSize];
    }

    /**
     * Is the queue empty?
     */
    public boolean isEmpty() {
        return front == rear;
    }

    /**
     * Is the queue full?
     */
    public boolean isFull() {
        return front == (rear + 1) % maxSize;
    }

    /**
     * Enqueue element
     */
    public void add(String a) {
        if (isFull()) {
            System.out.println("队列满");
            return;
        }
        queue[rear % maxSize] = a;
        rear = (rear + 1) % maxSize;
    }

    /**
     * Dequeue element
     */
    public String remove() {
        if (isEmpty()) {
            System.out.println("队列空");
            return null;
        }
        String a = queue[front % maxSize];
        queue[front % maxSize] = null;
        front = (front + 1) % maxSize;
        return a;
    }

    public static void main(String[] args) {
        // simulate a queue of size 4
        CircleQueue circleQueue = new CircleQueue(4);
        System.out.print("a,b,c入队:");
        circleQueue.add("a");
        circleQueue.add("b");
        circleQueue.add("c");
        System.out.println(Arrays.stream(circleQueue.queue).collect(Collectors.toList()) + "front=" + circleQueue.front + ";rear" + circleQueue.rear);
        // ... further operations omitted for brevity ...
    }
}

Sequential Stack (Java)

package cn.gov.zcy.announcement;

import java.util.Arrays;
import java.util.stream.Collectors;

public class Stack {
    // array length
    private int maxSize;
    /**
     * top pointer
     */
    private int top;
    private String[] stack;

    /**
     * Initialize stack
     * @param objSize number of elements
     */
    public Stack(int objSize) {
        maxSize = objSize;
        top = -1;
        stack = new String[maxSize];
    }

    /**
     * Is the stack empty?
     */
    public boolean isEmpty() {
        return top == -1;
    }

    /**
     * Is the stack full?
     */
    public boolean isFull() {
        return top == maxSize - 1;
    }

    /**
     * Push element onto stack
     */
    public void push(String a) {
        if (isFull()) {
            System.out.println("栈满");
            return;
        }
        top = top + 1;
        stack[top] = a;
    }

    /**
     * Pop element from stack
     */
    public String pop() {
        if (isEmpty()) {
            System.out.println("栈空");
            return null;
        }
        String a = stack[top];
        top = top - 1;
        return a;
    }

    public static void main(String[] args) {
        // simulate a stack of size 4
        Stack test = new Stack(4);
        System.out.print("a,b,c,d入栈:");
        test.push("a");
        test.push("b");
        test.push("c");
        test.push("d");
        System.out.println(Arrays.stream(test.stack).collect(Collectors.toList()) + ";top=" + test.top);
        // ... further operations omitted for brevity ...
    }
}

Queue Thought Application Practice

Application Background

A batch of already published announcement data needs to be pushed, and the push time must be at least ten minutes after publication.

Implementation Medium

The queue is persisted in a database table.

FIFO Realisation

Records are ordered by modification time in ascending order.

Enqueue Operation

Insert a new row into the database table.

Dequeue Operation

Delete the row from the database table.

Re‑enqueue Operation

Update the modification time so that processed or error‑prone records can be re‑queued for later scheduled handling.

Summary

The definitions of queues and stacks are straightforward, but their underlying ideas have been wrapped in various media and are widely applied in practice.

References

1. Baidu Baike.

Recruitment Notice

The article concludes with an invitation to follow the public account and a recruitment call for engineers interested in cloud‑native, blockchain, AI, low‑code platforms, middleware, big data, and other cutting‑edge technologies.

JavaAlgorithmStackfundamentalsQueuecircular queue
政采云技术
Written by

政采云技术

ZCY Technology Team (Zero), based in Hangzhou, is a growth-oriented team passionate about technology and craftsmanship. With around 500 members, we are building comprehensive engineering, project management, and talent development systems. We are committed to innovation and creating a cloud service ecosystem for government and enterprise procurement. We look forward to your joining us.

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.