CHIP KIDD

Queue 자료구조 구현 본문

전기전자/ARM

Queue 자료구조 구현

쑨야미 2021. 5. 6. 12:48

ARM을 이용해

 

Circular Queue 를 구현하는 자료입니다.

위 그림에서 설명하는 단계대로 코드를 구현해보겠습니다. 

중요한 점은 Full 과 Empty 상태를 알아내는 것.

+ Rear와 Front는 항상 붙어다니는 존재이기에 TypeDef Struct 를 통한 구조체로 만드는게 편리합니다.

+ 포인터를 이용해 직접 메모리에 접근하겠습니다. ( * / -> / &)

 

 

Queue.c

/*
 * Queue.c
 *
 *  Created on: May 6, 2021
 *      Author: kccistc
 */

#include "Queue.h"




void QueInit(QUEUE *que)
{
	que->rear = 0;
	que->front = 0;
}

uint8_t isQueFull(QUEUE *que)
{
	return ((que->rear+1)%QUE_SIZE == que->front);
}

uint8_t isQueEmpty(QUEUE *que)
{
	return (que->rear == que->front);
}

void EnQue(QUEUE *que, int data)
{
	if(isQueFull(que)) return;
	que->rear = (que->rear + 1) % QUE_SIZE;
	que->aQueBuff[que->rear] = data;
}

uint8_t DeQue(QUEUE *que)
{
	if(isQueEmpty(que)) {
		return 0;
	}
	else{
		que->front = (que->front +1) % QUE_SIZE;
		return que->aQueBuff[que->front];
	}

}

Main.c

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "../UserCode/Queue.h"
#include <stdio.h>
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart2;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int __io_putchar(int ch)
{
	HAL_UART_Transmit(&huart2, &ch, 1, 100);
	return ch;
}
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
	QUEUE btn1QueBuf;
	QueInit(&btn1QueBuf);
	int count =10;
	for (int i =0; i<14; i++){
		count = count +i;
		EnQue(&btn1QueBuf, count);
		printf("index : %d, Enque : %d\n", i, count);
	}

	for (int i =0; i<15; i++){
		if (isQueEmpty(&btn1QueBuf)){
			printf("index : %d, QueBuff is Empty !\n", i );
		}
		else {
			printf("index : %d, Deque : %d\n", i, DeQue(&btn1QueBuf));
		}
	}
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

Queue.h

/*
 * Queue.h
 *
 *  Created on: May 6, 2021
 *      Author: kccistc
 */

#ifndef USERCODE_QUEUE_H_
#define USERCODE_QUEUE_H_

#include "stm32f4xx_hal.h"

#define QUE_SIZE	10

typedef struct _queue {
	int rear;
	int front;
	int aQueBuff[QUE_SIZE];
}QUEUE;

void QueInit(QUEUE *que);
uint8_t isQueFull(QUEUE *que);
uint8_t isQueEmpty(QUEUE *que);
void EnQue(QUEUE *que, int data);
uint8_t DeQue(QUEUE *que);

#endif /* USERCODE_QUEUE_H_ */

 

결과값