Detailed explanation of stacks and queues in Java data structures
This article mainly introduces the relevant information of Java data structure stack and queue. Here are implementation examples of stack and queue in Java to help everyone understand and learn the data structure. Friends in need can refer to it
java data structure stack and queue
1: Column alignment
The queue is a first-in, first-out data Structure
Implementation code:
package Queue; /* * 使用java构建队列,并模拟实现队列的入队和出对方法 */ public class Queue { //队列类 private int maxSize; //定义队列的长度 private int[] arrQueue; //队列 private int rear; //定义队列的尾指针 private int front; //定义队列的头指针 private int empty; //元素的个数 public Queue(int s) //初始化构造函数 { maxSize = s; arrQueue = new int[s]; rear = -1; front=0; empty = 0; } //实现插入方法 public void insert(int m) { if(rear == maxSize-1) //处理循环 rear = -1; arrQueue[++rear] = m; //对尾指针加一,把值放在队列结尾 empty++; //队列元素个数加1 System.out.println("队列入队元素 为:" + m); } //实现出栈的方法,即取得队列的头元素 public int remove() { int temp = arrQueue[front++]; //将栈顶元素赋值给temp,栈顶指针加1 if(front == maxSize) //处理循环 front = 0; empty--; //元素个数-1 return temp; } //判断队列是否为空 public boolean isEmpty() { return (empty==0); } //判断对列是否为满 public boolean isFull() { return (empty == maxSize); } //返回队列长度 public int qLong() { return empty; } public static void main(String[] args) { Queue q = new Queue(5); //初始化队列为5个元素 q.insert(1); q.insert(2); q.insert(3); q.insert(4); q.insert(5); int t1 = q.remove(); System.out.println("队列元素出队:" + t1); int t2 = q.remove(); System.out.println("队列元素出队:" + t2); System.out.println("队列是否为空:" + q.isEmpty()); System.out.println("队列是否为满:" + q.isFull()); System.out.println("队列的长度:" + q.qLong()); } }
2: Stack
The stack is a first-in, last-out data structure
1: Use an array to simulate the stack
package Statck; /* * 使用java构建栈,并模拟实现栈的入栈和出栈方法 * 使用数组实现 */ public class Statck1 { private int maxSize; //栈的最多元素数 private int top; //栈顶指针 private int len; //栈的深度 private int[] arrStack; // 模拟栈 //栈的初始化 public Statck1(int s){ maxSize = s; len =0; top= -1; arrStack = new int[s]; } //获取栈的长度 public int getLen(){ return len; } //获取当前栈还能插入多少个f元素 public int getLeaveLen(){ return (maxSize-len); } //判断栈是否满 public boolean isFull(){ return (len==maxSize); } //判断栈是否为空 public boolean isEmpty(){ return (len ==0); } //元素入栈 public void inStack(int s) { arrStack[++top] = s; //栈顶指针加1,入栈 System.out.println("元素入栈:" + s); len ++ ;//栈深度+1 } //元素出栈 public int outStack() { int temp = arrStack[top--];//赋值之后减1 System.out.println("元素出栈:" + temp); len--; //栈深度-1 return temp; } public static void main(String[] args) { Statck1 s = new Statck1(5); s.inStack(1); s.inStack(2); s.inStack(3); s.inStack(4); s.inStack(5); s.outStack(); s.outStack(); System.out.println("栈的长度:" + s.getLen()); System.out.println("还能入栈元素个数:" + s.getLeaveLen()); System.out.println("栈的是否为空:" + s.isEmpty()); System.out.println("栈的是否为满:" + s.isFull()); } }
2: Use linked list to simulate stack
package Statck; import java.util.ArrayList; import java.util.EmptyStackException; import java.util.List; /* * 使用java构建栈,并模拟实现栈的入栈和出栈方法 * 使用链表实现 */ public class Statck2<E extends Object> { private List<E> statck = new ArrayList<E>(); public Statck2(){ //栈的初始化 } //清空栈 public void clear(){ statck.clear(); System.out.println("清空栈.........."); } //判断栈是否为空 public boolean isEmpty(){ return statck.isEmpty(); } //获取栈顶元素 public E getTop(){ if(isEmpty()) return null; return statck.get(0); } //弹出栈操作 public E pop(){ if (isEmpty()) throw new EmptyStackException(); System.out.println(statck.size() + "\t 出栈"); return statck.remove(statck.size() - 1); } //压入栈操作 public void push(E e){ statck.add(e); System.out.println(e + "\t 入栈"); } //获取当前栈的深度 public int getStatckSize(){ if(isEmpty()) throw new EmptyStackException(); return statck.size(); } public static void main(String[] args) { Statck2 s = new Statck2(); s.clear(); //清空栈 System.out.println("当前栈是否为空:" + s.isEmpty()); s.push(1); s.push(2); s.push(3); s.pop(); System.out.println("当前栈的深度为:" + s.getStatckSize()); System.out.println("当前栈顶元素为:" + s.getTop()); } }
The above is the detailed content of Detailed explanation of stacks and queues in Java data structures. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
