学习来源:https://blog.csdn.net/minfanphd/article/details/116974889
循环队列
1 整除的作用.
2 想像操场跑道里放一队人, 循环的感觉就出来了.
3 为了区分空队列与满队列, 需要留一个空间. 相当于不允许首尾相连. 还是画个图吧, 否则容易进坑.
4 用链式结构, 空间的分配与回收由系统做, 用循环队列, 则是自己把控. 想像自己写的是操作系统, 从这个代码可以感受下内存的管理.
package datastructure;
/**
* @time 2022/4/6
* @author Liang Huang
*/
public class CircleIntQueue {
/**
* The total space. One space can never be used.
*/
public static final int TOTAL_SPACE = 10;
/**
* The data.
*/
int[] data;
/**
* The index for calculating the head. The actual head is head % TOTAL_SPACE.
*/
int head;
/**
* The index for calculating the tail.
*/
int tail;
/**
* The constructor.
*/
public CircleIntQueue() {
data = new int[TOTAL_SPACE];
head = 0;
tail = 0;
}//Of the first constructor
/**
**************
*Enqueue.
*
* @param paraValue The given value.
**************
*/
public void enqueue(int paraValue) {
if((tail + 1) % TOTAL_SPACE == head) {
System.out.println("Queue full.");
return;
}//Of if
data[tail % TOTAL_SPACE] = paraValue;
tail++;
}//Of enqueue
/**
************
* Dequeue
*
* @return The value of the head.
************
*/
public int dequeue() {
if(head == tail) {
System.out.println("No element in this queue.");
return -1;
}//Of if
int resultValue = data[head % TOTAL_SPACE];
head++;
return resultValue;
}//Of dequeue
/**
* Overrides the method claimed in Object, the superclass of any class.
*/
public String toString() {
String resultString = "";
if(head == tail) {
return "empty";
}//Of if
for(int i=head; i<tail; i++) {
resultString += data[i % TOTAL_SPACE] + ", ";
}//Of for i
return resultString;
}//Of toSting
/**
* ********
* The entrance of the program.
*
* @param args not used now.
*/
public static void main(String[] args) {
CircleIntQueue tempQueue = new CircleIntQueue();
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 list is: " + tempQueue.toString());
int tempValue = tempQueue.dequeue();
System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());
for(int i=0; i<6; i++) {
tempQueue.dequeue();
System.out.println("Enqueue, the queue is: " + tempQueue.toString());
}//Of for i
for (int i = 0; i < 3; i++) {
tempValue = tempQueue.dequeue();
System.out.println("Dequeue " + tempValue + ", the queue is: " + tempQueue.toString());
} // Of for i
for (int i = 0; i < 6; i++) {
tempQueue.enqueue(i + 100);
System.out.println("Enqueue, the queue is: " + tempQueue.toString());
} // Of for i
}//Of main
}//Of class CircleIntQueue
字符串匹配
1 String 是 Java 常用的类, 这里重新实现下部分功能.
2 转义符 , 有了它才能正常打印引号.
3 简单的越界检查.
package datastructure;
/**
* @time 2022/4/6
* @author Liang Huang
*/
public class MyString {
/**
* The max length.
*/
public static final int MAX_LENGTH = 10;
/**
* The actual length.
*/
int length;
/**
* The data.
*/
char[] data;
/**
* Construct an empty char array.
*/
public MyString() {
data = new char[MAX_LENGTH];
length = 0;
}//Of the first constructor
/**
*********************
* Construct using a system defined string.
*
* @param paraString The given string.
* Its length should not exceed MAX_LENGTH - 1.
*********************
*/
public MyString(String paraString) {
data = new char[MAX_LENGTH];
length = paraString.length();
//Copy data
for(int i=0; i<length; i++) {
data[i] = paraString.charAt(i);
}//Of for i
}//Of the second constructor
/**
* Overrides the method claimed in Object, the superclass of any class.
*/
public String toString() {
String resultString = "";
for (int i = 0; i < length; i++) {
resultString += data[i];
} // Of for i
return resultString;
}//Of toString
/**
*********************
* Locate the position of a substring.
*
* @param paraMyString The given substring.
* @return The first position. -1 for no matching.
*********************
*/
public int locate(MyString paraMyString) {
boolean tempMatch = false;
for(int i=0; i<length - paraMyString.length + 1; i++) {
tempMatch = true;
for(int j=0; j<paraMyString.length; j++) {
if(data[i + j] != paraMyString.data[j]) {
tempMatch = false;
break;
}//Of if
}//Of for j
if(tempMatch) {
return i;
}//Of if
}//Of for i
return -1;
}//Of locate
/**
*********************
* Get a substring获取一个子串
*
* @param paraString The given substring.
* @param paraStartPosition The start position in the original string.
* @param paraLength The length of the new string.
* @return The first position. -1 for no matching.
*********************
*/
public MyString substring(int paraStartPosition, int paraLength) {
if(paraStartPosition + paraLength > length) {
System.out.println("The bound is exceeded.");
return null;
}//Of if
MyString resultMyString = new MyString();
resultMyString.length = paraLength;
for(int i=0; i<paraLength; i++) {
resultMyString.data[i] = data[i + paraStartPosition];
}//Of for i
return resultMyString;
}//Of substring
/**
* ********
* The entrance of the program.
*
* @param args not used now.
*/
public static void main(String[] args) {
MyString tempFirstString = new MyString("I like ik");
MyString tempSecondString = new MyString("ik");
int tempPosition = tempFirstString.locate(tempSecondString);
System.out.println("The position of " + tempSecondString + " in "
+ tempFirstString + " is " + tempPosition);
//查找失败的情况
MyString tempThirdString = new MyString("ki");
tempPosition = tempFirstString.locate(tempThirdString);
System.out.println("The position of " + tempThirdString + " in "
+ tempFirstString + " is " + tempPosition);
tempThirdString = tempFirstString.substring(1, 1);
System.out.println("The substring is: \"" + tempThirdString + "\"");
tempThirdString = tempFirstString.substring(2, 4);
System.out.println("The substring is: \"" + tempThirdString + "\"");
tempThirdString = tempFirstString.substring(5, 5);
System.out.println("The substring is: \"" + tempThirdString + "\"");
tempThirdString = tempFirstString.substring(5, 6);
System.out.println("The substring is: \"" + tempThirdString + "\"");
}//Of main
}//Of class MyString