์๋ฃ๊ตฌ์กฐ
์ฐ๊ฒฐ๋ฆฌ์คํธ์ ํ์ฅ Circular linked list, Double linked list
์์ณ์ด
2021. 7. 19. 18:46
๊ณตํต์ | ์ฐจ์ด์ | |
๋จ์ ์ฐ๊ฒฐ ๋ฆฌ์คํธ | ํฌ์ธํฐ๋ฅผ ์ด์ฉํ์ฌ ๊ตฌํ | |
์ํ ์ฐ๊ฒฐ ๋ฆฌ์คํธ | ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ ๊ฐ์ฅ ๋ง์ง๋ง ๋ ธ๋๊ฐ ์ฒซ ๋ฒ์งธ ๋ ธ๋์ ์ฐ๊ฒฐ | |
์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ | ์๋ฐฉํฅ ๋งํฌ๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์, ํน์ ๋ ธ๋ ์ด์ ๋ ธ๋์ ๋ํ ์ง์ ์ ๊ทผ์ด ๊ฐ๋ฅ |
์ํ ์ฐ๊ฒฐ ๋ฆฌ์คํธ : ๋ง์ง๋ง ๋ ธ๋๊ฐ ์ฒซ ๋ฒ์งธ ๋ ธ๋์ ์ฐ๊ฒฐ, ๋จ์ ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ ๋ง์ฐฌ๊ฐ์ง๋ก ๋จ๋ฐฉํฅ
์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ : ๋ง์ง๋ง ๋ ธ๋๊ฐ ํค๋๋ ธ๋์ ์ฐ๊ฒฐ, ์๋ฐฉํฅ
- ๋ ธ๋์ ์ด์ ๋ ธ๋์ ๋ค์ ๋ ธ๋ == ๋ ธ๋์ ๋ค์ ๋ ธ๋์ ์ด์ ๋ ธ๋ == ๋ ธ๋
์ํ ์ฐ๊ฒฐ ๋ฆฌ์คํธ
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct CircularListNodeType {
int data;
struct CircularListNodeType *pLink;
} CircularListNode;
typedef struct CircularListType {
int currentCount;
CircularListNode headerNode;
} CircularList;
CircularList *createCircularList()
{
CircularList *pReturn = (CircularList *)malloc(sizeof(CircularList));
memset(pReturn, 0, sizeof(CircularList));
return (pReturn);
}
int getCircularListData(CircularList* pList, int position)
{
int i = 0;
CircularListNode *pCurrentNode = &(pList->headerNode);
for (i = 0; i <= position; i++)
pCurrentNode = pCurrentNode->pLink;
return (pCurrentNode->data);
}
int addCircularListData(CircularList* pList, int position, int data)
{
int i = 0;
CircularListNode *pNewNode = NULL;
CircularListNode *pPreNode = NULL;
pNewNode = (CircularListNode*)malloc(sizeof(CircularListNode));
pNewNode->data = data;
pPreNode = &(pList->headerNode);
for (i = 0; i < position; i++)
pPreNode = pPreNode->pLink;
pNewNode->pLink = pPreNode->pLink;
pPreNode->pLink = pNewNode;
pList->currentCount++;
if (pNewNode->pLink == NULL) // ๋ง์ง๋ง ๋
ธ๋๋ฅผ ์ฒซ ๋ฒ์งธ ๋
ธ๋์ ๋งํฌ
pNewNode->pLink = pList->headerNode->pLink;
return (0);
}
int removeCircularListData(CircularList* pList, int position)
{
int i = 0;
CircularListNode *pDelNode = NULL;
CircularListNode *pPreNode = NULL;
pPreNode = &(pList->headerNode);
for (i = 0; i < position; i++)
pPreNode = pPreNode->pLink;
pDelNode = pPreNode->pLink;
pPreNode->pLink = pDelNode->pLink;
pList->currentCount--;
if (0 == pList->currentCount)
pList->headerNode.pLink = NULL;
free(pDelNode);
return 0;
}
void deleteCircularList(CircularList* pList)
{
int i = 0;
CircularListNode *pDelNode = NULL;
CircularListNode *pPreNode = pList->headerNode.pLink;
for (i = 0; i < pList->currentCount; i++)
{
pDelNode = pPreNode;
pPreNode = pPreNode->pLink;
free(pDelNode);
}
free(pList);
}
void displayCircularList(CircularList* pList)
{
int i = 0;
CircularListNode* pNode = NULL;
pNode = pList->headerNode.pLink;
if (0 == pList->currentCount)
printf("์๋ฃ๊ฐ ์์ต๋๋ค\n");
else
{
printf("๋
ธ๋ ๊ฐ์: %d\n", pList->currentCount);
for (i = 0; i < pList->currentCount; i++)
{
printf("[%d],%d\n", i, pNode->data);
pNode = pNode->pLink;
}
}
}
int main(void)
{
CircularList *pList = NULL;
pList = createCircularList();
addCircularListData(pList, 0, 10);
displayCircularList(pList);
addCircularListData(pList, 0, 20);
displayCircularList(pList);
addCircularListData(pList, 1, 30);
displayCircularList(pList);
removeCircularListData(pList, 2);
displayCircularList(pList);
removeCircularListData(pList, 1);
displayCircularList(pList);
removeCircularListData(pList, 0);
displayCircularList(pList);
deleteCircularList(pList);
return (0);
}
์ด์ค ์ฐ๊ฒฐ ๋ฆฌ์คํธ
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct DoublyListNodeType {
int data;
struct DoublyListNodeType* pLLink; // ์ด์ ๋
ธ๋
struct DoublyListNodeType* pRLink; // ๋ค์ ๋
ธ๋, ๋จ์ ์ฐ๊ฒฐ ๋ฆฌ์คํธ์ pLink
} DoublyListNode;
typedef struct DoublyListType
{
int currentCount;
DoublyListNode headerNode;
} DoublyList;
DoublyList* createDoublyList()
{
DoublyList *pReturn = (DoublyList *)malloc(sizeof(DoublyList));
if (pReturn != NULL)
memset(pReturn, 0, sizeof(DoublyList));
pReturn->headerNode.pLLink = &(pReturn->headerNode);
pReturn->headerNode.pRLink = &(pReturn->headerNode);
}
return pReturn;
}
int getDoublyListData(DoublyList* pList, int position)
{
int i = 0;
DoublyListNode* pCurrentNode = &(pList->headerNode);
for (i = 0; i <= position; i++)
pCurrentNode = pCurrentNode->pRLink;
return pCurrentNode->data;
}
// ์ถ๊ฐ์ ์ ๊ฑฐ์์ ์ด์ ๋
ธ๋์ ๋ค์ ๋
ธ๋ ์ฒ๋ฆฌ
int addDoublyListData(DoublyList* pList, int position, int data)
{
int ret = 0, i = 0;
DoublyListNode *pPreNode = NULL, *pNewNode = NULL;
pNewNode = (DoublyListNode*)malloc(sizeof(DoublyListNode));
if (pNewNode != NULL)
{
memset(pNewNode, 0, sizeof(DoublyListNode));
pNewNode->data = data;
pPreNode = &(pList->headerNode);
for (i = 0; i < position; i++)
pPreNode = pPreNode->pRLink;
pNewNode->pRLink = pPreNode->pRLink;
pNewNode->pLLink = pPreNode;
pPreNode->pRLink = pNewNode; // ์ด์ ๋
ธ๋ ์ฒ๋ฆฌ
pNewNode->pRLink->pLLink = pNewNode; // ๋ค์ ๋
ธ๋ ์ฒ๋ฆฌ
pList->currentCount++;
}
else
{
printf("์ค๋ฅ, ๋ฉ๋ชจ๋ฆฌ ํ ๋น addListData()\n");
return 1;
}
return ret;
}
int removeDoublyListData(DoublyList* pList, int position)
{
int ret = 0, i = 0;
DoublyListNode *pPreNode = NULL, *pDelNode = NULL;
pPreNode = &(pList->headerNode);
for (i = 0; i < position; i++) // ์ด์ ๋
ธ๋ ์ฐพ๊ธฐ
pPreNode = pPreNode->pRLink;
pDelNode = pPreNode->pRLink;
pPreNode->pRLink = pDelNode->pRLink;
pDelNode->pRLink->pLLink = pPreNode;
free(pDelNode);
pList->currentCount--;
return ret;
}
int getDoublyListLength(DoublyList* pList)
{
int ret = 0;
if (pList != NULL)
ret = pList->currentCount;
return ret;
}
void displayDoublyList(DoublyList* pList)
{
int i = 0;
DoublyListNode* pNode = NULL;
pNode = pList->headerNode.pRLink;
if (0 == pList->currentCount)
printf("์๋ฃ๊ฐ ์์ต๋๋ค\n");
else
{
printf("๋
ธ๋ ๊ฐ์: %d\n", pList->currentCount);
for (i = 0; i < pList->currentCount; i++)
{
printf("[%d],%d\n", i, pNode->data);
pNode = pNode->pRLink;
}
}
}
void deleteDoublyList(DoublyList* pList)
{
if (pList != NULL) {
while (pList->currentCount > 0) {
removeDoublyListData(pList, 0);
}
free(pList);
}
}
int main(int argc, char *argv[])
{
DoublyList *pList = pList = createDoublyList();
if (pList != NULL)
{
addDoublyListData(pList, 0, 10);
addDoublyListData(pList, 1, 20);
addDoublyListData(pList, 1, 30);
displayDoublyList(pList);
removeDoublyListData(pList, 0);
displayDoublyList(pList);
deleteDoublyList(pList);
}
return 0;
}
์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํ ๋คํญ์ ๊ณ์ฐ ์์
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct TermType { // ํญ์ ๋ํ๋ด๋ ๊ตฌ์กฐ์ฒด
double coef; // ๊ณ์
int degree; // ์์
} Term;
typedef struct LinkedListNodeType { //๋
ธ๋
Term data;
struct LinkedListNodeType *pLink;
} LinkedListNode;
typedef struct LinkedListType {
int currentCount;
LinkedListNode headerNode;
} LinkedList, PolyList;
LinkedList *createLinkedList()
{
LinkedList *pReturn = (LinkedList *)malloc(sizeof(LinkedList));
memset(pReturn, 0, sizeof(LinkedList));
return (pReturn);
}
Term getLinkedListData(LinkedList* pList, int position)
{
int i = 0;
LinkedListNode *pCurrentNode = &(pList->headerNode);
for (i = 0; i <= position; i++)
pCurrentNode = pCurrentNode->pLink;
return (pCurrentNode->data); // ๊ตฌ์กฐ์ฒด ๋ฆฌํด
}
int addLinkedListData(LinkedList* pList, int position, Term data)
{
int i = 0;
LinkedListNode *pNewNode = NULL;
LinkedListNode *pPreNode = NULL;
pNewNode = (LinkedListNode*)malloc(sizeof(LinkedListNode));
pNewNode->data = data;
pPreNode = &(pList->headerNode);
for (i = 0; i < position; i++)
pPreNode = pPreNode->pLink;
pNewNode->pLink = pPreNode->pLink;
pPreNode->pLink = pNewNode;
pList->currentCount++;
return (1);
}
int removeLinkedListData(LinkedList* pList, int position)
{
int i = 0;
LinkedListNode *pDelNode = NULL;
LinkedListNode *pPreNode = NULL;
pPreNode = &(pList->headerNode);
for (i = 0; i < position; i++)
pPreNode = pPreNode->pLink;
pDelNode = pPreNode->pLink;
pPreNode->pLink = pDelNode->pLink;
free(pDelNode);
pList->currentCount--;
return (1);
}
void deleteLinkedList(LinkedList* pList)
{
LinkedListNode *pDelNode = NULL;
LinkedListNode *pPreNode = pList->headerNode.pLink;
while (pPreNode != NULL)
{
pDelNode = pPreNode;
pPreNode = pPreNode->pLink;
free(pDelNode);
}
free(pList);
}
int getLinkedListLength(LinkedList* pList)
{
if (NULL != pList)
return pList->currentCount;
return (0);
}
// ์ฌ๊ธฐ์๋ถํฐ ์๋ก์ด ํจ์
int addPolyNodeLast(PolyList* pList, double coef, int degree) // ๋ง์ง๋ง ์์น์ ๋
ธ๋ ์ถ๊ฐ
{
int ret = 0, position = 0;
Term term = { 0, };
term.coef = coef;
term.degree = degree;
if (pList != NULL)
{
position = pList->currentCount;
ret = addLinkedListData(pList, position, term);
}
return (ret);
}
void displayPolyList(PolyList* pList)
{
int i = 0;
LinkedListNode* pNode = NULL;
pNode = pList->headerNode.pLink;
if (0 == pList->currentCount)
printf("์๋ฃ๊ฐ ์์ต๋๋ค\n");
else
{
for (i = 0; i < pList->currentCount; i++)
{
if (i > 0)
printf(" + ");
printf("%.1fx^%d", pNode->data.coef, pNode->data.degree);
pNode = pNode->pLink;
}
printf("\n");
}
}
PolyList* polyAdd(PolyList* pListA, PolyList* pListB)
{
PolyList* pReturn = NULL;
LinkedListNode *pNodeA = NULL, *pNodeB = NULL;
double coefSum = 0;
if (pListA != NULL && pListB != NULL) {
pReturn = createLinkedList();
if (pReturn == NULL) {
printf("๋ฉ๋ชจ๋ฆฌ ํ ๋น ์ค๋ฅ, polyAdd()\n");
return NULL;
}
pNodeA = pListA->headerNode.pLink;
pNodeB = pListB->headerNode.pLink;
while (pNodeA != NULL && pNodeB != NULL)
{
int degreeA = pNodeA->data.degree;
int degreeB = pNodeB->data.degree;
if (degreeA > degreeB) // ๋
ธ๋A์ ์ฐจ์ > ๋
ธ๋B์ ์ฐจ์ ์ธ ๊ฒฝ์ฐ
{
coefSum = pNodeA->data.coef;
addPolyNodeLast(pReturn, coefSum, degreeA);
pNodeA = pNodeA->pLink;
}
else if (degreeA == degreeB) // ๋
ธ๋A์ ์ฐจ์ == ๋
ธ๋B์ ์ฐจ์ ์ธ ๊ฒฝ์ฐ
{
coefSum = pNodeA->data.coef + pNodeB->data.coef;
addPolyNodeLast(pReturn, coefSum, degreeA);
pNodeA = pNodeA->pLink;
pNodeB = pNodeB->pLink;
}
else // ๋
ธ๋A์ ์ฐจ์ < ๋
ธ๋B์ ์ฐจ์ ์ธ ๊ฒฝ์ฐ
{
coefSum = pNodeB->data.coef;
addPolyNodeLast(pReturn, coefSum, degreeB);
pNodeB = pNodeB->pLink;
}
}
// ํ์ชฝ ๋
ธ๋๊ฐ ๋จผ์ ๋๋ ๊ฒฝ์ฐ ๋จ์ ๋
ธ๋ ๋ชจ๋์ฒ๋ฆฌ
while (pNodeA != NULL) {
coefSum = pNodeA->data.coef;
addPolyNodeLast(pReturn, coefSum, pNodeA->data.degree);
pNodeA = pNodeA->pLink;
}
while (pNodeB != NULL) {
coefSum = pNodeB->data.coef;
addPolyNodeLast(pReturn, coefSum, pNodeB->data.degree);
pNodeB = pNodeB->pLink;
}
}
else
{
printf("์ค๋ฅ, NULL ๋คํญ์์ด ์ ๋ฌ๋์์ต๋๋ค, polyAdd()\n");
}
return (pReturn);
}
int main(int argc, char *argv[])
{
PolyList *pListA = NULL;
PolyList *pListB = NULL;
PolyList *pListC = NULL;
pListA = createLinkedList();
pListB = createLinkedList();
if (pListA != NULL && pListB != NULL)
{
// pListA: 7x^6 + 3x^5 + 5x^2
// pListB: 1x^5 + 2x^4 + 3x^2 + 4x^0
addPolyNodeLast(pListA, 7, 6);
addPolyNodeLast(pListA, 3, 5);
addPolyNodeLast(pListA, 5, 2);
displayPolyList(pListA);
addPolyNodeLast(pListB, 1, 5);
addPolyNodeLast(pListB, 2, 4);
addPolyNodeLast(pListB, 3, 2);
addPolyNodeLast(pListB, 4, 0);
displayPolyList(pListB);
pListC = polySub(pListA, pListB);
if (pListC != NULL)
{
displayPolyList(pListC);
deleteLinkedList(pListC);
}
deleteLinkedList(pListA);
deleteLinkedList(pListB);
}
return (0);
}