随笔 - 0  文章 - 3  trackbacks - 0
<2025年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

留言簿

文章档案(50)

搜索

  •  

最新评论


文件一:ListNode.java


java 代码




  1. public class ListNode {   

  2.            

  3.         char element;        //当前字符   

  4.         int count;                //字符个数   

  5.   

  6.         ListNode next;   

  7.            

  8.         ListNode(char c,ListNode n)   

  9.         {   

  10.                 element = c;   

  11.                 count=1;   

  12.                 next=n;   

  13.         }   

  14.            

  15.         ListNode(char c)   

  16.         {   

  17.                 this(c,null);   

  18.         }   

  19. }   



LinkedList.java


java 代码




  1. public class LinkedList {   

  2.   

  3.         ListNode header;   

  4.            

  5.         LinkedList()   

  6.         {   

  7.                 this(null);   

  8.         }   

  9.            

  10.         LinkedList(ListNode n)   

  11.         {   

  12.                 header=new ListNode(' ',n);   

  13.         }   

  14.            

  15.         boolean isEmpty()   

  16.         {   

  17.                 return header.next==null;   

  18.         }   

  19.            

  20.         void makeEmpty()   

  21.         {   

  22.                 header=null;   

  23.         }   

  24.            

  25.         ListNode find(char c)   

  26.         {   

  27.                 if(isEmpty())   

  28.                         return null;   

  29.                    

  30.                 ListNode node = header.next;   

  31.                 while(node!=null)   

  32.                 {   

  33.                         if(node.element==c)   

  34.                                 return node;   

  35.                            

  36.                         node=node.next;   

  37.                 }   

  38.                 return null;   

  39.         }   

  40.            

  41.         void insert(char c,ListNode node)   

  42.         {   

  43.                 node.next=new ListNode(c,node.next);   

  44.         }   

  45.            

  46.         void remove(char c)   

  47.         {   

  48.                 if(isEmpty())   

  49.                         return;   

  50.                    

  51.                 ListNode node = header.next;   

  52.                 while(node!=null)   

  53.                 {   

  54.                         if(node.next.element==c)   

  55.                         {   

  56.                                 node.next=node.next.next;   

  57.                                 return;   

  58.                         }   

  59.                         node = node.next;   

  60.                 }   

  61.         }   

  62. }   



 


文件三:Test.java


java 代码




  1. public class Test {   

  2.            

  3.         public static void showCharCount(char s[])   

  4.         {   

  5.                 System.out.println("原字符串是:"+new String(s));   

  6.                 LinkedList list = new LinkedList();   

  7.                 ListNode node; 

  8.                 for(int i=0; i s.length; i++)

  9.                 {   

  10.                         if((node=list.find(s[i]))==null)   

  11.                                 list.insert(s[i],list.header);   

  12.                         else  

  13.                                 node.count++;                                   

  14.                 }   

  15.                    

  16.                 /*  

  17.                  * 打印链表  

  18.                  */  

  19.                 node = list.header.next;   

  20.                 while(node!=null&&!list.isEmpty())   

  21.                 {   

  22.                         System.out.println("字符:  '"  

  23.                                         +node.element+"'    出现:  "  

  24.                                         +node.count+"  次");   

  25.                         node=node.next;   

  26.                 }   

  27.         }   

  28.            

  29.         public static void showCharCount(String s)   

  30.         {   

  31.                 showCharCount(s.toCharArray());   

  32.         }   

  33.   

  34.         public static void main(String[] args) {   

  35.   

  36.                 showCharCount("abcd ssd wool");   

  37.   

  38.         }   

  39. }   



最后修饰下,把main方法写漂亮点:


java 代码




  1. public static void main(String[] args) throws IOException{   

  2.   

  3.                 String s;   

  4.                 while(true)   

  5.                 {   

  6.                         System.out.print("请输入待统计字符串:  ");   

  7.                         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   

  8.                         s = br.readLine();   

  9.                            

  10.                         if(s.length()>=40)   

  11.                         {   

  12.                                 System.out.println("请输入40个字符以内.");   

  13.                                 System.out.println("===============================\n");   

  14.                                 continue;   

  15.                         }   

  16.                         else  

  17.                                 break;                           

  18.                 }   

  19.                 showCharCount(s);   

  20.   

  21.         }   



效果如下:



请输入待统计字符串:  Woden!Never give up!!!I'll do my best!!!

请输入40个字符以内.

===============================



请输入待统计字符串:  Woden!Never give up!!!

原字符串是:Woden!Never give up!!!

字符:  'p'    出现:  1  次

字符:  'u'    出现:  1  次

字符:  'i'    出现:  1  次

字符:  'g'    出现:  1  次

字符:  ' '    出现:  2  次

字符:  'r'    出现:  1  次

字符:  'v'    出现:  2  次

字符:  'N'    出现:  1  次

字符:  '!'    出现:  4  次

字符:  'n'    出现:  1  次

字符:  'e'    出现:  4  次

字符:  'd'    出现:  1  次

字符:  'o'    出现:  1  次

字符:  'W'    出现:  1  次





评论也很精彩,请点击查看精彩评论。欢迎您也添加评论。查看详细 >>





JavaEye推荐
北京:优秀公司NHNChina招聘:WEB开发,系统管理,JAVA开发, DBA
广州:急招 JAVA开发经理/系统架构师(10-15K/月)也招聘java程序员
与Hibernate之父面对面-4月19日 Gavin King上海交流研讨会
高薪工作机会 美国法国上海 15-20k/月 J2EE SA



文章来源: http://xiaozhe.javaeye.com/blog/59045
posted on 2007-03-11 09:46 xiaozhe 阅读(604) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: