Skip to content

Instantly share code, notes, and snippets.

@dalcon10028
Created June 3, 2020 05:18
Show Gist options
  • Save dalcon10028/6bba655f98a026441e7c8216fa017dd1 to your computer and use it in GitHub Desktop.
Save dalcon10028/6bba655f98a026441e7c8216fa017dd1 to your computer and use it in GitHub Desktop.
연결 리스트 실습 3
#include <stdio.h>
#include <stdlib.h>
/* 노드 정의 */
struct _node {
int data;
struct _node *link;
};
typedef struct _node node;
/* 데이터를 찾는 함수 */
node * search_node(int item,node *head) {
/* 원하는 데이터를 찾을 때까지 */
while ( head->data != item ) {
/* 마지막 노드에 도달하면 NULL을 반환합니다. */
if ( head->link == NULL ) return (NULL);
/* 아니면 노드의 이동 */
else head = head->link;
}
/* 찾으면 데이터 반환 */
return (head);
}
main(void) {
node *current, *temp, *head;
int num,item;
/* 앞 예제와 동일 */
current = (node*) malloc(sizeof(node));
current->data = 1;
current->link = NULL;
head = temp = current;
num = 2;
while ( num <= 10 ) {
current = (node*) malloc(sizeof(node));
if ( current == NULL ) {
printf("Memory Allocation Failed!\n");
exit(0);
}
current->data = num;
current->link = NULL;
temp->link = current;
temp = current;
num++;
}
/* 여기까지 앞 예제와 동일 */
printf("찾을 자료는 = ");
scanf("%d",&item);
/* 찾기 함수 */
head = search_node(item,head);
/* 마지막 노드까지 존재 하지 않으면 */
if ( head == NULL ) printf("Not Found\n");
/* 찾았으면 */
else printf("Founded Data = %d\n",head->data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment