当前位置:网站首页>day18--栈队列
day18--栈队列
2022-04-23 02:12:00 【lxl513513】
1.队列
队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。
2.入队
- 创建一个新的节点
- 将新节点接在队尾
- 然后队尾指针指向新节点
public void enqueue(int paraValue) {
Node tempNode = new Node(paraValue);
tail.next = tempNode;
tail = tempNode;
}// of enqueue
3.出栈
- 首先判断队列是否为空
- 然后队列从队头开始出队
- 出队后,判断header的next是否为空,如果为空,这该队列为空,将tail=header
public int dequeue() {
if (header == tail) {
System.out.println("No element in the queue");
return -1;
}
int resultValue = header.next.data;
header.next = header.next.next;
if (header.next == null) {
tail = header;
}
return resultValue;
}// of dequeue
4.代码实现
package com.datastructure;
public class LinkedQueue {
class Node {
int data;
Node next;
public Node(int paraValue) {
data = paraValue;
next = null;
}// Of the constructor
}// of class Node
Node header;
Node tail;
public LinkedQueue() {
header = new Node(-1);
tail = header;
}// of the first constructor
//入队,入队操作再队尾进行
//1.创建一个新节点,再将新节点接在队尾,将队尾指针指向新节点
public void enqueue(int paraValue) {
Node tempNode = new Node(paraValue);
tail.next = tempNode;
tail = tempNode;
}// of enqueue
//出队,出队操作再对头进行
//1.判断队列是否为空
public int dequeue() {
if (header == tail) {
System.out.println("No element in the queue");
return -1;
}
int resultValue = header.next.data;
header.next = header.next.next;
// 出队后,判断header的next是否为空,如果为空,这该队列为空,将tail=header
if (header.next == null) {
tail = header;
}
return resultValue;
}// of dequeue
public String toString() {
String resultString = "";
if (header.next == null) {
return "empty";
} // Of if
Node tempNode = header.next;
resultString += tempNode.data;
tempNode = tempNode.next;
while (tempNode != null) {
resultString += ", " + tempNode.data;
tempNode = tempNode.next;
} //of while
return resultString;
}//of toString
public static void main(String args[]) {
LinkedQueue tempQueue = new LinkedQueue();
System.out.println("Initialized, the list is: " + tempQueue.toString());
for (int i = 0; i < 5; i++) {
tempQueue.enqueue(i + 1);
} // of for i
System.out.println("Enqueue, the queue is: " + tempQueue.toString());
tempQueue.dequeue();
System.out.println("Dequeue, the queue is: " + tempQueue.toString());
int tempValue;
for (int i = 0; i < 5; i++) {
tempValue = tempQueue.dequeue();
System.out.println("Looped delete " + tempValue + ", the new queue is: " + tempQueue.toString());
} // of for i
for (int i = 0; i < 3; i++) {
tempQueue.enqueue(i + 10);
} // of for i
System.out.println("Enqueue, the queue is: " + tempQueue.toString());
}//of main
}// of class LinkedQueue
运行结果
版权声明
本文为[lxl513513]所创,转载请带上原文链接,感谢
https://blog.csdn.net/lxl513513/article/details/124355050
边栏推荐
- 009_Redis_RedisTemplate入门
- Analyze the three functions of static proxy IP.
- php 2022年4月20面试题整理
- 配置iptables实现本地端口转发的方法详解
- Analyze the advantages and disadvantages of tunnel proxy IP.
- Latin goat (20204-2022) - daily question 1
- 2018 China Collegiate Programming Contest - Guilin Site J. stone game
- What is a dial-up server and what is its use?
- Esp32 message queue using FreeRTOS
- 从0开始开发一个chrome插件(2)
猜你喜欢
Esp32 message queue using FreeRTOS
009_Redis_RedisTemplate入门
从开源爱好者到 Apache 董事,一共分几步?
Shardingsphere introduction and sub table usage
Heap overflow of kernel PWN basic tutorial
Lane cross domain problem
New book recommendation - IPv6 technology and application (Ruijie version)
Daily question (April 22, 2022) - rotation function
What categories do you need to know before using proxy IP?
010_StringRedisTemplate
随机推荐
tp6阿里云短信 window 报 cURL error 60: SSL certificate problem: unable to get local issuer certificate
World Book Day 𞓜 a good book that technicians should not miss (it cutting-edge technology)
今天终于会写System.out.println()了
Introduction to esp32 Bluetooth controller API
Develop a chrome plug-in from 0 (2)
Micro build low code zero foundation introductory course
002_Redis_String类型常见的操作命令
Gray scale range corresponding to colors (red, yellow, green, blue, purple, pink, brick red and magenta) in HSV color space
Applet reads files
Open3d point cloud processing
Dynamic memory management
Today will finally write system out. Println()
What is a dial-up server and what is its use?
hyperscan --- 1
How to choose a good dial-up server?
Leetcode46 Full Permutation
On LAN
They are all intelligent in the whole house. What's the difference between aqara and homekit?
Flink real-time data warehouse project - Design and implementation of DWS layer
VMware virtual machine installation openwrt as side route single arm route img image to vmdk