์ž๋ฃŒ๊ตฌ์กฐ

์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ํ™•์žฅ 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);
}