안녕하십니까 저는 오늘 처음 가입한 change1995입니다
글쓴이: change1995 / 작성시간: 토, 2018/01/20 - 2:44오후
자기 소개부터 하기전에 코딩이 안되서 이 사이트에 방문했습니다
제가쓴 소스코드는 한 책에 나오는 소스코드를 거의 그대로 썻는데 책의 내용결과랑 다르게 나와서 좀 여쭤보려고 합니다 답변 해주시면 정말 감사드립니다
결과는 다운로드함에 넣었습니다
#include <stdio.h> #include <stdlib.h> typedef int element; struct ListNode { struct ListNode * link; element data; }; typedef struct ListNode ListNode; void error(char *message) { fprintf(stderr, "%s\n", message); exit(1); } void insert(ListNode ** phead, ListNode * p, ListNode * new_node) { if (*phead == NULL) { new_node->link = NULL; *phead = new_node; } else if (p == NULL) { new_node->link = *phead; *phead = new_node; } else { new_node->link = p->link; p->link = new_node; } } void remove_node(ListNode ** phead, ListNode *p, ListNode * removed) { if (p == NULL) { *phead = (*phead)->link; } else p->link = removed->link; free(removed); } void display(ListNode *head) { ListNode *p = head; while (p != NULL) { printf("%d->", p->data); p = p->link; } printf("\n"); } void display_recur(ListNode *head) { ListNode *p = head; while (p != NULL) { printf("%d->", p->data); display_recur(p->link); } } ListNode *search(ListNode *head, int x) { ListNode *p; p = head; while (p != NULL) { if (p->data == x) return p; } return p; } ListNode *concat(ListNode * head1, ListNode * head2) { ListNode *p; if (head1 == NULL) return head2; else if (head2 == NULL) return head1; else { p = head1; while (p->link != NULL) p = p->link; p->link = head2; return head1; } } ListNode * reverse(ListNode * head) { ListNode * p, *q, *r; p = head; q = NULL; while (p != NULL) { r = q; q = p; p = p->link; q->link = r; } return q; } ListNode *creat_node(element data, ListNode *link) { ListNode *new_node; new_node = (ListNode*)malloc(sizeof(ListNode)); if (new_node == NULL) error("메모리 할당 에러"); new_node->data = data; new_node->data = link; return(new_node); } void main() { ListNode * list1 = NULL, *list2 = NULL; ListNode *p; insert(&list1, NULL, creat_node(10, NULL)); insert(&list1, NULL, creat_node(20, NULL)); insert(&list1, NULL, creat_node(30, NULL)); display(list1); remove_node(&list1, NULL, list1); display(list1); insert(&list2, NULL, creat_node(60, NULL)); insert(&list2, NULL, creat_node(70, NULL)); insert(&list2, NULL, creat_node(80, NULL)); display(list2); list1 = concat(list1, list2); display(list1); list1 = reverse(list1); display(list1); p = search(list1, 20); printf("탐색성공 : %d\n", p->data); }
File attachments:
첨부 | 파일 크기 |
---|---|
캡처.JPG | 23.24 KB |
Forums:
ListNode *creat_node(element
댓글 달기