Untitled

기본적인 큐는 front값이 증가함에 따라 실제 가용한 배열의 크기가 줄어들지만,

원형 큐는 아래와 같은 구조로 rear값이 MAX를 넘어가면 다시 배열의 첫번째 위치로 들어가게 되어

배열을 효율적으로 사용하게 된다.

#include <stdio.h>
#define MAX 100
int front=-1;
int rear=-1;
int queue[MAX];
 
int IsEmpty(void){
    if(front==rear)//front와 rear가 같으면 큐는 비어있는 상태 
        return true;
    else return false;
}
int IsFull(void){
    **int tmp=(rear+1)%MAX; //원형 큐에서 rear+1을 MAX로 나눈 나머지값이
    if(tmp==front)//front와 같으면 큐는 가득차 있는 상태** 
        return true;
    else
        return false;
}
void addq(int value){
    if(IsFull())
        printf("Queue is Full.\\n");
    else{
rear = (rear+1)%MAX;
 queue[rear]=value;
 }
 
}
int deleteq(){
    if(IsEmpty())
        printf("Queue is Empty.\\n");
    else{
        front = (front+1)%MAX;
        return queue[front];
    }
}
 
int main(){
    
    addq(4);
    addq(7);
    addq(12);
    printf("%d\\n",deleteq());
    printf("%d\\n",deleteq());
    printf("%d\\n",deleteq());
deleteq();
    
    return 0;
}

#1966 프린터큐


//큐 내부의 데이터는 순차적으로 접근할 수 없으므로 큐에 데이터를 넣을 때 미리 중요도를 순서대로 정리한다.
///따라서 orderArray를 동적 할당하고, 큐에 데이터를 추가하는 동시에 orderArray배열에 저장한다. 
//입력이 끝나면 orderArray를 버블정렬을 통해 내림차순으로 정리하고 큐 값과 비교하며 연산을 진행한다.

//void sort(int num, int* arr) : 내림차순으로 버블정렬해주는 함수
//문서의 중요도를 파악하여 큐의 데이터를 삭제할지, 뒤에 재배치할지를 내림차순 정리된 배열과 비교하여 결정한다.

#include <stdio.h>
#include <stdlib.h>

#define TRUE	1
#define FALSE	0

typedef struct info {
	int num;
	int name;
} Info;

typedef Info* Data;

typedef struct _node
{
	Data data;
	struct _node * next;
} Node;

typedef struct _lQueue
{
	int size;
	Node * front;
	Node * rear;
} LQueue;

typedef LQueue Queue;

void QueueInit(Queue * pq);
int QIsEmpty(Queue * pq);
void Enqueue(Queue * pq, Data data);
Data Dequeue(Queue * pq);
Data QPeek(Queue * pq);

void QueueInit(Queue * pq)
{
	pq->size = 0;
	pq->front = NULL;
	pq->rear = NULL;
}

int QIsEmpty(Queue * pq)
{
	if (pq->front == NULL)
		return TRUE;
	else
		return FALSE;
}

void Enqueue(Queue * pq, Data data)
{
	Node * newNode = (Node*)malloc(sizeof(Node));
	newNode->next = NULL;
	newNode->data = data;

	if (QIsEmpty(pq))
	{
		pq->front = newNode;
		pq->rear = newNode;
	}
	else
	{
		pq->rear->next = newNode;
		pq->rear = newNode;
	}
	pq->size++;

}
Data Dequeue(Queue * pq)
{
	Node * delNode;
	Data retData;

	if (QIsEmpty(pq))
	{
		printf("Queue Memory Error!");
		exit(-1);
	}

	delNode = pq->front;
	retData = delNode->data;
	pq->front = pq->front->next;

	free(delNode);
	pq->size--;
	return retData;
}

Data QPeek(Queue * pq)
{
	if (QIsEmpty(pq))
	{
		printf("Queue Memory Error!");
		exit(-1);
	}

	return pq->front->data;
}
void sort(int num, int* arr) {    //내림차순 버블정렬
	for (int i = 0; i < num - 1; i++) {
		for (int j = 0; j < num - 1; j++) {
			if (arr[j] < arr[j + 1]) {
				int saveNum = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = saveNum;
			}
		}
	}
}

int main(void)
{
	int testCase;
	scanf("%d", &testCase);
	for (int i = 0; i < testCase; i++) {
		Queue q, q2;
		QueueInit(&q), QueueInit(&q2);
		int fileNum, fileCheck;
		scanf("%d%d", &fileNum, &fileCheck);
		int *orderArray = (int*)malloc(sizeof(int)*fileNum);  //버블정렬을 통해 우선순위를 파악하기위해 할당
		for (int i = 0; i < fileNum; i++) {
			Info *info = (Info*)malloc(sizeof(Info));
			int order;
			scanf("%d", &order);
			orderArray[i] = order;
			info->name = i;
			info->num = order;
			Enqueue(&q, info);
		}
		sort(fileNum, orderArray);

		int countCheck = 0;
		while (q.size > 0) {                            //큐값을 확인하여 우선순위가 가장높은지확인
			if (QPeek(&q)->num != orderArray[countCheck]) {
				Info *info2 = QPeek(&q);
				Dequeue(&q);
				Enqueue(&q, info2);
			}
			else if (QPeek(&q)->num == orderArray[countCheck]) {
				if (QPeek(&q)->name == fileCheck) {
					break;
				}
				else if (q.size > 0) {
					Dequeue(&q);
				}
				countCheck++;
			}
		}
		printf("%d\\n", countCheck + 1);
	}
}
#include<stdio.h>
#define MAX_STACK_SIZE 100
 
int stack[MAX_STACK_SIZE];
int top=-1;
 
int IsEmpty(){
    if(top<0)
        return true;
    else
        return false;
    }
int IsFull(){
    if(top>=MAX_STACK_SIZE-1)
        return true;
    else
        return false;
}
 
void push(int value){
    if(IsFull()==true)
        printf("스택이 가득 찼습니다.");
    else
        stack[++top]=value; 
}
 
int pop(){
    if(IsEmpty()==true)
        printf("스택이 비었습니다.");
    else 
        return stack[top--];
}
 
int main(){
    
    push(3);
    push(5);
    push(12);
    printf("%d ",pop());
    printf("%d ",pop());
    printf("%d ",pop());
 
    return 0;