java使用數組和鏈表實現隊列樣本_java

來源:互聯網
上載者:User

(1)用數組實現的隊列:

複製代碼 代碼如下:
 
//先自己定義一個介面 
public interface NetJavaList { 
  public void add(Student t);    //繼承該介面的類必須實現的方法 
  public Student get(int index);//隊列的加入,取出,隊列的大小 
  public int size(); 


定義一個學生類

複製代碼 代碼如下:

class Student { 
    private String name ;   //私人屬性 名字,學分 
    private int score ; 
    public Student(String name , int score){ 
        this.name = name ; 
        this.score = score ; 
    } 
    public void printInfo(){ 
        System.out.println("姓名"+name + "學分"+score ) ; 
    } 

 實現自訂介面

複製代碼 代碼如下:

public class STList implements NetJavaList{ 
private Student[] str = new Student[0] ; 
    //增加隊列的元素 
    public void add(Student t) { 
        Student[] src = new Student[str.length+1]; 
        for(int i=0;i<str.length;i++){ 
            src[i]=str[i] ; 
        } 
        src[str.length]=t ; 
        str = src ; 
    } 

    //得到隊列中的某個元素 
    public Student get(int index) { 
        Student t = str[index]; 
        return t; 
    } 

    //返回隊列的長度 
    public int size() { 

        return str.length; 
    } 


寫個主函數類實現下隊列

複製代碼 代碼如下:

public class Manager { 
    public static void main(String[] args) { 
        STList sil = new STList() ; 
        for(int i=0;i<5;i++){ 
        Student st = new Student("name"+i,i*10);     
        sil.add(st); 
        } 
       printList(sil) ; 

    } 
//輸出隊列中的所有元素 
  public static void printList(STList t){ 
      for(int i=0;i<t.size();i++){ 
          Student f =t.get(i); 
          f.printInfo(); 
      } 

  } 

 (2)鏈表實現的隊列
  先定義一個節點類;

複製代碼 代碼如下:

public class LinkNode { 
private Object obj ; //節點內的資料對象 
private LinkNode next ; //對下一個節點的引用 
//在建立節點對象的時候就傳入節點的資料對象 
public LinkNode(Object obj){ 
    this.obj = obj ; 

public Object getObj(){ 
    return obj ; 

public void setObj(Object obj){ 
    this.obj = obj ; 


public LinkNode getNext(){ 
    return next ; 

public void setNext(LinkNode next){ 
    this.next =next ; 


 然後寫個隊列的實現方法類

複製代碼 代碼如下:

public class LinkList { 

    public static LinkNode root ;//第一個節點 
    public LinkNode last = null ;//最後的一個節點 
    public static void main(String ara[]){ 
        LinkList df = new LinkList() ; 
        df.add(1); 
        df.add(2); 
        df.add(3); 
        df.printLinkList(root); 
        df.move(root,2) ; 
        df.move(root,2) ; 
        df.printLinkList(root); 

    } 
    /*
     * 插入節點
     */ 
    public void add(Object obj){ 
        //建立一個新的節點 
        LinkNode t = new LinkNode(obj); 
        if(root ==null){ 
            root = t ; 
            last = root ; 
        }else{ 
            last.setNext(t); 
            last = t ; 
        } 

    } 
    /*
     * 輸出操作
     */ 
    public void printLinkList(LinkNode root){ 
        if(null != root){ 
            Object data = root.getObj(); 
            System.out.println(data); 
            LinkNode temp = root.getNext(); 
            printLinkList(temp) ; 
        } 
    } 
    /*
     * 刪除操作
     */ 
    public LinkNode move(LinkNode root,int index){ 
        if(this.getLength()<index || index <0){ 
            throw new RuntimeException("下標越界:"+index + 
                ",size:" +this.getLength()) ; 
        }else{ 
        int count = 1 ;LinkNode sd = root ; 
         while(count!=index-1){ 
             sd = sd.getNext(); 

         } 

         
         sd.setNext(sd.getNext().getNext()); 
        return root ; 
    }} 

   /*
    * 得到鏈表的長度
    */ 
      public int  getLength(){ 
        int count = 0 ; 
        if(root==null){ 
            return count ; 
        } 
        LinkNode node =root.getNext(); 
        while(null != node){ 
            count ++ ; 
            node=node.getNext(); 

        } 
        //System.out.println((count+1)); 
        return count+1 ; 
      } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.