728x90
연결리스트 관련 함수
1. 순회
2. 연결
3. 역순
헤더 파일 추가(linkedlistop.h)
#ifndef _LINKEDLIST_OP_
#define _LINKEDLIST_OP_
void iterateLinkedList(LinkedList* pList);
void concatLinkedList(LinkedList* pListA, LinkedList* pListB);
void reverseLinkedList(LinkedList* pList);
#endif
리스트 실행 소스(linkedListop.c)
순회함수 iterateLinkedList()
void iterateLinkedList(LinkedList* pList){
ListNode* pNode = NULL;
int count = 0;
if(pList != NULL){
pNode = pList -> headerNode.pLink;
while(pNode != NULL){
printf("[%d], %d\n",count,pNode->data);
count++;
pNode = pNode -> pLink;
}
print("노드개수 : %d\n", count);
} else {
printf("공백 리스트 입니다");
}
}
연결리스트의 연결
기존에 연결되어 있던 연결을 끊고 NULL을 바라보도록 함
void concatLinkedList(LinkedList* pListA, LinkedList* pListB){
ListNode* pNodeA = NULL, *pNodeB = NULL;
if(pListA != NULL && pListB != NULL){
pNodeA = pListA -> headerNode.pLink;
while(pNodeA -> pLink != NULL){
pNodeA = pNodeA->pLink;
}
pNodeA -> pLink = pListB ->headerNode.pLink;
pListA -> currentElementCount += pListB -> currentElementCount;
pListB -> headerNode.pLink = NULL;
pListB -> currentElementCount = 0;;
}
}
연결리스트의 역순 만들기
void reverseLinkedList(LinkedList* pList){
ListNode* pNode = NULL, *pCurrentNode = NULL, *pPrevNode = NULL;
if(pList != NULL){
pNode = pList -> headerNode.pLink; //(1)
pCurrentNode = pNode; //(2)
pNode = pNode->pLink; //(3)
pCurrentNode -> pLink = pPrevNode; //(4)
}
pList -> headerNode.pLink = pCurrentNode;
}
}
순회하는 노드
(4) 부분이 역순의 핵심이다
반응형
'Study Log > 자료구조' 카테고리의 다른 글
[자료구조] 스택의 개념과 스택 추상 자료형 (0) | 2021.05.11 |
---|---|
[자료구조] 연결리스트의 응용 - 다항식 (0) | 2021.05.06 |
[자료구조] 연결리스트의 종류와 특성 (0) | 2021.05.03 |
[자료구조] 연결리스트의 개념과 배열리스트와의 비교 (0) | 2021.05.02 |
[자료구조] 배열리스트의 특징과 원소 추가 제거 방법 (0) | 2021.04.27 |