閉關修鍊中 *** Java常用演算法之 -- 隊列結構

來源:互聯網
上載者:User

標籤:als   空間   warning   修鍊   返回   int   warnings   添加   ++   

   

     什麼是隊列結構:

        隊列結構和棧結構很相類似。

        和棧結構一樣是一種具有特殊的運算規則,從資料的邏輯結構看,隊列結構其實

        是一種線性結構。

        從儲存結構來進一步劃分,也分為兩類:

          順序隊列結構:即使用一組地址連續的記憶體單元依次儲存隊列中的資料。 在

                 程式中,可以定義一個指定大小的結構數組作為隊列。

          鏈式隊列結構:即用鏈表形式儲存隊列中各元素的值。

     典型的隊列結構:

        在隊列結構中允許對兩端進行操作,但兩端的操作不同。一端只能刪除--隊頭,一

        端只能插入--隊尾。

     隊列的運算規則:

        是按照先進後出(First In First Out,FIFO)進行處理結點資料的。

     一般隊列的兩個基本操作:

        入隊列:將一個元素添加到隊尾(相當於到隊列最後排隊等候)。

        出隊列:將隊頭的元素取出來,同時刪除該元素,使後一個元素成為對頭。

 

看的再多不如動手來操作一番,,哈哈,理解代碼重要~

  1 import java.util.Scanner;  2   3 /*******************************************************  4  *                                                        *  5  *                         隊列結構                           *  6  *                                                        *  7  *******************************************************/  8   9 class DATA4{ 10     String name; 11     int age; 12 } 13  14 class SQType{ 15     static final int QUEUELEN =15; 16     DATA4[] data = new DATA4[QUEUELEN];            //隊列數組 17     int head;                                    //對頭 18     int tail;                                    //隊尾 19      20     @SuppressWarnings("unused") 21     SQType SQTypeInit(){ 22         SQType q; 23         if((q = new SQType())!= null){            //申請記憶體 24             q.head = 0;                            //設定對頭 25             q.tail = 0;                            //設定對尾 26             return q; 27         } 28         else{ 29             return null;                        //返回空 30         } 31     } 32     //判斷空隊列 33     int SQTypeIsEmpty(SQType q){ 34         int temp = 0; 35         if(q.head == q.tail) 36             temp = 1; 37         return (temp); 38     } 39     //判斷滿隊列 40     int SQTypeIsFull(SQType q){ 41         int temp = 0; 42         if(q.tail == QUEUELEN) 43             temp =1; 44         return (temp); 45     } 46     //清空隊列 47     void SQTypeClear(SQType q){ 48         q.head = 0;                                //設定對頭 49         q.tail = 0;                                //設定對尾 50     } 51     //釋放隊列 52     void SQTypeFree(SQType q){ 53         if(q!=null){ 54             q=null; 55         } 56     } 57     //入隊列 58     int InSQType(SQType q,DATA4 data){ 59         if(q.tail == QUEUELEN){ 60             System.out.println("隊列已滿!操作失敗!"); 61             return (0); 62         } 63         else{ 64             q.data[q.tail++] = data;            //將元素入對列 65             return(1); 66         } 67     } 68     //出隊列 69     DATA4 OutSQType(SQType q){ 70         if(q.head == q.tail){ 71             System.out.println("隊列以空!操作失敗!"); 72             System.exit(0); 73         } 74         else{ 75             return q.data[q.head++]; 76         } 77         return null; 78     } 79     //讀結點資料 80     DATA4 PeekSQType(SQType q){ 81         if(SQTypeIsEmpty(q)==1){ 82             System.out.println("空隊列"); 83             return null; 84         } 85         else{ 86             return q.data[q.head]; 87         } 88     } 89     //計算對列長度 90     int SQTypeLen(SQType q){             91         int temp; 92         temp = q.tail - q.head; 93         return(temp); 94     } 95 } 96  97  98 public class P2_4 { 99 100     @SuppressWarnings("resource")101     public static void main(String[] args) {102         SQType st = new SQType();103         DATA4 data1;104         105         Scanner input = new Scanner(System.in);106         SQType stack = st.SQTypeInit();            //初始化隊列107         System.out.println("入隊列操作:");108         System.out.println("輸入姓名 年齡進行入隊列操作:");109         do{110             DATA4 data = new DATA4();111             data.name = input.next();112             data.age = input.nextInt();113             if(data.name.equals("0")){114                 break;                            //若輸入0,則退出115             }116             else{117                 st.InSQType(stack, data);118             }119         }while(true);120         121         String temp = "1";122         System.out.println("出隊列操作:按任意非0鍵進行出棧操作:");123         temp = input.next();124         while(!temp.equals("0")){125             data1 = st.OutSQType(stack);126             System.out.printf("出隊列的資料是(%s,%d)\n",data1.name,data1.age);127             temp = input.next();128         }129         System.out.println("測試結束!");130         st.SQTypeFree(stack);                    //釋放隊列所佔用的空間131     }132 133 }

 

          

閉關修鍊中 *** Java常用演算法之 -- 隊列結構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.