[데이터 구조] 순환 대기 열의 일부 함수

10880 단어
 1 #include 
 2 #include 
 3 
 4 #define OK 1
 5 #define ERROR 0
 6 #define MAXQSIZE 10
 7 typedef int Status;   
 8 typedef int QElemType; //         
 9 
10 typedef struct
11 {
12     QElemType *base;
13     int front;
14     int rear;
15 }SqQueue;  //        
16 
17 Status InitQueue(SqQueue & Q) ;//
18 Status GetHead(SqQueue Q,QElemType &e) ;//            e 
19 Status EnQueue(SqQueue &Q,QElemType e);//    ,   e  
20 Status DeQueue(SqQueue &Q,QElemType &e);//    ,   e  
21 int QueueLength(SqQueue Q);//      
22 
23 int main()
24 {
25     SqQueue Q;
26     int m;
27     QElemType e;
28     InitQueue(Q);
29     scanf("%d",&m);
30     while(m!=0) //m=0      
31     {
32         if(m==1) //m=1    
33         {
34             scanf("%d",&e);
35             EnQueue(Q,e);
36         }
37         else if(m==2)//m=2    
38         {
39             DeQueue(Q,e);
40         }
41         scanf("%d",&m);
42     }
43     printf("length=%d
",QueueLength(Q)); 44 if (GetHead(Q,e)==OK) 45 printf("first=%d",e); 46 else 47 printf("first=NULL"); 48 return 0; 49 } 50 51 Status InitQueue(SqQueue & Q) // 52 { 53 Q.base=(QElemType *)malloc(sizeof(QElemType)*MAXQSIZE); 54 if(!Q.base) 55 return ERROR; 56 Q.front=Q.rear=0; 57 return OK; 58 } 59 60 Status GetHead(SqQueue Q,QElemType &e) // e 61 { 62 if(Q.rear==Q.front) return ERROR; 63 e=Q.base[Q.front]; 64 return OK; 65 } 66 67 Status EnQueue(SqQueue &Q,QElemType e)// , e 68 { 69 if((Q.rear+1)%MAXQSIZE==Q.front) return ERROR; 70 Q.base[Q.rear]=e; 71 Q.rear=(Q.rear+1)%MAXQSIZE; 72 return OK; 73 } 74 75 Status DeQueue(SqQueue &Q,QElemType &e)// 76 { 77 if(Q.rear==Q.front) return ERROR; 78 e=Q.base[Q.front]; 79 Q.front=(Q.front+1)%MAXQSIZE; 80 return OK; 81 } 82 83 int QueueLength(SqQueue Q)// 84 { 85 return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE; 86 }

좋은 웹페이지 즐겨찾기