이번에는 Double LinkedList를 이용하여 데이터를 넣었다가 빼고, 리스트에 존재하는지 등에 대한 Function들을 작성해보았습니다.
(SingleLinkedList와 비교 : http://jwprogramming.tistory.com/230 )
(CircularList와 비교 : http://jwprogramming.tistory.com/233 )
(1) doublylistMain.c
#include <stdio.h>
#include <stdlib.h>
#include "doublelist.h"
int main(int argc, char* argv[]) {
int i = 0;
int arrayCount = 0;
DoublyList *pList = NULL;
DoublyListNode *pValue = NULL;
DoublyListNode node = { 0, };
pList = createDoublyList();
if (pList != NULL) {
node.data = 1;
addDLElement(pList, 0, node);
node.data = 3;
addDLElement(pList, 1, node);
node.data = 5;
addDLElement(pList, 2, node);
displayDoublyList(pList);
removeDLElement(pList, 0);
displayDoublyList(pList);
deleteDoublyList(pList);
}
return 0;
}
(2) doublylist.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "doublelist.h"
DoublyList* createDoublyList() {
DoublyList *pReturn = NULL;
int i = 0;
pReturn = (DoublyList *)malloc(sizeof(DoublyList));
if (pReturn != NULL) {
memset(pReturn, 0, sizeof(DoublyList));
pReturn->headerNode.pLLink = &(pReturn->headerNode);
pReturn->headerNode.pRLink = &(pReturn->headerNode);
}
else {
printf("오류, 메모리 할당 createDoublyList() \n");
return NULL;
}
return pReturn;
}
int addDLElement(DoublyList* pList, int position, DoublyListNode element) {
int ret = FALSE, i = 0;
DoublyListNode *pPreNode = NULL, *pNewNode = NULL, *pTempNode = NULL;
if (pList != NULL) {
if (position >= 0 && position <= pList->currentElementCount) {
pNewNode = (DoublyListNode *)malloc(sizeof(DoublyList));
if (pNewNode == NULL) {
printf("오류, 메모리 할당 addDLElement()\n");
return ret;
}
*pNewNode = element;
pNewNode->pLLink = NULL;
pNewNode->pRLink = NULL;
pPreNode = &(pList->headerNode);
for (i = 0; i < position; i++) {
pPreNode = pPreNode->pRLink;
}
pNewNode->pLLink = pPreNode;
pNewNode->pRLink = pPreNode->pRLink;
pPreNode->pRLink = pNewNode;
pNewNode->pRLink->pLLink = pNewNode;
pList->currentElementCount++;
ret = TRUE;
}
else {
printf("오류, 위치 인덱스-[%d] addDLElement()\n", position);
}
}
return ret;
}
int removeDLElement(DoublyList* pList, int position) {
int ret = FALSE;
int i = 0, arrayCount = 0;
DoublyListNode *pPreNode = NULL, *pDelNode = NULL, *pTempNode = NULL;
if (pList != NULL) {
arrayCount = getDoublyListLength(pList);
if (position >= 0 && position < arrayCount) {
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->currentElementCount--;
ret = TRUE;
}
else {
printf("오류, 위치 인덱스-[%d] removeDLElement() \n", position);
}
}
return ret;
}
DoublyListNode* getDLElement(DoublyList* pList, int position) {
DoublyListNode* pReturn = NULL;
int i = 0;
DoublyListNode* pNode = NULL;
if (pList != NULL) {
if (position >= 0 && position < pList->currentElementCount) {
pNode = pList->headerNode.pRLink;
for (i = 0; i < position; i++) {
pNode = pNode->pRLink;
}
pReturn = pNode;
}
else {
printf("오류, 위치 인덱스-[%d] getDLElement()\n", position);
}
}
return pReturn;
}
void displayDoublyList(DoublyList* pList) {
int i = 0;
if (pList != NULL) {
printf("현재 노드 개수: %d\n", pList->currentElementCount);
for (i = 0; i < pList->currentElementCount; i++) {
printf("[%d], %d\n", i, getDLElement(pList, i)->data);
}
}
else {
printf("원소가 없습니다.\n");
}
}
void deleteDoublyList(DoublyList* pList) {
if (pList != NULL) {
clearDoublyList(pList);
free(pList);
}
}
void clearDoublyList(DoublyList * pList) {
if (pList != NULL) {
while (pList->currentElementCount > 0) {
removeDLElement(pList, 0);
}
}
}
int getDoublyListLength(DoublyList* pList) {
int ret = 0;
if (pList != NULL) {
ret = pList->currentElementCount;
}
return ret;
}
(3) doublelist.h
#ifndef _DOUBLYLIST_
#define _DOUBLYLIST_
typedef struct DoublyListNodeType {
int data;
struct DoublyListNodeType* pLLink;
struct DoublyListNodeType* pRLink;
} DoublyListNode;
typedef struct DoublyListType {
int currentElementCount;
DoublyListNode headerNode;
} DoublyList;
DoublyList* createDoublyList();
int addDLElement(DoublyList* pList, int position, DoublyListNode element);
int removeDLElement(DoublyList* pList, int position);
DoublyListNode* getDLElement(DoublyList* pList, int position);
void displayDoublyList(DoublyList* pList);
void deleteDoublyList(DoublyList* pList);
void clearDoublyList(DoublyList * pList);
int getDoublyListLength(DoublyList* pList);
#endif
#ifndef _COMMON_LIST_DEF_
#define _COMMON_LIST_DEF_
#define TRUE 1
#define FALSE 0
#endif
'Programming > C' 카테고리의 다른 글
Sorting(1) Bubble Sort(버블 정렬) (0) | 2016.11.01 |
---|---|
(6) CircularList 사용하기 (0) | 2016.10.30 |
(4) Linkedlist 를 이용하여 Reverse, Concat 응용 (0) | 2016.10.30 |
(3) Linkedlist 사용하기 (0) | 2016.10.30 |
(2) ArrayStack 사용하기 (0) | 2016.10.30 |
WRITTEN BY