利用數組創建隊列

 

  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. int queue[10]; 
  4. int front=-1; 
  5. int rear=-1; 
  6.  
  7. void addqueue(int number) 
  8.     if(rear-front>=10) 
  9.     { 
  10.         printf("the queue is full"); 
  11.     }else
  12.     rear++; 
  13.     queue[rear]=number; 
  14.     } 
  15.  
  16. int delqueue() 
  17.     int temp; 
  18.     if(front==rear) 
  19.     { 
  20.         printf("the queue is empty"); 
  21.         return -1; 
  22.     }else
  23.         front++; 
  24.         temp=queue[front]; 
  25.         queue[front]=0; 
  26.         return temp; 
  27.     } 
  28.  
  29.  
  30. int main() 
  31.     int select; 
  32.  
  33.     printf("(1)input a data"); 
  34.     printf("(2)output a data"); 
  35.     printf("(3)exit"); 
  36.  
  37.     scanf("%d",&select) 
  38.  
  39.     while(select!=3) 
  40.     { 
  41.         int number; 
  42.         switch(select) 
  43.         { 
  44.             case 1:scanf("%d",&number);addqueue(number);break
  45.             case 2:delqueue();break
  46.         } 
  47.  
  48.         printf("(1)input a data"); 
  49.         printf("(2)output a data"); 
  50.         printf("(3)exit"); 
  51.  
  52.         scanf("%d",&select) 
  53.     } 
  54.  
  55.     return 0; 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章