当前位置:网站首页>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
边栏推荐
- Use of push() and pop()
- What are the common proxy IP problems?
- Hyperscan -- 2 compilation
- 013_基于Session实现短信验证码登录流程分析
- 【Chrome扩展程序】content_script的跨域问题
- Shardingsphere read write separation
- 从开源爱好者到 Apache 董事,一共分几步?
- [chrome extender] content_ Cross domain problem of script
- 2018 China Collegiate Programming Contest - Guilin Site J. stone game
- The importance of ERP integration to the improvement of the company's system
猜你喜欢

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

What is a dial-up server and what is its use?

Realize linear regression with tensorflow (including problems and solutions in the process)

MySQL C language connection

Lane cross domain problem

arduino esp8266 网络升级 OTA

Is the availability of proxy IP equal to the efficiency of proxy IP?

89 régression logistique prédiction de la réponse de l'utilisateur à l'image de l'utilisateur

Shardingsphere broadcast table and binding table

How many steps are there from open source enthusiasts to Apache directors?
随机推荐
PTA: 浪漫倒影 [二叉树重建] [深度优先遍历]
006_redis_jedis快速入门
Analyze the three functions of static proxy IP.
Tp6 Alibaba cloud SMS window reports curl error 60: SSL certificate problem: unable to get local issuer certificate
easyswoole环境配置
Campus transfer second-hand market source code
Shardingsphere read write separation
006_redis_SortedSet类型
Log4j2 configuration
What is a makefile file?
Analyze the advantages and disadvantages of tunnel proxy IP.
006_ redis_ Jedis quick start
Lane cross domain problem
【ValueError: math domain error】
PHP sorting of interview questions on April 20, 2022
011_RedisTemplate操作Hash
Nanny level tutorial on building personal home page (II)
How to call out services in idea and display the startup class in services
Unicorn bio raised $3.2 million to turn prototype equipment used to grow meat into commercial products
How to write the resume of Software Test Engineer so that HR can see it?