1. Stack
1.1 Classification
Sequential stacks: implementation of sequential linear tables
Chained stacks: one-way linked list storage stack
1.2 Stacks of applications
1) The conversion of the numeral
ImportJava.util.Scanner;ImportJava.util.Stack; Public classtran{ Public Static voidMain (String arg[]) {Scanner y=NewScanner (system.in); System.out.println ("Please enter a decimal number"); intb=Y.nextint (); Tran J=NewTran (); J.erjinzhi (b); J.bajinzhi (b); J.shiliujinzhi (b); } // Convert to Binary voidErjinzhi (inta) {Stack<Integer> s=NewStack<integer>(); String Str=""; while(a>0) {S.push (a%); A=a/2; } while(!S.isempty ()) {STR=str+S.pop (); } System.out.println ("Binary is" +str); } //Convert to eight-step system voidBajinzhi (inta) {Stack<Integer> s=NewStack<integer>(); String Str=""; while(a>0) {S.push (a%8); A=a/8; } while(!S.isempty ()) {STR=str+S.pop (); } System.out.println ("Octal is" +str); } //Convert to hexadecimal voidShiliujinzhi (inta) { intC=0; String Str=""; Stack<Character> s=NewStack<character>(); while(a>0) {C=a%16; Switch(c) { Case(Ten): S.push (' A '); Break; Case(one): S.push (' B '); Break; Case(a): S.push (' C '); Break; Case(a): S.push (' D '); Break; Case(+): S.push (' E '); Break; Case(a): S.push (' F '); Break; default: S.push ((Char) (a%16+48)); } A=a/16; } while(!S.isempty ()) {STR=str+S.pop (); } System.out.println ("Hex is" +str); } }View Code
2) Conversion of expressions
Infix expression: a+b*c; prefix-expression: +A*BC; Suffix expression: acb*+
Data structure and algorithm analysis java--stack and queue 1