μλ£ κ΅¬μ‘° μ€μμ λ°μ΄ν°μ λ§ν¬λ‘ ꡬμ±λ μ°κ²° 리μ€νΈ(Linked List)λΌλ κ²μ΄ μλ€. μ΄ μ°κ²°λ¦¬μ€νΈλ₯Ό μ΄λ»κ² νμ©νμ¬ λ§λλλμ λ°λΌμ μ¬λ¬ μλ£ κ΅¬μ‘°λ₯Ό λ§λ€ μ μλ€. μ΄λ²μ κ·Έ μ€μμ Queueμ λν΄μ μκΈ°νκ² λ€.
true(1)
μ 리ν΄QUEUE_MAX_SIZE
ν¬κΈ°μ λ°°μ΄μ κ°μ λ£λ ννμ νμ΄λ€.push
νλ©΄ rear
κ° λ€λ‘ λ°λ¦¬κ³ pop
νλ©΄ front
κ° λ€λ‘ λ°λ¦°λ€.rear
μ front
κ° λ€λ‘ λ°λ¦¬κΈ° λλ¬Έμ μ μ μ¬μ©ν μ μλ νμ ν¬κΈ°κ° μμμ§λ€.front
κ° QUEUE_MAX_SIZE
λ₯Ό λκΈ°λ©΄ ν μ€λ²νλ‘μ°(Queue Overflow)κ° μΌμ΄λλ€.# define QUEUE_MAX 10
static size_t front = 0;
static size_t rear = 0;
int pop(int queue[])
{
queue[front] = 0;
front++;
}
void push(int queue[], int item)
{
queue[rear] = item;
rear++;
}
int seek(int queue[])
{
return (queue[front]);
}
int is_empty(int queue[])
{
return (front == rear);
}
QUEUE_MAX_SIZE
ν¬κΈ°μ λ°°μ΄μ κ°μ λ£λ ννμ νμ΄λ€.push
μ pop
μ μν΄μ rear
μ front
κ° λ€λ‘ λ°λ¦°λ€.rear
λ front
κ° QUEUE_MAX_SIZE
κ°μΌ λ, push
λ pop
μ νλ©΄ 0μ΄ λΌμ μννλ€.QUEUE_MAX_SIZE
λ§νΌμ μμ΄ν
μ κ°μ§ μ μλ€.rear
μ μμΉλ λ§μ§λ§ μμμ λ€ μΈλ±μ€μ΄λ€.# define QUEUE_MAX 10
static size_t front = 0;
static size_t rear = 0;
int pop(int queue[])
{
int val;
if (is_empty(queue))
return (0);
val = queue[front];
front = (front + 1) % QUEUE_MAX;
return (val);
}
void push(int queue[], int item)
{
if (is_full(queue))
return ;
queue[rear] = item;
rear = (rear + 1) % QUEUE_MAX;
}
int seek(int queue[])
{
return (queue[front]);
}
int is_empty(int queue[])
{
return (front == rear);
}
int is_full(int queue[])
{
if (front <= rear)
return (front == 0 && rear == QUEUE_MAX);
return (front == rear + 1);
}
typedef struct s_linked_queue
{
int data;
struct s_linked_queue *next;
} t_linked_queue;
int pop(t_linked_queue *queue)
{
t_linked_queue *node;
int ret_val;
if (!queue || !queue->next)
return ;
node = queue->next;
ret_val = node->data;
queue->next = node->next;
free(node);
return (ret_val);
}
void push(t_linked_queue *queue, int item)
{
t_linked_queue *node;
t_linked_queue *new;
if (!queue)
return ;
if ((new = (t_linked_queue *)malloc(sizeof(t_linked_queue))) == 0)
return ;
node = queue;
while (node->next)
node = node->next;
new->data = item;
new->next = NULL;
node->next = new;
}
int peek(t_linked_queue *queue)
{
return (queue->next->data);
}
int is_empty(t_linked_queue *queue)
{
if (!queue)
return (1);
return (!queue->next);
}
front
, rear
μμ push
, pop
λͺ¨λ κ°λ₯νλ€.typedef struct s_node
{
int data;
struct s_node *next;
struct s_node *prev;
} t_node;
typedef struct s_dequeue
{
t_node *head;
t_node *tail;
} t_dequeue;
t_dequeue *init_dequeue(void)
{
t_dequeue *dequeue;
if ((dequeue = (t_dequeue *)malloc(sizeof(t_dequeue))) == 0)
return (NULL);
dequeue->head = NULL;
dequeue->tail = NULL;
return (dequeue);
}
int pop_back(t_dequeue *dequeue)
{
t_node *node;
t_node *tmp;
int ret_val;
if (!dequeue)
return (0);
node = dequeue->tail;
ret_val = node->data;
if (dequeue_size(dequeue) == 1)
{
free(node);
dequeue->head = NULL;
dequeue->tail = NULL;
}
else
{
tmp = node->prev;
node->prev->next = NULL;
free(node);
dequeue->tail = tmp;
}
return (ret_val);
}
void push_back(t_dequeue *dequeue, int item)
{
t_node *new_node;
if (!dequeue)
return ;
if ((new_node = (t_node *)malloc(sizeof(t_node))) == 0)
return ;
new_node->data = item;
new_node->prev = dequeue->tail;
new_node->next = NULL;
if (is_empty(dequeue))
dequeue->head = new_node;
else
dequeue->tail->next = new_node;
dequeue->tail = new_node;
}
int pop_front(t_dequeue *dequeue)
{
t_node *node;
t_node *tmp;
int ret_val;
if (!dequeue)
return (0);
node = dequeue->head;
ret_val = node->data;
if (dequeue_size(dequeue) == 1)
{
free(node);
dequeue->head = NULL;
dequeue->tail = NULL;
}
else
{
tmp = node->next;
tmp->prev = NULL;
free(node);
dequeue->head = tmp;
}
return (ret_val);
}
void push_front(t_dequeue *dequeue, int item)
{
t_node *new_node;
if (!dequeue)
return ;
if ((new_node = (t_node *)malloc(sizeof(t_node))) == 0)
return ;
new_node->data = item;
new_node->prev = NULL;
new_node->next = dequeue->head;
if (is_empty(dequeue))
dequeue->tail = new_node;
else
dequeue->head->prev = new_node;
dequeue->head = new_node;
}
int is_empty(t_dequeue *dequeue)
{
return (dequeue->head == NULL);
}
size_t dequeue_size(t_dequeue *dequeue)
{
size_t cnt;
t_node *node;
if (!dequeue)
return (0);
if (is_empty(dequeue))
return (0);
cnt = 1;
node = dequeue->head;
while (node->next)
{
node = node->next;
cnt++;
}
return (cnt);
}