데이터 구조 - 체인 큐 (linked queue)

11300 단어
/* linkedQueue.c */
/*     */

#include 
#include 
#include 

/*         */
typedef struct node {
    int data;            /*        */
    struct node *next;  /*            */
} Node;
/* front     ,rear      */
/* front->next          */
typedef struct {
    Node *front, *rear;    
} Queue;

void interface(void);
/*         */
Queue *initializeQueue();
bool isEmptyQueue(Queue *q);
void inQueue(Queue *q, int number);
int outQueue(Queue *q);

int main(){
    Queue *q = initializeQueue();
    int flag, number;

    interface();
    for(;;){
        printf("Command: ");
        scanf("%d", &flag);
        switch(flag){
            case 0: puts("Bye!"); return 0; break;
            case 1:
                printf("Enter a number: ");
                scanf("%d", &number);
                inQueue(q, number);
                break;
            case 2:
                if(isEmptyQueue(q))
                    printf("Queue is empty!
"); else printf("value: %d
", outQueue(q)); break; } } return 0; } void interface(void){ puts("+************************+"); puts("+ 0, quit +"); puts("+ 1, in +"); puts("+ 2, out +"); puts("+************************+"); } /* */ /* */ Queue *initializeQueue(){ /* Queue Node ,front rear */ Queue *q = (Queue*)malloc(sizeof(Queue)); Node *p = (Node*)malloc(sizeof(Node)); p->next = NULL; q->front = p; q->rear = p; return q; } /* */ bool isEmptyQueue(Queue *q){ if(q->front==q->rear) return true; else return false; } /* */ void inQueue(Queue *q, int number){ /* , number ,rear */ Node *p = (Node*)malloc(sizeof(Node)); p->data = number; p->next = NULL; q->rear->next = p; /* next */ q->rear = p; /* rear */ } /* */ int outQueue(Queue *q){ Node *p = q->front->next; /* p */ q->front->next = p->next; /* q->front->next */ int r = p->data; /* */ free(p); /* */ /* , , */ if(q->front->next==NULL) q->rear = q->front; return r; }

좋은 웹페이지 즐겨찾기