資料結構JAVA版之棧和佇列
阿新 • • 發佈:2018-10-31
一、涉及的內容大綱
二、簡單介紹棧、佇列和其他資料結構的不同
1 對於其他的資料結構而言,都適用與資料庫應用中作資料記錄。但是因為棧和佇列的生命週期比那些資料庫型別的資料結構要短,所以他們只是在程式的操作期間才會建立和執行,在完成任務之後就會被銷燬。所以棧和佇列更多的是用於構思演算法的輔助工具。
2 棧和佇列的訪問是受限制的,因為在這些結構介面設計時就增強了這種受限訪問。
3 棧的主要機制可以用陣列來實現,但也可以利用連結串列來實現。在優先順序佇列中,內部的實現可以用陣列,也可以用一種特別的樹——堆來實現。
三、棧
棧的特點:只允許訪問最後插入的資料項,移除這個資料項後,才可以訪問倒數第二個插入的資料項。簡單說就是,先進後出。
棧的基礎操作:壓棧和彈棧
public class MyStack { //設定陣列的範圍 private int maxSize; //建立陣列 private long[] stackArray; //設定棧頂的位置 private int top; //帶參構造方法,初始化各個引數 public MyStack(int s) { maxSize = s; stackArray = new long[maxSize]; //將棧頂的值設定為-1,為了在壓入第一個值時,棧頂正好在下標0 top = -1; } /** * 壓棧 * @param j */ public void push(long j) { //先對棧頂的下標值增加,然後賦給陣列 stackArray[++top] = j; } /** * 彈棧 * @return */ public long pop() { //先將棧頂的值彈出,再將棧頂的下標減一 return stackArray[top--]; } /** * 返回棧頂的值 * @return */ public long peek() { return stackArray[top]; } /** * 檢查棧是否為空 * @return */ public boolean isEmpty() { return (top == -1); } /** * 檢查棧是否已經滿了 * @return */ public boolean isFull() { return (top == maxSize - 1); } }
測試類
/** * MyStack類的測試類 */ public class StackApp { public static void main(String[] args) { MyStack ms = new MyStack(10); ms.push(10); ms.push(20); ms.push(30); ms.push(40); ms.push(50); while(!ms.isEmpty()) { System.out.print(ms.pop() + " "); } System.out.println(""); } } /** * 執行結果: * 50 40 30 20 10 */
從上面的執行結果可以看出,輸出的結果很輸入的結果順序相反。
棧的簡單運用例項1:單詞逆序
主要思想:對單詞的輸入和輸出採用棧的方式,就可以實現逆序
程式碼如下:
/**
* 實現對單詞的逆序
*/
public class ReverseWords {
//單詞的輸入
private String input;
//單詞逆序後的輸出
private String output;
//帶參構造實現資料的輸入
public ReverseWords(String in) {
input = in;
}
/**
* 單詞逆序
* @return
*/
public String Reverse() {
//設定棧的大小
int stackSize = input.length();
MyStack ms = new MyStack(stackSize);
//將輸入的每一個單詞的每個字母壓入到棧中
for (int i = 0; i < stackSize; i++) {
char ch = input.charAt(i);
ms.push(ch);
}
output = "";
//將每一個字元彈出棧中
while(!ms.isEmpty()) {
//long型別轉換成char型別需要進行強制轉化
char ch = (char)ms.pop();
output += ch;
}
//返回逆序後的單詞
return output;
}
}
測試類
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 單詞逆序的測試類
*/
public class ReverseWordsApp {
public static void main(String[] args) throws IOException {
String input, output;
while(true) {
System.out.println("請輸入一個單詞:");
System.out.flush();
input = getString();
if (input.equals("")) {
break;
}
ReverseWords rw = new ReverseWords(input);
output= rw.Reverse();
System.out.println("逆序後的單詞是:" + output);
}
}
/**
* 實現使用者輸入的讀取
* @return
* @throws IOException
*/
public static String getString() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
return s;
}
}
/*
* 執行結果
* 請輸入一個單詞:
word
逆序後的單詞是:drow
*/
棧的簡單運用例項2:分隔符的匹配
分隔符匹配類,程式碼如下:
/**
* 分隔符的匹配實現
*/
public class BracketChecker {
private String input;
public BracketChecker(String in) {
input = in;
}
public void check() {
//設定棧的大小
int stackSize = input.length();
MyStack ms = new MyStack(stackSize);
//對輸入的字串進行字元遍歷
for (int i = 0; i < stackSize; i++) {
char ch = input.charAt(i);
switch(ch) {
case '{':
case '[':
case '('://如果是碰到左匹配符,就壓入棧中
ms.push(ch);
break;
case '}':
case ']':
case ')':
if (!ms.isEmpty()) {
//只有棧中有左匹配符才進入進行匹配
//將棧頂的字元彈出
char chx = (char)ms.pop();
if ((ch == '}' && chx != '{') ||
(ch == ']' && chx != '[') ||
(ch == ')' && chx != '(')) {
//如果現在的右匹配符與彈出的左匹配符不相匹配,報錯
System.out.println("括號匹配錯誤:" + ch + " 在 " + i);
}
}else {
//說明棧中沒有任何匹配符,但有右括號需要匹配
System.out.println("缺少左括號,右括號匹配錯誤:" + ch + " 在 " + i);
}
break;
default:
break;
}
}
if(!ms.isEmpty()) {
//如果棧中還有匹配符存在,說明左匹配符未完全被彈出,所以缺少右匹配符
System.out.println("錯誤:缺少右匹配括號");
}
}
}
測試類
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 分隔符匹配的測試類
*/
public class BrackerApp {
public static void main(String[] args) throws IOException {
String input;
while(true) {
System.out.println("請輸入帶有分割符的字串:");
System.out.flush();
input = getString();
if (input.equals("")) {
break;
}
BracketChecker bc = new BracketChecker(input);
bc.check();
}
}
/**
* 實現使用者輸入的讀取
* @return
* @throws IOException
*/
public static String getString() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
return s;
}
}
/*
* 執行結果
* 請輸入帶有分割符的字串:
a{b[c(d)f]ge
錯誤:缺少右匹配括號
*/
佇列