求用链表实现回文数的Java程序
RT判断从键盘输入的一个数字是否是回文数,希望用Java的链表实现。
PS.回文数:正着和倒着读是一样的,比如121,2332等等。
多谢高手帮忙~~
import java.util.Scanner; import /** * Describe class test here. * * * Created: Wed Apr 20 14:29:04 2011 * * @author <a href="mailto:enthusiasmer@* @version 1.0 */ public class test { /** * Creates a new <code>test</code> instance. * */ public test() { } /** * Describe <code>main</code> method here. * * @param args a <code>String</code> value */ public static final void main(final String[] args) { String str = ""; test me = new test(); boolean flag; InputStream is = System.in; Scanner scan = new Scanner(is); while (str.equals("quit")!=true) { System.out.println("please input a number:"); str = scan.next(); flag = me.IsPalindrome(str); System.out.println(flag); } System.out.println("bye"); } /** * Describe <code>IsPalindrome</code> method here. * * @param str a <code>String</code> value * @return a <code>boolean</code> value */ private boolean IsPalindrome(String str) { try { Integer.parseInt(str); } catch(NumberFormatException e) { return false; } int half; String tmpStr1, tmpStr2; half = str.length()/2; for (int i=0; i<half; i++) { tmpStr1 = str.substring(i, i+1); tmpStr2 = str.substring(str.length()-i-1, str.length()-i); if (tmpStr1.equals(tmpStr2)==false) { return false; } } return true; } }
public class main { public static void main(String args[]) throws IOException { int b; byte buffer[] = new byte[100];//字节流 System.out.println("输入一行文本: "); b = System.in.read(buffer); b = b-2; LinkedList list = new LinkedList(); while (list.size() != b) { list.add(buffer[list.size()]); } while (list.size() > 1) { Object a = list.getFirst(); Object c = list.getLast(); if (a.equals(c)) { list.removeFirst(); list.removeLast(); } else { System.out.println("不是回文"); System.exit(0); } } System.out.println("是回文"); } }
package test; import java.awt.*; import java.applet.*; public class Main extends Applet{ Label lab; TextField input; int a1,a2=0,k=0,a; public void init(){ lab=new Label("请输入数据"); input=new TextField(16); add(lab); add(input); } public boolean action(Event e,Object o){ if(e.target==input) a1=Integer.parseInt(input.getText()); k=a1; while(k<0||k>0) { a=k%10; a2=10*a2+a; k=k/10; } if(a1==a2) showStatus("输入的数为"+a1+",该数为回文数"); else showStatus("输入的数为"+a1+",该数不是回文数"); input.setText(""); a2=0; return true; } }以前做的。。用到小程序查看器。
package test; import java.util.*; //这个方法也可用于判断回文字符串 public class Main{ public static String st; public static void type(){ Scanner sc=new Scanner(System.in); st=sc.nextLine(); } public static void main(String[] args){ int len; System.out.println("输入一个数字"); Main.type(); len=st.length(); char a[]=new char[len]; a=st.toCharArray(); for(int t=0;t<(len+1)/2;t++){ if(a[t]!=a[len-t-1]){ System.out.println("不是回文数!"); break; } if(t<(len-1)/2) //用于判断是否检查完所有对应组 continue; System.out.println("是回文数!"); } } }不过怎么用链表我真不知道,,我java就没学过链表