數據結構(java語言描述)棧(隊列)習題

1.編寫一個函數,要求借助一個棧把數組中的數據元素逆置

  代碼實現(順序棧實現)

package exercise;
import  stack.sqstack;
import java.util.Scanner;
public class nizhi {
public  int[]  shunizhi(int[] x)throws Exception{
    if(x!=null){
        sqstack sq=new sqstack(x.length);
        for(int i=0;i<x.length;i++){
            sq.push(x[i]);
        }
        while(!sq.isEmpty()){
            for(int i=0;i<x.length;i++){
                x[i]=(int)sq.pop();
            }
        }    
        return x;
    }else
        return null;
}
public static void main(String[] args) throws Exception{
    Scanner sc=new Scanner(System.in);
    System.out.println("請輸入數組的大小:");
    int n=sc.nextInt();
    int[] ar=new int[n];
    System.out.println("請輸入數組的元素:");
    for(int i=0;i<n;i++){
        ar[i]=sc.nextInt();
    }
    System.out.print("逆置以前的數組爲: ");
    for(int i=0;i<n;i++){
        System.out.print(ar[i]+" ");
    }
    nizhi a=new nizhi();
    a.shunizhi(ar);
    System.out.print(" 逆置操做以後的數組爲");
    for(int i=0;i<n;i++){
        System.out.print(ar[i]+" ");
    }
}
}  java

結果:數組

請輸入數組的大小:
5
請輸入數組的元素:
23
56
47
55
78
逆置以前的數組爲: 23 56 47 55 78  逆置操做以後的數組爲78 55 47 56 23 函數

2.編寫一個函數判斷一個字符序列是否爲一個迴文序列。

所謂的迴文序列就是正讀和反讀都是相同的字符序列,例如:abba和abdba均爲迴文序列。要求只使用棧來實現。ui

代碼:(鏈棧實現)spa

package exercise;
import stack.Linkstack;
import java.util.Scanner;
public class Ishuiwen {
public boolean ishui(String x) throws Exception
    {
        Linkstack sq=new Linkstack();
        int index=0;
        int l=0;
        while(index<x.length()){
            sq.push(x.charAt(index));
            //System.out.println(sq.peek());
            index++;
        }
        for(int i=0;i<x.length();i++){
            if(sq.peek()!=null){
                String t=sq.pop().toString();
                //System.out.println("sq.pop()="+t);
                char q=t.charAt(0);
                //System.out.println("x.charAt(i)="+x.charAt(i));
            if(x.charAt(i)==q)
                ++l;
            }
        }
        //System.out.println("index="+index);
        //System.out.println("l="+l);
    return l==x.length()?true:false;
    }//ishu
public static void main(String[] args)throws Exception{
    Scanner sc=new Scanner(System.in);
    System.out.println("請輸入要判斷的字符串:");
    String s=new String();
    s=sc.nextLine();
    Ishuiwen hui=new Ishuiwen();
    boolean a=hui.ishui(s);
    System.out.println("您輸入的字符串:"+s+" ?迴文:"+a);
}
} 設計

結果:ci

請輸入要判斷的字符串:
ghasrharhahhahrthrsahg
您輸入的字符串:ghasrharhahhahrthrsahg ?迴文:false字符串

3.設計一個將十進制數轉化爲二進制的方法。io

代碼:class

package exercise;
import stack.Linkstack;
import java.util.Scanner;
public class JZzh {
public void  sh2er(int a)throws Exception{
    Linkstack s=new Linkstack();
    while(a/2!=0){
        int t=a%2;//取餘數爲低位作push操做
        s.push(t);
        a=a/2;
    }
    if(a!=0)//最高爲不爲0,則再次執行push操做
        s.push(a);
    String er=new String();
    while(s.peek()!=null)//輸出棧中的元素,逆置
        er=er.concat(s.pop().toString());
    System.out.println("轉換後的二進制整數爲:"+er);
}
public static void main(String[] args)throws Exception{
    JZzh jinzhi=new JZzh();
    Scanner sc=new Scanner(System.in);
    System.out.println("請輸入要轉換的十進制整數:");
    int b=sc.nextInt();
    jinzhi.sh2er(b);
}
}

結果:

請輸入要轉換的十進制整數:
16
轉換後的二進制整數爲:10000

相關文章
相關標籤/搜索