当前位置:网站首页>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
边栏推荐
- Nanny level tutorial on building personal home page (II)
- Introduction to micro build low code zero Foundation (lesson 2)
- Gray scale range corresponding to colors (red, yellow, green, blue, purple, pink, brick red and magenta) in HSV color space
- 006_ redis_ Sortedset type
- Flink real-time data warehouse project - Design and implementation of DWS layer
- If 404 page is like this | daily anecdotes
- Consider defining a bean of type 'com netflix. discovery. AbstractDiscoveryClientOptionalArgs‘
- 2018 China Collegiate Programming Contest - Guilin Site J. stone game
- Unity editor hierarchy drop-down menu extension
- 【汇编语言】从最底层的角度理解“堆栈”
猜你喜欢

Leetcode39 combined sum

LeetCode 447. Number of boomerangs (permutation and combination problem)

Use Xdebug breakpoint debugging in postman

Dynamic memory management

用TensorFlow实现线性回归(包括过程中出现的问题及解决方法)

007_ Redis_ Jedis connection pool

Shardingsphere broadcast table and binding table

89 régression logistique prédiction de la réponse de l'utilisateur à l'image de l'utilisateur
Unicorn bio raised $3.2 million to turn prototype equipment used to grow meat into commercial products

011_ Redistemplate operation hash
随机推荐
LeetCode 447. Number of boomerangs (permutation and combination problem)
【Chrome扩展程序】content_script的跨域问题
What business scenarios will the BGP server be used in?
89 logistic regression user portrait user response prediction
Numerical remapping method (remap)
011_RedisTemplate操作Hash
Unity editor hierarchy drop-down menu extension
Makefile文件是什麼?
Dynamic memory management
Why is one plus one equal to two
Tp6 Alibaba Cloud SMS Window message Curl Error 60: SSL Certificate Problem: Unable to get local issuer Certificate
[Dahua cloud native] micro service chapter - service mode of five-star hotels
都是做全屋智能的,Aqara和HomeKit到底有什么不同?
LeetCode 283. Move zero (simple, array) Day12
002_ Redis_ Common operation commands of string type
C # import details
002_Redis_String类型常见的操作命令
tp6阿里云短信 window 报 cURL error 60: SSL certificate problem: unable to get local issuer certificate
Lane cross domain problem
Some tips for using proxy IP.