zoukankan      html  css  js  c++  java
  • 数据结构之栈的应用

    一、单词逆序

     1 package data.struct.algorithm;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.IOException;
     5 import java.io.InputStreamReader;
     6 
     7 //以数组为内核实现栈
     8 class Stacky {
     9     private int maxSize;
    10     private char[] stackArray;
    11     private int top;
    12 
    13     public Stacky(int s) {
    14         maxSize = s;
    15         stackArray = new char[maxSize];
    16         top = -1;
    17     }
    18 
    19     public void push(char value) {
    20         stackArray[++top] = value;
    21     }
    22 
    23     public char pop() {
    24         return stackArray[top--];
    25     }
    26 
    27     public char peek() {
    28         return stackArray[top];
    29     }
    30 
    31     public boolean isEmpty() {
    32         return top == -1;
    33     }
    34 }
    35 
    36 // 定义Reverse类实现字符串逆序
    37 class Reverse {
    38     private String input;
    39     private String output;
    40 
    41     public Reverse(String input) {
    42         this.input = input;
    43     }
    44 
    45     public String doRev() {
    46         int size = input.length();
    47         Stacky theStacky = new Stacky(size);
    48         for (int x = 0; x < input.length(); x++) {
    49             char ch = input.charAt(x);
    50             theStacky.push(ch);
    51         }
    52         output = "";
    53         while (!theStacky.isEmpty()) {
    54             output = output + theStacky.pop();
    55         }
    56         return output;
    57 
    58     }
    59 }
    60 
    61 public class StackApp2 {
    62 
    63     /**
    64      * @param args
    65      * @author ysw_go
    66      * @throws IOException
    67      */
    68     // 用栈实现单词逆序
    69     public static void main(String[] args) throws IOException {
    70 
    71         String input, output;
    72         while (true) {
    73             System.out.println("please input a string:");
    74             input = getString();
    75             if (input.equals("")) {
    76                 break;
    77             }
    78             Reverse reverse = new Reverse(input);
    79             output = reverse.doRev();
    80             System.out.println("Reversed:" + output);
    81         }
    82     }
    83 
    84     // 定义方法从键盘获取输入的字符串
    85     public static String getString() throws IOException {
    86         BufferedReader bufr = new BufferedReader(new InputStreamReader(
    87                 System.in));
    88         return bufr.readLine();
    89 
    90     }
    91 }

    二、括号匹配

      1 package data.struct.algorithm;
      2 
      3 import java.io.BufferedReader;
      4 import java.io.IOException;
      5 import java.io.InputStreamReader;
      6 
      7 import javax.swing.DefaultBoundedRangeModel;
      8 
      9 //以数组为内核实现栈
     10 class Stackz {
     11     private int maxSize;
     12     private char[] stackArray;
     13     private int top;
     14 
     15     public Stackz(int s) {
     16         maxSize = s;
     17         stackArray = new char[maxSize];
     18         top = -1;
     19     }
     20 
     21     public void push(char value) {
     22         stackArray[++top] = value;
     23     }
     24 
     25     public char pop() {
     26         return stackArray[top--];
     27     }
     28 
     29     public char peek() {
     30         return stackArray[top];
     31     }
     32 
     33     public boolean isEmpty() {
     34         return top == -1;
     35     }
     36 }
     37 class BracketChecker{
     38     private String input;
     39     public BracketChecker(String s){
     40         input=s;
     41     }
     42     public void check(){
     43         Stackz theStackz=new Stackz(input.length());
     44         for(int x=0;x<input.length();x++){
     45             char ch=input.charAt(x);
     46             switch(ch){
     47             case '{':
     48             case '(':
     49             case '[':theStackz.push(ch);break;
     50             case '}':
     51             case ']':
     52             case ')':
     53                 if(!theStackz.isEmpty()){    
     54                     char chx=theStackz.pop();
     55                     if(ch=='}'&&chx!='{'||ch==']'&&chx!='['||ch==')'&&chx!='('){
     56                         System.out.println("Error:"+ch+"at"+x);
     57                     }
     58                 }
     59                 else {
     60                     
     61                     System.out.println("Error:"+ch+"at"+x);break;
     62                 }
     63             default:
     64                    break;
     65             }
     66             
     67         }
     68         if(!theStackz.isEmpty()){
     69             System.out.println("不匹配");
     70         }
     71     }
     72 }
     73 
     74 
     75 public class CopyOfStackApp3 {
     76 
     77     /**
     78      * @param args
     79      * @author ysw_go
     80      * @throws IOException
     81      */
     82     // 用栈实现括号匹配
     83     public static void main(String[] args) throws IOException {
     84         String input;
     85         while(true){
     86             System.out.println("please Enter a String:");
     87             input=getString();
     88             if(input.equals("")){
     89                 break;
     90             }
     91             BracketChecker brac=new BracketChecker(input);
     92             brac.check();
     93         }
     94     }
     95 
     96     // 定义方法从键盘获取输入的字符串
     97     public static String getString() throws IOException {
     98         BufferedReader bufr = new BufferedReader(new InputStreamReader(
     99                 System.in));
    100         return bufr.readLine();
    101 
    102     }
    103 }
  • 相关阅读:
    磁盘冗余阵列之RAID 5
    磁盘冗余阵列之RAID10
    Linux常见的命令与vi的介绍
    通过挂在系统光盘搭建本地yum
    Windows 2008 系统安装
    在windows主机中,利用XSHELL生成“密钥”进行虚拟机与物理机的传输
    在VMware下进行的使用ssh服务管理远程主机
    Linux中的快捷方式
    Linux中常用命令
    Linux中vi命令的详细总结
  • 原文地址:https://www.cnblogs.com/ysw-go/p/5351669.html
Copyright © 2011-2022 走看看