본문 바로가기
공부/C, C++

[C] Queue 구현 (non-Circular)

by 김샤랑 2022. 3. 14.

[c] Queue 구현 (non-Circular)


 

-Code

#include<stdio.h>
#define MAX_SIZE 10

void inqueue(int data);
int dequeue();
int queue_full();
int queue_empty();

int queue[MAX_SIZE] = {};
int front = -1;
int rear = -1;

int main() {

	int want = 0; //operation number
	int input = 0; //input data

	while (1) {

		printf("What do you want? 1.inqueue 2.dequeue 3.end :");
		scanf_s("%d", &want);

		switch (want) {

		case 1:
			if (queue_full())
				printf("Now queue is full... you can't inqueue.\n\n");

			else {
				printf("Input inqueue number: ");
				scanf_s("%d", &input);
				inqueue(input);
				printf("Done.\n");
			}
			break;

		case 2:
			if (queue_empty())
				printf("Now queue is empty... you can't dequeue.\n\n");

			else {
				printf("dequeue is %d \n", dequeue());
			}
			break;

		case 3:
			return 0;
		}
	}
}

void inqueue(int data)
{
	if (rear == -1)
		front == 0;

	rear++;
	queue[rear] = data;
}
int dequeue()
{
	front++;
	return queue[front];
}
int queue_full()
{
	if (rear == MAX_SIZE - 1)
		return 1;
	else
		return 0;
}
int queue_empty()
{
	if (front == rear)
		return 1;
	else
		return 0;
}

 

 

댓글