728x90
반응형
#include <stdio.h>

#define TRUE	1
#define FALSE	0

#define QUEUE_LEN	8

typedef struct _queue
{
	int front;
	int rear;
	int Qarr[QUEUE_LEN];
}Queue;

void Init(Queue* copy)
{
	copy->front = 0;
	copy->rear = 0;
}

int Empty(Queue* copy)
{
	if (copy->front == copy->rear)
	{
		return TRUE;
	}
	else
		return FALSE;
}

int NextIndex(int idx)
{
	if (idx == QUEUE_LEN - 1)
		return 0;
	else
		return idx + 1;
}

int Peek(Queue* copy)
{
	if (Empty(copy))
	{
		printf("Queue Memory Error");
		exit(-1);
	}
	
	return copy->Qarr[NextIndex(copy->front)];
}

void Enqueue(Queue* copy, int data)
{
	if (NextIndex(copy->rear) == copy->front)
	{
		printf("Queue Memory Error");
		exit(-1);
	}

	copy->rear = NextIndex(copy->rear);
	copy->Qarr[copy->rear] = data;
}

int Dequeue(Queue* copy)
{
	if (Empty(copy))
	{
		printf("Queue Memory Error");
		exit(-1);
	}

	copy->front = NextIndex(copy->front);
	return copy->Qarr[copy->front];
}

int main(void)
{
	/* Queue Generationand Initialization */

	Queue q;
	Init(&q);

	/* Input Data in the array */
	Enqueue(&q, 2);
	Enqueue(&q, 4);
	Enqueue(&q, 6);
	Enqueue(&q, 8);
	Enqueue(&q, 10);
	Enqueue(&q, 12);
	Enqueue(&q, 14);

	/* Peek the front data */
	//printf("%d\n", Peek(&q));

	//for (int i = 0; i < QUEUE_LEN; i++)
	//{
	//	printf("%d ", q.Qarr[i]);	// -858993460 2 4 6 8 10 12 14
	//}

	/* OutPut Entire Data */
	while (!Empty(&q))
	{
		printf("%d ", Dequeue(&q));
	}

	return 0;
}
728x90
반응형

'Data Structure & Algorithm' 카테고리의 다른 글

Binary Tree  (0) 2022.10.30
Hambuger Simulator (using queue)  (0) 2022.10.29
Spinning Donuts  (0) 2022.10.28
Calculator (stack, postpix)  (0) 2022.10.28
Stack (Calculator)  (0) 2022.10.27

+ Recent posts