publicclassJosephusTest{publicstaticvoidmain(String[] args){CircleSingleLinkedList list =newCircleSingleLinkedList();
list.add(5);
list.show();System.out.println("==================");
list.out(3,3);}}classCircleSingleLinkedList{privateNode first;publicCircleSingleLinkedList(){
first =null;}publicvoidadd(int count){if(count <1){System.out.println("数字不对");return;}Node temp =null;for(int i =1; i <= count; i++){Node node =newNode(i);if(i ==1){
first = node;
first.setNext(first);
temp = first;}else{
temp.setNext(node);
node.setNext(first);
temp = temp.getNext();}}}publicvoidout(int k,int m){if(k <1|| m <1){System.out.println("数字不对");return;}Node temp = first;while(temp.getNext()!= first){
temp = temp.getNext();}for(int i =0; i < k -1; i++){
first = first.getNext();
temp = temp.getNext();}while(first != temp){for(int i =0; i < m -1; i++){
first = first.getNext();
temp = temp.getNext();}System.out.println(first);
first = first.getNext();
temp.setNext(first);}System.out.println(first);}publicvoidshow(){if(first ==null){System.out.println("环为空");return;}Node temp = first;System.out.println(temp);
temp = temp.getNext();while(temp != first){System.out.println(temp);
temp = temp.getNext();}}}classNode{privateint id;privateNode next;publicNode(){
next =null;}publicNode(int id){this.id = id;this.next =null;}publicintgetId(){return id;}publicvoidsetId(int id){this.id = id;}publicNodegetNext(){return next;}publicvoidsetNext(Node next){this.next = next;}@OverridepublicStringtoString(){return"Node{"+"id="+ id +'}';}}
栈
用数组模拟栈
publicclassArrayStackTest{publicstaticvoidmain(String[] args){try{Stack stack =newStack(3);System.out.println("栈是否为空");System.out.println(stack.isEmpty());System.out.println("栈是否为满");System.out.println(stack.isFull());System.out.println("压入1");
stack.push(1);System.out.println("压入2");
stack.push(2);System.out.println("压入3");
stack.push(3);System.out.println("压入4");
stack.push(4);System.out.println("遍历");
stack.show();System.out.println("弹出");System.out.println(stack.pop());System.out.println("弹出");System.out.println(stack.pop());System.out.println("弹出");System.out.println(stack.pop());System.out.println("弹出");System.out.println(stack.pop());System.out.println("栈是否为空");System.out.println(stack.isEmpty());System.out.println("栈是否为满");System.out.println(stack.isFull());}catch(Exception e){
e.printStackTrace();}}}classStack{privateint maxSize;privateint[] stack;privateint top;publicStack(int maxSize){this.maxSize = maxSize;this.top =-1;this.stack =newint[maxSize];}publicbooleanisFull(){return top == maxSize -1;}publicbooleanisEmpty(){return top ==-1;}publicvoidpush(int value){if(isFull()){System.out.println("栈满了");return;}
top++;
stack[top]= value;}publicintpop(){if(isEmpty()){thrownewRuntimeException("栈空了");}int value = stack[top];
top--;return value;}publicvoidshow(){if(isEmpty()){System.out.println("栈空了");return;}for(int i = top; i >=0; i--){System.out.print(" "+stack[i]+" ");}}}
简易计算器
publicclassCalculatorTest{publicstaticvoidmain(String[] args){String expression ="300*6-4/2+30";char[] chars = expression.toCharArray();Stack numberStack =newStack(10);Stack operatorStack =newStack(10);for(int i =0; i < chars.length; i++){if(operatorStack.isOperator(chars[i])){if(!operatorStack.isEmpty()){while(!operatorStack.isEmpty()&& operatorStack.priority(chars[i])<= operatorStack.priority((char) operatorStack.peek())){int right = numberStack.pop();int left = numberStack.pop();char operator =(char) operatorStack.pop();int result = numberStack.cal(right, left, operator);
numberStack.push(result);}}
operatorStack.push(chars[i]);}else{String number = chars[i]+"";while(i < chars.length -1&&!operatorStack.isOperator(chars[i +1])){
number += chars[i +1];
i++;}
numberStack.push(Integer.parseInt(number));}}while(!operatorStack.isEmpty()){int right = numberStack.pop();int left = numberStack.pop();char operator =(char) operatorStack.pop();int result = numberStack.cal(right, left, operator);
numberStack.push(result);}System.out.println(numberStack.pop());}}classStack{privateint maxSize;privateint[] stack;privateint top;publicStack(int maxSize){this.maxSize = maxSize;this.top =-1;this.stack =newint[maxSize];}publicbooleanisFull(){return top == maxSize -1;}publicbooleanisEmpty(){return top ==-1;}publicvoidpush(int value){if(isFull()){System.out.println("栈满了");return;}
top++;
stack[top]= value;}publicintpop(){if(isEmpty()){thrownewRuntimeException("栈空了");}int value = stack[top];
top--;return value;}publicintpeek(){return stack[top];}publicintpriority(char operator){if(operator =='*'|| operator =='/'){return1;}elseif(operator =='+'|| operator =='-'){return0;}else{return-1;}}publicbooleanisOperator(char val){return val =='*'|| val =='/'|| val =='+'|| val =='-';}publicintcal(int right,int left,char operator){int result =0;switch(operator){case'+':
result = left + right;break;case'-':
result = left - right;break;case'*':
result = left * right;break;case'/':
result = left / right;break;}return result;}publicvoidshow(){if(isEmpty()){System.out.println("栈空了");return;}for(int i = top; i >=0; i--){System.out.print(" "+stack[i]+" ");}}@OverridepublicStringtoString(){return"Stack{"+"stack="+Arrays.toString(stack)+'}';}}
逆波兰表达式计算
publicclassPolandNotation{publicstaticvoidmain(String[] args){String expression ="3 4 + 5 * 6 -";List<String> list =getListString(expression);System.out.println(calculate(list));}publicstaticList<String>getListString(String expression){String[] strings = expression.split(" ");returnnewArrayList<>(Arrays.asList(strings));}publicstaticintcalculate(List<String> list){Stack<String> stack =newStack<>();for(String s : list){if(s.matches("\\d+")){
stack.push(s);}else{int right =Integer.parseInt(stack.pop());int left =Integer.parseInt(stack.pop());int result =0;switch(s){case"+":
result = left + right;break;case"-":
result = left - right;break;case"*":
result = left * right;break;case"/":
result = left / right;break;}
stack.push(result +"");}}returnInteger.parseInt(stack.pop());}}
中缀表达式转后缀表达式
publicclassInfixSuffix{publicstaticvoidmain(String[] args){String expression ="10+((2+3)*4)-5";List<String> list =getInfixList(expression);System.out.println(getSuffixList(list));}publicstaticList<String>getInfixList(String expression){List<String> list =newArrayList<>();String number ="";for(int i =0; i < expression.length(); i++){char c = expression.charAt(i);if(c <48|| c >57){
list.add(c +"");}else{
number += c;while(i < expression.length()-1){
c = expression.charAt(i +1);if(c <=57&& c >=48){
number += c;
i++;}else{break;}}
list.add(number);
number ="";}}return list;}publicstaticList<String>getSuffixList(List<String> infixList){List<String> suffixList =newArrayList<>();Stack<String> stack =newStack<>();for(String s : infixList){if(s.matches("\\d+")){
suffixList.add(s);}elseif(s.equals("(")){
stack.push(s);}elseif(s.equals(")")){while(!stack.peek().equals("(")){
suffixList.add(stack.pop());}
stack.pop();}else{while(!stack.empty()&&priority(s)<=priority(stack.peek())){
suffixList.add(stack.pop());}
stack.push(s);}}while(!stack.empty()){
suffixList.add(stack.pop());}return suffixList;}publicstaticintpriority(String operator){if(operator.equals("*")|| operator.equals("/")){return1;}elseif(operator.equals("+")|| operator.equals("-")){return0;}else{return-1;}}}
递归
迷宫问题
publicclassLabyrinthTest{publicstaticvoidmain(String[] args){//0表示没有走过,1表示墙,2表示可以通过,3表示已经搜索过,但走不通int[][] ints =newint[8][8];for(int i =0; i <8; i++){
ints[0][i]=1;
ints[7][i]=1;}for(int i =0; i <8; i++){
ints[i][0]=1;
ints[i][7]=1;}
ints[3][1]=1;
ints[3][2]=1;
ints[3][4]=1;
ints[6][5]=1;
ints[5][5]=1;
ints[4][5]=1;for(int i =0; i <8; i++){for(int j =0; j <8; j++){System.out.print(" "+ints[i][j]+" ");}System.out.println();}getWay(ints,1,1);System.out.println();for(int i =0; i <8; i++){for(int j =0; j <8; j++){System.out.print(" "+ints[i][j]+" ");}System.out.println();}}//从(1,1)出发,走到(6,6),以下,右,上,左的次序搜索publicstaticbooleangetWay(int[][] map,int i,int j){if(map[6][6]==2){returntrue;}else{if(map[i][j]==0){
map[i][j]=2;if(getWay(map, i +1, j)){returntrue;}elseif(getWay(map, i, j +1)){returntrue;}elseif(getWay(map, i -1, j)){returntrue;}elseif(getWay(map, i, j -1)){returntrue;}else{
map[i][j]=3;returnfalse;}}else{returnfalse;}}}}
八皇后问题
publicclassQueue8{privatestaticint max =8;privatestaticint[] arr =newint[max];publicstaticvoidmain(String[] args){check(0);}publicstaticbooleanjudge(int n){for(int i =0; i < n; i++){//arr[i] == arr[n]:判断是否在同一列//Math.abs(n - i) == Math.abs(arr[n] - arr[i]):判断是否在同一斜线if(arr[i]== arr[n]||Math.abs(n - i)==Math.abs(arr[n]- arr[i])){returnfalse;}}returntrue;}publicstaticvoidcheck(int n){if(n == max){print();return;}for(int i =0; i < max; i++){
arr[n]= i;if(judge(n)){check(n +1);}}}publicstaticvoidprint(){for(int i : arr){System.out.print(" "+i+" ");}System.out.println();}}