Linkedlist를 이용하여 List에 대한 사용은 지난 포스팅http://jwprogramming.tistory.com/230 ) 에서 살펴보았습니다. 이제 Linkedlist를 이용하여 데이터를 reverse하고, Concat하는 부분을 해보았습니다.


(참고 : http://jwprogramming.tistory.com/230 )

아래 소스를 위해서는 지난 포스팅에서 진행했었던 linkedlist.h 가 추가로 필요합니다.!


(1) linkedlistopmain.c

#include <stdio.h>

#include <stdlib.h>

#include "linkedlist.h"

#include "linkedlistop.h"


int main(int argc, char* argv[]) {

int i = 0, arrayCount = 0;

LinkedList *pListA = NULL, *pListB = NULL;

ListNode node;

pListA = createLinkedList();

pListB = createLinkedList();

if (pListA != NULL && pListB != NULL) {

node.data = 1;

addLLElement(pListA, 0, node);

node.data = 2;

addLLElement(pListA, 1, node);

node.data = 3;

addLLElement(pListA, 2, node);

node.data = 4;

addLLElement(pListB, 0, node);

node.data = 5;

addLLElement(pListB, 1, node);

iterateLinkedList(pListA);

iterateLinkedList(pListB);

concatLinkedList(pListA, pListB);

printf("After concatLinkedList()\n");

iterateLinkedList(pListA);

iterateLinkedList(pListB);

reverseLinkedList(pListA);

printf("After reverseLinkedList()\n");

iterateLinkedList(pListA);

deleteLinkedList(pListA);

deleteLinkedList(pListB);

}

return 0;

}


(2) linkedlistop.c

#include <stdio.h>

#include <stdlib.h>

#include "linkedlist.h"

#include "linkedlistop.h"


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;

}

printf("노드 개수 : %d\n", count);

}

else {

printf("공백 리스트입니다.\n");

}

}


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;

while (pNode != NULL) {

pPrevNode = pCurrentNode;

pCurrentNode = pNode;

pNode = pNode->pLink;

pCurrentNode->pLink = pPrevNode;

}

pList->headerNode.pLink = pCurrentNode;

}

}


(3) linkedlistop.h

#ifndef _LINKEDLIST_OP_

#define _LINKEDLIST_OP_


void iterateLinkedList(LinkedList* pList);

void concatLinkedList(LinkedList* pListA, LinkedList* pListB);

void reverseLinkedList(LinkedList* pList);


#endif

'Programming > C' 카테고리의 다른 글

(6) CircularList 사용하기  (0) 2016.10.30
(5) DoubleLinkedList 사용하기  (0) 2016.10.30
(3) Linkedlist 사용하기  (0) 2016.10.30
(2) ArrayStack 사용하기  (0) 2016.10.30
(1) ArrayList 사용하기  (0) 2016.10.30

WRITTEN BY
SiriusJ

,