printf("ho_tari\n");

과제 - 원형 큐 구현 본문

대학교 2학년 1학기/데이터구조

과제 - 원형 큐 구현

호타리 2023. 9. 4. 11:38
#include <stdio.h>
#include <stdlib.h>

#define QUEUE_SIZE 5

int menu_queue(int*); 
int input_data(int*);
int addq(int);     
int deleteq(void);    
int end_queue(void);

int queue[QUEUE_SIZE]; 
int front = 0;  
int rear = 0; 

int main(void)
{
	int nSelete = 0, nData = 0, nRetry = 1, rData = 0; 

	while (nRetry)
	{
		menu_queue(&nSelete);

		switch (nSelete)
		{
		case 1:  
			input_data(&nData);
			addq(nData);     
			break;
		case 2:  
			rData = deleteq();   
			break;
		case 3: 
			nRetry = end_queue();
			break;
		}
	}

	return 0;
}

int menu_queue(int* pSelect)
{
	printf("\n===================\n");
	printf("1.push 2.pop 3.exit >> ");
	scanf_s("%d", pSelect);

	return 0;
}

int input_data(int* pData)
{
	printf("[+] push value : ");
	scanf_s("%d", pData); 

	return 0;
}


int addq(int nData)
{
	if (((rear + 1) % QUEUE_SIZE) == front)  
		puts("[-]round queue is full");
	else 
	{
		rear = (rear + 1) % QUEUE_SIZE;  
		queue[rear] = nData;     
	}

	return 0;
}

int deleteq(void)
{
	if (front == rear)  
	{
		puts("[-]round queue is empty");
		return 0;
	}
	else
	{
		front = (front + 1) % QUEUE_SIZE;  
		printf("[+] pop data : %d\n", queue[front]); 
		return queue[front];
	}
}

int end_queue(void)
{
	puts("Terminate...");

	return 0;
}

homework_03_2019270774.docx
0.40MB