当前位置:网站首页>Day18 -- stack queue
Day18 -- stack queue
2022-04-23 02:14:00 【lxl513513】
1. queue
A queue is a special kind of linear table , What's special about this is that it's only allowed at the front of the table (front) Delete operation , And at the back end of the table (rear) Insert operation , Like the stack , A queue is a linear table with restricted operations . The end of the insertion operation is called the tail of the queue , The end of the delete operation is called the queue head .
2. The team
- Create a new node
- Connect the new node to the end of the queue
- Then the tail pointer points to the new node
public void enqueue(int paraValue) {
Node tempNode = new Node(paraValue);
tail.next = tempNode;
tail = tempNode;
}// of enqueue
3. Out of the stack
- First, judge whether the queue is empty
- Then the queue starts at the head of the team and leaves the team
- After leaving the team , Judge header Of next Is it empty , If it is empty , This queue is empty , take 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. Code implementation
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
// The team , The operation of joining the team will be carried out at the end of the team
//1. Create a new node , Then connect the new node to the end of the team , Point the tail pointer to the new node
public void enqueue(int paraValue) {
Node tempNode = new Node(paraValue);
tail.next = tempNode;
tail = tempNode;
}// of enqueue
// Out of the team , Head to head operation after leaving the team
//1. Determines if the queue is empty
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;
// After leaving the team , Judge header Of next Is it empty , If it is empty , This queue is empty , take 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
Running results
版权声明
本文为[lxl513513]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230212331506.html
边栏推荐
- Go language ⌈ mutex and state coordination ⌋
- 都是做全屋智能的,Aqara和HomeKit到底有什么不同?
- Develop a chrome plug-in from 0 (2)
- 我国科学家揭示突破水稻产量瓶颈新机制
- 一加一为什么等于二
- 每日一题(2022-04-21)——山羊拉丁文
- A simple and open source navigation website source code
- Dynamic batch processing and static batch processing of unity
- JSP page nesting
- They are all intelligent in the whole house. What's the difference between aqara and homekit?
猜你喜欢
想体验HomeKit智能家居?不如来看看这款智能生态
每日一题(2022-04-22)——旋转函数
What is BGP server and what are its advantages?
006_ redis_ Jedis quick start
005_ redis_ Set set
Micro build low code zero foundation introductory course
World Book Day 𞓜 a good book that technicians should not miss (it cutting-edge technology)
89 régression logistique prédiction de la réponse de l'utilisateur à l'image de l'utilisateur
Leetcode46 Full Permutation
How to set computer IP?
随机推荐
What problems will you encounter when dialing VPS?
005_ redis_ Set set
[chrome extender] content_ Cross domain problem of script
010_ StringRedisTemplate
PHP sorting of interview questions on April 20, 2022
Unicorn bio raised $3.2 million to turn prototype equipment used to grow meat into commercial products
What categories do you need to know before using proxy IP?
都是做全屋智能的,Aqara和HomeKit到底有什么不同?
今天终于会写System.out.println()了
Network jitter tool clumsy
arduino esp8266 网络升级 OTA
Nanny level tutorial on building personal home page (II)
How many steps are there from open source enthusiasts to Apache directors?
89 régression logistique prédiction de la réponse de l'utilisateur à l'image de l'utilisateur
Is it better to use a physical machine or a virtual machine to build a website?
Consider defining a bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs‘
Tp6 Alibaba cloud SMS window reports curl error 60: SSL certificate problem: unable to get local issuer certificate
Redis memory recycling strategy
关于局域网浅谈
R language advanced | generalized vector and attribute analysis