원형 링크드

언어/c언어2017. 10. 5. 00:04

// circle linkedlist

# include <stdio.h>

# include <stdlib.h>

# include <string.h>

# include <stdbool.h>

typedef struct circleNode {

struct circleNode* Rlink;

//struct circleNode* Llink;

char nData;

}circleNode, *ptrCircleNode;


typedef struct node {

ptrCircleNode headNode;

ptrCircleNode tailNode;

int count;

int element;

}node, *ptrNode;

// 함수 원형

ptrNode createNode(); // [1]

void init(node** param); // [2]

ptrCircleNode createCircleNode(); // [3]

void dataInsert(node** param); // [4]

void testPrintf(node** param); // [5]

void ResultPrintf(node** param); // [6]

int main(void) {

ptrNode pNode = (ptrNode)malloc(sizeof(node));

init(&pNode); // [2]


for (int i = 0; i < 10; i++) {

dataInsert(&pNode);

// A,B,C,D,E,F,G,H,I,J

}

//testPrintf(&pNode);

ResultPrintf(&pNode);

free(pNode);

return 0;

} // end of main function

/*

ptrNode createNode() { // [1]

ptrNode node = (ptrNode)malloc(sizeof(node));

return node;

} // end of createNode function

*/

void init(node** param) { // [2] 

if ((*param) == NULL) {

fprintf(stderr, "malloc error ... !!!\n");

exit(1);

}

else {

// (*param) != NULL

(*param)->headNode = NULL;

(*param)->tailNode = NULL;

(*param)->count = 0;

(*param)->element = 65; // 'A'

}

} // end of init function

/*

ptrCircleNode createCircleNode() { // [3]

ptrCircleNode sNode = (ptrCircleNode)malloc(sizeof(circleNode));

return sNode;

} // end of createCircleNode function

*/

void dataInsert(node** param) { // [4]

ptrCircleNode nNode = (ptrCircleNode)malloc(sizeof(circleNode));

if (nNode == NULL) { exit(1); }

else { // nNode != NULL

//nNode->Llink = NULL;

nNode->Rlink = NULL;

nNode->nData = (char)(** param).element;

(** param).element++; // A -> B

if ((*param)->count == 0) {

(** param).headNode = nNode;

(** param).tailNode = nNode;

nNode->Rlink = (** param).headNode;

(** param).count++;

}

else {

//(*param)->count >= 1

(** param).tailNode->Rlink = nNode;

nNode->Rlink = (** param).headNode;

(** param).tailNode = nNode;

(** param).count++;

}

}

} // end of dataInsert function


void testPrintf(node** param) { // [5]

ptrCircleNode index = NULL;

index = (** param).headNode;

for (int i = 0; i < 10; i++) {

printf("%c ", index->nData);

index = index->Rlink;

}

printf("\n");

} // end of testPrintf function


void ResultPrintf(node** param) { // [6]

ptrCircleNode index = NULL;

index = (** param).headNode;

int arr[10] = { 0, };

for (int n = 0; n < (** param).count; n++) {

printf("%c", index->nData);

if (n < (** param).count - 1) { printf(", "); }

index->nData = '#';

for (int i = 0; i < 4; i++) {

while (true) {

if (index->Rlink->nData == '#') {

index = index->Rlink;

if (n == (** param).count-1) { break; }

}

else {

break;

}

}

index = index->Rlink;

}

}

printf("\n");

} // end of ResultPrintf function 

'언어 > c언어' 카테고리의 다른 글

네이버 문제 풀이 ㅋㅋㅋ c언어 중간고사가 다가오나보군 ㅋㅋㅋ  (0) 2017.10.06
더블릿 koi_watch 문제  (0) 2017.10.05
진수 변환  (0) 2017.10.04
c언어 문제 lavida  (0) 2017.10.02
c언어 URL  (0) 2017.10.02