ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 프린터_queue
    C연습 2021. 1. 17. 11:59
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define MAX_QUEUE_SIZE 10000
    
    typedef int element;
    typedef struct {
    	element data[MAX_QUEUE_SIZE];
    	int front, rear;
    }queuetype;
    
    void init_queue(queuetype *q) {
    	q->front = q->rear = 0;
    }
    
    int full(queuetype *q) {
    	return ((q->rear + 1) % MAX_QUEUE_SIZE == q->front);
    }
    
    int empty(queuetype *q) {
    	if (q->front == q->rear)
    		return 1;
    	else
    		return 0;
    }
    
    void push(queuetype *q, element item) {
    	if (full(q)) return;
    	q->rear = (q->rear + 1) % MAX_QUEUE_SIZE;
    	q->data[q->rear] = item;
    }
    
    int pop(queuetype *q) {
    	if (empty(q)) 
    	{
    		printf("-1\n");
    		return 0;
    	}
    	printf("%d\n", q->data[q->front + 1]);
    	q->front = (q->front + 1) % MAX_QUEUE_SIZE;
    }
    
    void size(queuetype *q) {
    	printf("%d\n", (q->rear - q->front) % MAX_QUEUE_SIZE);
    }
    
    int front(queuetype *q) {
    	if (empty(q)) {
    		printf("-1\n");
    		return 0;
    	}
    	printf("%d\n", q->data[q->front + 1]);
    }
    
    int back(queuetype *q) {
    	if (empty(q)) {
    		printf("-1\n");
    		return 0;
    	}
    	printf("%d\n", q->data[q->rear]);
    }
    
    int main()
    {
    	queuetype queue;
    	char str[10];
    	int N = 0;
    	init_queue(&queue);
    
    	scanf_s("%d ", &N);
    
    	for (int i = 0; i < N; i++)
    	{
    		scanf_s("%s", str, 10);
    
    		if (strcmp(str,"push") == 0) 
    		{
    			element num = 0;
    			scanf_s("%d", &num);
    			push(&queue, num);
    		}
    		else if (strcmp(str, "pop") == 0) {
    			pop(&queue);
    		}
    		else if (strcmp(str, "size") == 0) {
    			size(&queue);
    		}
    		else if (strcmp(str, "empty") == 0)	{
    			if (empty(&queue) == 1)
    				printf("1\n");
    			else
    				printf("0\n");
    		}
    		else if (strcmp(str, "front") == 0) {
    			front(&queue);
    		}
    		else if (strcmp(str, "back") == 0) {
    			back(&queue);
    		}
    	}
    	return 0;
    }

    'C연습' 카테고리의 다른 글

    344-2  (0) 2021.01.17
    98-4  (0) 2021.01.17
    프린터_우선순위_queue  (0) 2021.01.17
    queue  (0) 2021.01.17
    이진 검색 트리  (0) 2021.01.17

    댓글

Designed by Tistory.