当前位置:网站首页>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
边栏推荐
- tp6阿里云短信 window 报 cURL error 60: SSL certificate problem: unable to get local issuer certificate
- Is the sinking coffee industry a false prosperity or the eve of a broken situation?
- [NK] Niuke monthly race 48 D
- 009_ Redis_ Getting started with redistemplate
- PHP sorting of interview questions on April 20, 2022
- C standard library - < time h>
- Use Xdebug breakpoint debugging in postman
- 002_ Redis_ Common operation commands of string type
- JSP page nesting
- 006_redis_SortedSet类型
猜你喜欢

89 logistic回归用户画像用户响应度预测

Leetcode39 combined sum

How to choose a good dial-up server?

Chinese scientists reveal a new mechanism for breaking through the bottleneck of rice yield

【无标题】

What are the test steps of dynamic proxy IP?

Micro build low code zero foundation introductory course

012_ Access denied for user ‘root‘@‘localhost‘ (using password: YES)

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

How does Axure set the content of the text box to the current date when the page is loaded
随机推荐
How does Axure set the content of the text box to the current date when the page is loaded
How to choose a good dial-up server?
Kubernetes cluster installation based on Kirin SP10 server version
Analyze the advantages and disadvantages of tunnel proxy IP.
010_ StringRedisTemplate
What is BGP server and what are its advantages?
006_redis_jedis快速入门
Latin goat (20204-2022) - daily question 1
How to set computer IP?
Hyperscan -- 2 compilation
RT_Thread自问自答
Common formatting problems after word writing
[nk]牛客月赛48 D
数仓建表111111
校园转转二手市场源码
009_Redis_RedisTemplate入门
Some tips for using proxy IP.
89 régression logistique prédiction de la réponse de l'utilisateur à l'image de l'utilisateur
手写内存池以及原理代码分析【C语言】
The importance of ERP integration to the improvement of the company's system