https://kin.naver.com/qna/detail.nhn?d1id=1&dirId=1040101&docId=312021222

# include <stdio.h>

# include <stdlib.h>

# define ERROR 1

# define OR ||

// ===============================

typedef int element;


typedef struct ListNode {

element data;

struct ListNode* link;

}ListNode;


typedef struct ListLink {

ListNode *phead;

}ListLinkType;

// ===============================

void init(ListLinkType *param);                              // func (1)

ListNode* create_node(element data);                         // func (2)

void front_insert_node(ListLinkType* s, ListNode* new_node); // func (3)

void display(ListLinkType* s);                               // func (4)

void front_remove_node(ListLinkType* s);                     // func (5)

void concat(ListLinkType* s1, ListLinkType* s2);             // func (6)

ListNode* search(ListLinkType* s1, int x);                   // func (7)

// ===============================

int main(void) {

ListNode* new_node = NULL;

ListNode* p = NULL;

ListLinkType s1, s2;

init(&s1); // 초기화 1

init(&s2); // 초기화 2

// ==========================================

// 데이터 삽입 (10)

new_node = create_node(10); 

front_insert_node(&s1, new_node);

// 데이터 삽입 (20)

new_node = create_node(20);

front_insert_node(&s1, new_node);

// 데이터 삽입 (30)

new_node = create_node(30);

front_insert_node(&s1, new_node);

// ==========================================

// 데이터 출력

display(&s1);

// ==========================================

front_remove_node(&s1);

// 데이터 출력

printf("s1 node >>>   ");

display(&s1);


// 데이터 삽입 (40)

new_node = create_node(40);

front_insert_node(&s2, new_node);

// 데이터 삽입 (50)

new_node = create_node(50);

front_insert_node(&s2, new_node);

// 데이터 출력

printf("s2 node >>>   ");

display(&s2);


concat(&s1, &s2);

display(&s1);


p = search(&s1, 10);

if (p != NULL) {

printf("탐색 성공: %d \n", p->data);

else {

// p == NULL

printf("탐색 실패!!\n");

}

return 0;

} // end of main function 

void init(ListLinkType *param) {

param->phead = NULL;

} // end of init function 

// ===============================

ListNode* create_node(element data) {

ListNode* new_node = NULL;

new_node = (ListNode*)malloc(sizeof(ListNode));

if (new_node == NULL) {

printf("메모리 할당 에러 \n");

exit(ERROR);

}

else {

// new_node != NULL

new_node->data = data;

new_node->link = NULL;

}

return new_node;

} // end of create_node function 

// ===============================

void front_insert_node(ListLinkType* s, ListNode* new_node) {

if (s->phead == NULL) {

// 노드가 처음 추가되는 경우

s->phead = new_node;

}

else {

// s->phead != NULL

new_node->link = s->phead;

s->phead = new_node;

}

} // end of front_insert_node function 

// ===============================

void display(ListLinkType* s) {

ListNode *index = s->phead;

while (index != NULL) {

printf("%d", index->data);

if (index->link != NULL) {

printf("->");

}

index = index->link;

}

printf("\n\n");

} // end of display function 

// ===============================

void front_remove_node(ListLinkType* s) {

ListNode* removed = NULL;

if (s->phead == NULL) {

printf("삭제할 데이터가 없습니다!!! \n");

return;

}

else {

// s->phead != NULL

removed = s->phead;

s->phead = s->phead->link;

free(removed); // front node remove

}

} // end of front_remove_node function 

// ===============================

void concat(ListLinkType* s1, ListLinkType* s2) {

ListNode* p = s1->phead;

if (s1->phead == NULL OR s2->phead == NULL) {

return; // 종료

}

else {

// s1->phead != NULL AND s2->phead != NULL

while (1) {

if (p->link == NULL) {

break;

}

else {

p = p->link;

}

}

p->link = s2->phead;

}

} // end of concat function 

// ===============================

ListNode* search(ListLinkType* s1, int x) {

ListNode* p = s1->phead;

while (p != NULL) {

if (p->data == x) {

return p;

}

p = p->link;

}

return NULL; // 탐색 실패

} // end of search function 

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

네이버 지식인 풀이, c언어 숫자 거꾸로 출력  (0) 2018.11.03
네이버 지식인 풀이  (0) 2018.10.14
네이버 지식인 문제 풀이  (0) 2018.10.09
지식인 풀이  (0) 2018.10.09
행렬  (0) 2018.10.09