队列的应用——回文

  队列的应用很广泛。如操作系统中各种数据缓冲区的先进先出管理,应用系统中各种服务请求的排队管理等。

  以下例子是用一个队列和堆栈实现判断一个字符序列是否是回文。

  【例子】编写判断一个字符序列是否是回文的函数。回文是指一个字符序列以中间字符为基准两边字符完全相同,如字符序列"ABCDEDCBA"就是回文,而字符序列"ABCDEDBAC"就不是回文。设计一个主函数进行测试。

  【算法思想】设字符数组str中存放了要判断的字符串。把字符数组中的字符逐个分别存入一个队列和一个堆栈,然后逐个出队列和退栈并比较出队列的字符是否相等,若全部相等则该字符序列是回文,否则反之。

  【源代码】

Queue.java

 1 package seqqueue;
 2 
 3 public interface Queue {
 4     //把元素obj插入队尾
 5     public void append(Object obj)throws Exception;
 6     //把对头数据元素删除并由函数返回
 7     public Object delete()throws Exception;
 8     //取对头数据元素并返回
 9     public Object getFront()throws Exception;
10     //非空否。
11     public boolean notEmpty();
12 }

 

SeqQueue.java

 1 package seqqueue;
 2 
 3     
 4 public class SeqQueue implements Queue{
 5     static final int defaultSize = 10;
 6     int front;
 7     int rear;
 8     int count;
 9     int maxSize;
10     Object[] data;
11     
12     public SeqQueue(){
13         initiate(defaultSize);
14     }
15     
16     public SeqQueue(int sz){
17         initiate(sz);
18     }
19     private void initiate(int sz) {
20         // TODO Auto-generated method stub
21         maxSize = sz;
22         front=rear = 0;
23         count = 0;
24         data = new Object[sz];    
25     }
26 
27     @Override
28     public void append(Object obj) throws Exception {
29         // TODO Auto-generated method stub
30         if(count > 0 && front == rear)
31             throw new Exception("队列已满!");
32         data[rear]=obj;
33         rear=(rear+1)%maxSize;
34         count++;
35     }
36 
37     @Override
38     public Object delete() throws Exception {
39         // TODO Auto-generated method stub
40         if(count == 0)
41             throw new Exception("队列已空!");
42         Object temp = data[front];
43         front=(front+1)%maxSize;
44         count--;
45         return temp;
46     }
47 
48     @Override
49     public Object getFront() throws Exception {
50         // TODO Auto-generated method stub
51         if(count == 0)
52             throw new Exception("队列已空!");
53         return data[front];
54     }
55 
56     @Override
57     public boolean notEmpty() {
58         // TODO Auto-generated method stub
59         return count !=0 ;
60     }
61 
62 }

 

SeqQueueTest.java

 1 package seqqueue;
 2 
 3 import seqstack.SeqStack;
 4 
 5 public class SeqQueueTest {
 6     public static void huiWei(String str) throws Exception{
 7         int n=str.length();
 8         SeqStack myStack=new SeqStack(n);
 9         SeqQueue myQueue=new SeqQueue(n);
10         
11         for(int i = 0 ; i < n; i++)
12         {
13             myQueue.append(str.substring(i, i+1));
14             myStack.push(str.substring(i, i+1));
15         }
16         
17         while(myQueue.notEmpty() && myStack.notEmpty())
18         {
19             if(!myQueue.delete().equals(myStack.pop()))
20             {
21                 System.out.println(str+"不是回文!");
22                 return;
23             }
24         }
25         System.out.println(str+"是回文!");
26     }
27     
28     public static void main(String args[])
29     {
30         String str1="ABCDEDCBA";
31         String str2="ABCDEDBAC";
32         String str3="ABCDDCBA";
33         try
34         {
35             huiWei(str1);
36             huiWei(str2);
37             huiWei(str3);
38         }catch(Exception e)
39         {
40             System.out.println(e);
41             
42         }
43         
44     }
45 
46 }

  【运行截图】

 

posted on 2018-03-31 11:30  小星_log  阅读(555)  评论(0编辑  收藏  举报