当前位置:网站首页>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
边栏推荐
- Campus transfer second-hand market source code
- 从开源爱好者到 Apache 董事,一共分几步?
- leetcode:27. Remove element [count remove]
- Want to experience homekit smart home? Why don't you take a look at this smart ecosystem
- PHP & laravel & master several ways of generating token by API and some precautions (PIT)
- 想体验HomeKit智能家居?不如来看看这款智能生态
- 009_Redis_RedisTemplate入门
- JDBC cannot connect to MySQL, and the error is access denied for user 'root' @ '* * *' (using password: Yes)
- 89 régression logistique prédiction de la réponse de l'utilisateur à l'image de l'utilisateur
- 关于局域网浅谈
猜你喜欢

Leetcode40 - total number of combinations II

Halo open source project learning (I): project launch

使用代理IP是需要注意什么?

Easyswool environment configuration

PTA: praise the crazy devil

C语言中*与&的用法与区别 以及关键字static和volatile 的含义

每日一题(2022-04-21)——山羊拉丁文

What is a proxy IP pool and how to build it?

001_redis设置存活时间

Common formatting problems after word writing
随机推荐
小程序 读取文件
007_ Redis_ Jedis connection pool
2018 China Collegiate Programming Contest - Guilin Site J. stone game
Summary of I / O knowledge points
tp6阿里云短信 window 报 cURL error 60: SSL certificate problem: unable to get local issuer certificate
Gray scale range corresponding to colors (red, yellow, green, blue, purple, pink, brick red and magenta) in HSV color space
easyswoole环境配置
Daily question (April 22, 2022) - rotation function
我国科学家揭示突破水稻产量瓶颈新机制
011_RedisTemplate操作Hash
【dpdk】10. Dpdk DNS learning notes
LeetCode 349. Intersection of two arrays (simple, array) Day12
校园转转二手市场源码
How to configure iptables to realize local port forwarding
Thinkphp内核开发盲盒商城源码v2.0 对接易支付/阿里云短信/七牛云存储
Multithreading technology core
每日一题(2022-04-22)——旋转函数
Latin goat (20204-2022) - daily question 1
What categories do you need to know before using proxy IP?
LeetCode 283. Move zero (simple, array) Day12