c語言資料結構:用標誌位實現迴圈隊列

來源:互聯網
上載者:User

標籤:sem   判斷   static   delete   cas   efi   str   資料結構   操作   

  1 #include<stdio.h>  2 #include<stdlib.h>  3   4 #define MAXSIZE 10//定義隊列長度  5   6 static int flag=0;//定義標誌位  7   8 typedef struct {  9     int *base; 10     int front; 11     int rear; 12 }SqQueue;//建立結構體 13  14 int InitQueue(SqQueue &Q){ 15     Q.base=(int*)malloc(MAXSIZE*sizeof(int)); 16     if(!Q.base) 17         return 0; 18     else{ 19         Q.front=Q.rear=0; 20         return 1; 21     } 22 }//建立空鏈表 23  24  25 int IsEmpety(SqQueue &Q){ 26     if(flag==0&&Q.front==Q.rear){ 27         printf("隊列為空白\n"); 28         return 0; 29     } 30     else  31         return 1; 32  33 }//判斷是否為空白 34  35 int IsFull(SqQueue &Q){ 36     if(flag==1&&Q.front==Q.rear){ 37         printf("隊列已滿\n"); 38         return 0; 39     } 40     else 41         return 1; 42 }//判斷是否已滿 43  44 void AddQueue(SqQueue &Q){ 45     if(IsFull(Q)==1){ 46         int e; 47         printf("請輸入元素:\n"); 48         scanf("%d",&e); 49         Q.base[Q.rear]=e; 50         Q.rear=(Q.rear+1)%MAXSIZE; 51         flag=1; 52     } 53 }//添加元素 54  55 void DeleteQueue(SqQueue &Q){ 56     if(IsEmpety(Q)==1){ 57         int e; 58         e=Q.base[Q.front]; 59         Q.front=(Q.front+1)%MAXSIZE; 60         flag=0; 61         printf("%d\n",e); 62     } 63 }//刪減元素 64  65 void Action(SqQueue &Q){ 66     printf("1.入隊\n"); 67      printf("2.使隊頭元素出隊,並返回它的值\n"); 68      printf("3.退出\n"); 69      int a; 70      scanf("%d",&a); 71       switch(a) 72       { 73       case 1: 74       75           AddQueue(Q); 76           break; 77       case 2: 78            79           DeleteQueue(Q); 80           break; 81       case 3: 82           exit(0); 83       default: 84          printf("輸入不合法,請重新輸入\n"); 85  86       } 87       Action(Q); 88 }//操作選項 89  90 int main(){ 91     SqQueue Q; 92     if(InitQueue(Q)) 93         printf("建立成功。\n"); 94     else{ 95         printf("建立失敗。"); 96         system("pause"); 97         return 0; 98     } 99     100     Action(Q);101     102     system("pause");103     free(Q.base);104     return 0;105 106 107 }//主函數

 

 

 

雖然用標誌位實現了,可是將int型數組換為char型數組時會出現錯誤,在此保留疑問,待解決。

c語言資料結構:用標誌位實現迴圈隊列

聯繫我們

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