반응형
Queue List 구조 예제도 정리해서 Update 해놓도록 하자!
Queue List 예제 1
/*----------------Include--------------------*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*----------------Define--------------------*/
#define TSIZE 100
#define MAX_QSIZE 4
#define TRUE 1
#define FALSE 0
/*----------------Typedef--------------------*/
typedef unsigned int uint32_t;
typedef int int32_t;
typedef unsigned char uint8_t;
typedef struct element
{
char name[TSIZE];
}Element;
typedef Element Item;
typedef struct node
{
Item item;
struct node* next;
}Node;
typedef struct queue
{
Node* front;
Node* rear;
int n_items;
}Queue;
/*----------------Variable--------------------*/
/*----------------Function--------------------*/
static void CopyToNode(Item item, Node* pNode)
{
pNode->item = item;
}
static void CopyToItem(Node* pNode, Item* pItem)
{
*pItem = pNode->item;
}
void InitializeQueue(Queue* pQueue)
{
pQueue->front = NULL;
pQueue->rear = NULL;
pQueue->n_items = 0;
}
uint8_t QueueIsFull(Queue* pQueue)
{
if(pQueue->n_items == MAX_QSIZE)
{
return TRUE;
}
else
{
return FALSE;
}
}
uint8_t QueueIsEmpty(Queue* pQueue)
{
if(pQueue->n_items == 0)
{
return TRUE;
}
else
{
return FALSE;
}
}
int QueueItemCount(const Queue* pQueue)
{
return pQueue->n_items;
}
uint8_t EnQueue(Queue* pQueue, Element item)
{
Node* pnew = NULL;
if(QueueIsFull(pQueue) == TRUE)
{
return FALSE;
}
pnew = (Node*)malloc(sizeof(Node));
if(pnew == NULL)
{
printf("Malloc() failed.\n");
return FALSE;
}
CopyToNode(item,pnew);
pnew->next = NULL;
if(QueueIsEmpty(pQueue))
{
pQueue->front = pnew;
}
else
{
pQueue->rear->next = pnew;
}
pQueue->rear = pnew;
pQueue->n_items++;
return TRUE;
}
uint8_t DeQueue(Queue* pQueue, Element* pItem)
{
Node* pNode = NULL;
if(QueueIsEmpty(pQueue)==TRUE)
{
return FALSE;
}
CopyToItem(pQueue->front, pItem);
pNode = pQueue->front;
pQueue->front = pQueue->front->next;
free(pNode);
pQueue->n_items--;
if(pQueue->n_items == 0)
{
pQueue->rear = NULL;
}
return TRUE;
}
void print_Item(Element Item)
{
printf("%s ", Item.name);
}
void print_queue(Queue* pQueue)
{
Node* pNode;
if(QueueIsEmpty(pQueue) == TRUE)
{
printf("Queue Empty!\n");
}
else
{
printf("front: %p, rear: %p, n_items: %d\n",pQueue->front, pQueue->rear, pQueue->n_items);
pNode = pQueue->front;
while(pNode != NULL)
{
print_Item(pNode->item);
pNode = pNode->next;
}
printf("\n");
}
}
Element get_element(char* name)
{
Element temp;
strcpy(temp.name,name);
return temp;
}
int main(void)
{
Queue my_queue;
Element temp;
InitializeQueue(&my_queue);
(void)EnQueue(&my_queue, get_element("Tom"));
print_queue(&my_queue);
(void)EnQueue(&my_queue, get_element("Jack"));
print_queue(&my_queue);
(void)EnQueue(&my_queue, get_element("Kely"));
print_queue(&my_queue);
(void)EnQueue(&my_queue, get_element("Susan"));
print_queue(&my_queue);
(void)EnQueue(&my_queue, get_element("Lion"));
print_queue(&my_queue);
(void)EnQueue(&my_queue, get_element("Mouse"));
print_queue(&my_queue);
DeQueue(&my_queue, &temp);
printf("DeQueue Name : %s\n",temp.name);
print_queue(&my_queue);
(void)EnQueue(&my_queue, get_element("Lion"));
print_queue(&my_queue);
return 0;
}
실행 결과는 아래와 같다.
Queue List 예제 2
/*--------------------------Include------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*--------------------------Define------------------------------*/
#define TRUE 1
#define FALSE 0
#define TSIZE 50
#define MAX_QSIZE 4
/*--------------------------Typedef------------------------------*/
typedef unsigned int uint32_t;
typedef unsigned char uint8_t;
typedef int int32_t;
typedef struct element
{
char name[TSIZE];
}Element;
typedef Element Item;
typedef struct node
{
Item item;
struct node* next;
}Node;
typedef struct queue
{
Node* front;
Node* rear;
int n_items;
}Queue;
/*--------------------------Variable------------------------------*/
/*--------------------------Function------------------------------*/
void InitializeQueue(Queue* pQueue)
{
pQueue->front = NULL;
pQueue->rear = NULL;
pQueue->n_items = 0;
}
uint8_t QueueIsFull(Queue* pQueue)
{
if(pQueue->n_items == MAX_QSIZE)
{
//printf("Queue is Full!\n");
return TRUE;
}
else
{
return FALSE;
}
}
uint8_t QueueIsEmpty(Queue* pQueue)
{
if(pQueue->n_items == 0)
{
//printf("Queue is Empty!\n");
return TRUE;
}
else
{
return FALSE;
}
}
Item get_item(char* name)
{
Item new_item;
strcpy(new_item.name, name);
return new_item;
}
uint8_t EnQueue(Queue* pQueue, Item item)
{
Node* new_node = NULL;
if(QueueIsFull(pQueue)== TRUE)
{
return FALSE;
}
else
{
new_node = (Node*)malloc(sizeof(Node));
new_node->item = item;
new_node->next = NULL;
if(QueueIsEmpty(pQueue) == TRUE)
{
pQueue->front = new_node;
}
else
{
pQueue->rear->next = new_node;
}
pQueue->rear = new_node;
pQueue->n_items++;
return TRUE;
}
}
uint8_t DeQueue(Queue* pQueue, Item* pItem)
{
Node* pNode = NULL;
if(QueueIsEmpty(pQueue)==TRUE)
{
return FALSE;
}
else
{
*pItem = pQueue->front->item;
pNode = pQueue->front;
pQueue->front = pQueue->front->next;
free(pNode);
pQueue->n_items--;
if(pQueue->n_items == 0)
{
pQueue->rear = NULL;
}
return TRUE;
}
}
void print_queue(Queue* pQueue)
{
Node* pNode = NULL;
pNode = pQueue->front;
printf("front = %p, rear = %p, n_itmes = %d\n",pQueue->front, pQueue->rear, pQueue->n_items);
while(pNode != NULL)
{
printf("%s ",pNode->item.name);
pNode = pNode->next;
}
printf("\n");
}
int main(void)
{
Queue my_queue;
Item temp;
InitializeQueue(&my_queue);
(void)EnQueue(&my_queue, get_item("Apple"));
print_queue(&my_queue);
(void)EnQueue(&my_queue, get_item("Orange"));
print_queue(&my_queue);
(void)EnQueue(&my_queue, get_item("Kewi"));
print_queue(&my_queue);
(void)EnQueue(&my_queue, get_item("Banana"));
print_queue(&my_queue);
(void)EnQueue(&my_queue, get_item("WaterMelon"));
print_queue(&my_queue);
(void)DeQueue(&my_queue, &temp);
printf("DeQueue : name => %s\n",temp.name);
print_queue(&my_queue);
(void)EnQueue(&my_queue, get_item("WaterMelon"));
print_queue(&my_queue);
return 0;
}
실행결과는 아래와 같다.
반응형
'Computer Language > Data Structure' 카테고리의 다른 글
알고리즘 성능 분석 방법 정리 (순차탐색알고리즘, 이진탐색알고리즘 비교) (0) | 2022.07.01 |
---|---|
[자료구조] 04. Queue 배열 구조 예제 (C언어) (0) | 2021.07.10 |
[자료구조] 03. Stack Flood Fill 예제 (C언어) (0) | 2021.07.10 |
[자료구조] 02. Stack 구조 Basic 예제 (C언어) (0) | 2021.07.09 |
[자료구조] 01. List 구조 Baisc 예제 (C언어) (0) | 2021.07.09 |
댓글