当前位置:网站首页>栈和队列的基本使用
栈和队列的基本使用
2022-06-23 12:53:00 【Fly upward】
目录
1.栈(Stack)
1.1概念
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作,进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中元素遵守“先进后出”的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,如数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据在栈顶

1.2 栈的实现
1)利用顺序表实现,即使用尾插+尾删的方式实现
2)利用链表实现,则头尾都可以
因为顺序表实现上更为简单,此处使用顺序表实现
1.创建 MyStack 类
import java.util.Arrays;
/**
* 栈的实现
*/
public class MyStack {
public int[] elem;
public int usedSize;
public MyStack() {
this.elem = new int[5];
}
//压栈
public void push(int val) {
if(isFull()) {
//满了就2倍扩容
this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
}
this.elem[this.usedSize] = val;
this.usedSize++;
}
public boolean isFull() {
return this.usedSize == this.elem.length;
}
//出栈,pop 会删除栈元素,peek 获取栈元素,不删除
public int pop() {
if(isEmpty()) {
throw new RuntimeException("栈为空!");
}
int oldVal = this.elem[usedSize-1];
this.usedSize--;
return oldVal;
}
public int peek() {
if(isEmpty()) {
throw new RuntimeException("栈为空!");
}
int oldVal = this.elem[usedSize-1];
return oldVal;
}
//判断栈是否为空
public boolean isEmpty() {
return this.usedSize == 0;
}
}2.main 函数
import java.util.Stack;
/**
* 栈,先进后出
*/
public class StackTest {
public static void main(String[] args) {
MyStack stack = new MyStack();
//压栈
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.push(6);
//出栈,pop 是出一个栈顶元素并且在栈中删除
System.out.println(stack.pop());//6
// peek 是获取栈顶元素但不删除栈中元素
System.out.println(stack.peek());//5
System.out.println(stack.peek());//5
System.out.println(stack.isEmpty());//false
}
}3.运行结果

2.队列(Queue)
2.1 概念
2.2 单链表实现队列

单链表实现
/**
* 队列的实现,使用单链表
*/
class Node {
public int val;
public Node next;
public Node(int val) {
this.val = val;
}
}
public class MyQueue {
public Node head;
public Node last;
/**
* 添加队列元素
* 使用单链表尾插法
* @param val
*/
public void offer(int val) {
Node node = new Node(val);
if (head == null) {
head = node;
last = node;
}else {
last.next = node;
last = last.next;
}
}
/**
* 出队
* @return
*/
public int poll() {
if (isEmpty()) {
throw new RuntimeException("队列为空");
}
int oldVal = head.val;
this.head = head.next;
return oldVal;
}
public boolean isEmpty() {
return this.head == null;
}
/**
* 获取队列元素
* @return
*/
public int peek() {
if (isEmpty()) {
throw new RuntimeException("队列为空");
}
return head.val;
}
}main 方法
import java.util.Queue;
public class QueueTest {
public static void main(String[] args) {
MyQueue queue = new MyQueue();
//入队列
queue.offer(1);
queue.offer(2);
queue.offer(3);
//获取队列元素
System.out.println(queue.peek());//1
//出队
System.out.println(queue.poll());//1
System.out.println(queue.poll());//2
System.out.println(queue.poll());//3
}
}效果

2.3 循环队列
实际中我们有时还会使用一种队列叫循环队列。如操作系统在生产者消费者模型时可以就会使用循环队列。环形队列通常使用数组实现。


代码
/**
* 设计循环队列
*/
public class MyCircularQueue {
public int[] elem;
public int front;//队头下标
public int rear;//队尾下标
public MyCircularQueue(int k) {
this.elem = new int[k + 1];
}
/**
* 入队
* @param value
* @return
*/
public boolean enQueue(int value) {
if (isFull()) return false;
this.elem[rear] = value;
rear = (rear+1) % elem.length;
return true;
}
/**
* 出队
* @return
*/
public boolean deQueue() {
if (isEmpty()) return false;
front = (front+1) % elem.length;
return true;
}
/**
* 得到对头元素
* @return
*/
public int Front() {
if (isEmpty()) return -1;
return elem[front];
}
/**
* 队尾元素
* @return
*/
public int Rear() {
if (isEmpty()) return -1;
int index = -1;
if (rear == 0) {
index = elem.length - 1;
}else {
index = rear - 1;
}
return elem[index];
}
public boolean isEmpty() {
return front == rear;
}
public boolean isFull() {
if((this.rear+1) % elem.length == front) {
return true;
}
return false;
}
}2.4双端队列
3.java 中的栈和队列
方法 | 解释 |
E push(E item) | 压栈 |
E pop() | 出栈 |
E peek() | 查看栈顶元素 |
| boolean empty() | 查看栈是否为空 |
Queue
| 入队列 | add(e) | offer(e) |
| 出队列 | remove() | poll() |
| 队首元素 | element() | peek() |
| 错误处理 | 抛出异常 | 返回特殊值 |
边栏推荐
- 90%的人都不懂的泛型,泛型的缺陷和应用场景
- Qunhui 10 Gigabit network configuration and test
- Dataset之GermanCreditData:GermanCreditData数据集的简介、下载、使用方法之详细攻略
- How do the top ten securities firms open accounts? Is online account opening safe?
- ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.
- Tuikit audio and video low code solution navigation page
- R language dplyr package arrange function sorts dataframe data and sorts dataframe data through multiple data columns (ascending sort by default)
- Network foundation and framework
- When did the redo log under InnoDB in mysql start to perform check point disk dropping?
- The project experience of resume and several problems that testers should pay attention to in writing
猜你喜欢
![[website architecture] the unique skill of 10-year database design, practical design steps and specifications](/img/f2/061fa6dd42e57a121401e4f0cf1865.png)
[website architecture] the unique skill of 10-year database design, practical design steps and specifications

Analysis and solution of connection failure caused by MySQL using replicationconnection

Online text filter less than specified length tool

Stimulsoft Ultimate Reports 2022.3.1

Quickly understand the commonly used asymmetric encryption algorithm, and no longer have to worry about the interviewer's thorough inquiry

Architecture design methods in technical practice

90%的人都不懂的泛型,泛型的缺陷和应用场景

有向图D和E

618的省钱技术攻略 来啦 -体验场景 领取10元猫超卡!

Oracle中dbms_output.put_line怎么使用
随机推荐
MySQL使用ReplicationConnection導致的連接失效分析與解决
State machine framework
New project, how to ensure the coverage of the test?
Online text entity extraction capability helps applications analyze massive text data
Ablebits Ultimate Suite for Excel
Go write permissions to file writefile (FileName, data, 0644)?
#yyds干货盘点# 解决剑指offer: 判断是不是平衡二叉树
Cloud native essay deep understanding of ingress
Have you ever encountered incompatibility between flink1.15.0 and Flink CDC MySQL 2.2.1? f
【网站架构】10年数据库设计浓缩的绝技,实打实的设计步骤与规范
Restcloud ETL resolves shell script parameterization
kubernetes comfig subpath
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.
What is the version of version 1.54 when connecting to Oracle?
R language uses the polR function of mass package to build an ordered multi classification logistic regression model, and uses the summary function to obtain the summary statistical information of the
那些技术实战中的架构设计方法
R language uses the multinom function of NNET package to build a disordered multi classification logistic regression model, uses regression coefficients and their standard errors to calculate the valu
R语言dplyr包arrange函数排序dataframe数据、通过多个数据列排序dataframe数据(默认是升序排序)
在線文本過濾小於指定長度工具
R language dplyr package mutate_ The all function multiplies all numeric columns (variables) in the dataframe by a fixed value to generate a new data column, and specifies a user-defined suffix name f