순환 대기 열의 기본 동작

VC 6.0 에서 쓴 것:
#include
#include
#define MAXSIZE 100
typedef char datatype;
typedef struct
{
    datatype *base;     //        
    int front;      //       
    int rear;       //            
}Queue;

int judge = 0;      //       (0    ,1   )
Queue initQueue()   //    
{
    Queue Q;
    Q.base = (datatype *)malloc(sizeof(datatype) * MAXSIZE);
    if (judge == 1)
    {
        printf("     !
"
); free(Q.base); return Q; } if (!Q.base) { printf(" !
"
); return Q; } judge = 1; Q.front = 0; Q.rear = 0; printf("
"
); return Q; } Queue push_Queue(Queue Q) // { datatype data; if (judge == 0) { printf(" !
"
); return Q; } if ((Q.rear + 1) % MAXSIZE == Q.front) { printf(" , !
"
); return Q; } printf(" :
"
); scanf("%c", &data); getchar(); Q.base[Q.rear] = data; Q.rear = (Q.rear + 1) % MAXSIZE; printf(" %c !
"
, data); return Q; } Queue pop_Queue(Queue Q) // { datatype data; if (judge == 0) { printf(" !
"
); return Q; } if (Q.rear == Q.front) { printf(" , !
"
); return Q; } data = Q.base[Q.front]; Q.front = (Q.front + 1) % MAXSIZE; printf(" %c
"
, data); return Q; } void show_Queue(Queue Q) // ( ) { int i; if (judge == 0) { printf(" !
"
); return; } if (Q.rear == Q.front) { printf("
"
); return ; } i = Q.front; printf(" :
"
); while (i != Q.rear) { printf("%c ", Q.base[i]); i++; } } void length_Queue(Queue Q) { int i, n = 0; if (judge == 0) { printf(" !
"
); return; } i = Q.front; while (i != Q.rear) { i++; n++; } printf(" %d
"
, n); } void destroy_Queue(Queue Q) { if (judge == 0) { printf(" !
"
); return; } free(Q.base); judge = 0; printf(" !
"
); } void caidan() // { printf("
"
); printf("**************************************************
"
); printf("* 1----- *
"
); printf("* 2----- *
"
); printf("* 3----- *
"
); printf("* 4----- *
"
); printf("* 5----- *
"
); printf("* 6----- *
"
); printf("* 0----- *
"
); printf("**************************************************
"
); } int main() { caidan(); while(1) { int ch; Queue Q; printf("
***************************************************
"
); printf(" :"); scanf("%d",&ch); getchar(); switch(ch) { case 1: // { Q = initQueue(); break; } case 2: // { Q = push_Queue(Q); break; } case 3: // { Q = pop_Queue(Q); break; } case 4: // { show_Queue(Q); break; } case 5: // { length_Queue(Q); break; } case 6: // { destroy_Queue(Q); break; } case 0: return 0; } } return 0; }

좋은 웹페이지 즐겨찾기