Deque (Double-Ended Queue)
typedef struct DequeNodeType
{
char data;
struct DequeNodeType* pRLink;
struct DequeNodeType* pLLink;
} DequeNode;
typedef struct LinkedDequeType
{
int currentElementCount; // 현재 원소의 개수
DequeNode* pFrontNode; // Front 노드의 포인터
DequeNode* pRearNode; // Rear 노드의 포인터
} LinkedDeque;
LinkedDeque* createLinkedDeque();
int insertFrontLD(LinkedDeque* pDeque, DequeNode element);
int insertRearLD(LinkedDeque* pDeque, DequeNode element);
DequeNode* deleteFrontLD(LinkedDeque* pDeque);
DequeNode* deleteRearLD(LinkedDeque* pDeque);
DequeNode* peekFrontLD(LinkedDeque* pDeque);
DequeNode* peekRearLD(LinkedDeque* pDeque);
void deleteLinkedDeque(LinkedDeque* pDeque);
int isLinkedDequeEmpty(LinkedDeque* pDeque);
void displayLinkedDeque(LinkedDeque *pDeque);
LinkedDeque* createLinkedDeque()
{
LinkedDeque *deque;
deque = (LinkedDeque *)malloc(sizeof(LinkedDeque));
if (deque == NULL)
return (NULL);
deque->currentElementCount = 0;
deque->pFrontNode = NULL;
deque->pRearNode = NULL;
return (deque);
}
NULL
을 가리킴int insertFrontLD(LinkedDeque* pDeque, DequeNode element)
{
DequeNode *new;
new = (DequeNode *)malloc(sizeof(DequeNode));
if (new == NULL)
return (FALSE);
*new = element;
if (isLinkedDequeEmpty(pDeque) == TRUE)
{
new->pLLink = NULL;
new->pRLink = NULL;
pDeque->pFrontNode = new;
pDeque->pRearNode = new;
}
else
{
new->pLLink = NULL;
new->pRLink = pDeque->pFrontNode;
pDeque->pFrontNode->pLLink = new;
pDeque->pFrontNode = new;
}
pDeque->currentElementCount++;
return (TRUE);
}
NULL
을 가리킴int insertRearLD(LinkedDeque* pDeque, DequeNode element)
{
DequeNode *new;
new = (DequeNode *)malloc(sizeof(DequeNode));
if (new == NULL)
return (FALSE);
*new = element;
if (isLinkedDequeEmpty(pDeque) == TRUE)
{
new->pLLink = NULL;
new->pRLink = NULL;
pDeque->pFrontNode = new;
pDeque->pRearNode = new;
}
else
{
new->pRLink = NULL;
new->pLLink = pDeque->pRearNode;
pDeque->pRearNode->pRLink = new;
pDeque->pRearNode = new;
}
pDeque->currentElementCount++;
return (TRUE);
}
NULL
을 가리킴헤드 노드와 리어 노드는 NULL
새로운 프론트 노드의 왼쪽은 NULL
을 가리킴
DequeNode* deleteFrontLD(LinkedDeque* pDeque)
{
DequeNode *ret;
ret = peekFrontLD(pDeque);
if (ret == NULL)
return (NULL);
if (pDeque->currentElementCount == 1)
{
pDeque->pFrontNode = NULL;
pDeque->pRearNode = NULL;
}
else
{
pDeque->pFrontNode = ret->pRLink;
pDeque->pFrontNode->pLLink = NULL;
}
ret->pRLink = NULL;
pDeque->currentElementCount--;
return (ret);
}
NULL
을 가리킴헤드 노드의 프론트 노드는 NULL
새로운 리어 노드의 오른쪽은 NULL
을 가리킴
DequeNode* deleteRearLD(LinkedDeque* pDeque)
{
DequeNode *ret;
ret = peekRearLD(pDeque);
if (ret == NULL)
return (NULL);
if (pDeque->currentElementCount == 1)
{
pDeque->pFrontNode = NULL;
pDeque->pRearNode = NULL;
}
else
{
pDeque->pRearNode = ret->pLLink;
pDeque->pRearNode->pRLink = NULL;
}
ret->pLLink = NULL;
pDeque->currentElementCount--;
return (ret);
}