C를 이용하여 연결 리스트 상에서 구현한 삽입정렬
○ 소스코드
#define NULL '\0'
typedef struct _node *listptr;
typedef struct _node
{
int data;
listptr link;
} node;
void push(listptr* head, int data)
{
listptr tmp;
tmp = (listptr)malloc(sizeof(node));
tmp->data = data;
if (*head)
{
tmp->link = *head;
*head = tmp;
}
else
{
*head = tmp;
tmp->link = NULL;
}
}
void bulidList(listptr *head, int n)
{
int i;
for (i=0; i<n; i++)
{
push(head,i);
}
}
void printListAll(listptr head)
{
printf("head");
while(1)
{
if (head == NULL)
{
printf("-||\n");
break;
}
else
{
printf("->%d",head ->data);
}
head = head->link;
}
}
void sortedInsertver1(listptr *head, listptr newNode)
{
listptr current = *head;
printf("processing node data is %d\n",newNode->data);
if (*head == NULL || (*head)->data >= newNode->data )
{
newNode->link = *head;
*head = newNode;
}
else
{
while(current->link != NULL && (current->link < newNode->data))
{
current = current->link;
}
newNode->link = current->link;
current->link = newNode;
}
}
void insertSort(listptr *head)
{
listptr current = *head;
listptr result = NULL;
listptr next;
while(current != NULL)
{
next= current->link;
sortedInsertver1(&result, current);
printListAll(result);
current = next;
}
*head = result;
}
int main()
{
int n = 6;
int i, count;
listptr head = NULL;
listptr tmp;
bulidList(&head, n);
printListAll(head);
tmp = (listptr)malloc(sizeof(node));
tmp->data = 100;
tmp->link = NULL;
sortedInsert(&head, tmp);
printListAll(head);
insertSort(&head);
printListAll(head);
return 0;
}
'study' 카테고리의 다른 글
노인단독가구에 대해 다운받기 (0) | 2020.08.28 |
---|---|
우리 나라의 수학교육 변천 과정 (0) | 2020.08.28 |
[독후감/서평] - 1분 경영수업 : 켄 블랜차드가 최고의 비즈니스 멘토들에게 배웠던 모든 것 (0) | 2020.08.27 |
밀가루의 종류에 따른 글루텐 형성 정도의 차이와 설탕, 마가린, 소금의 첨가가 글루텐 형성에 미치는 영향 (0) | 2020.08.23 |
자기소개서 작성요령과 1분 스피치목차I. 에스오일 (0) | 2020.08.10 |
미국 증권시장의 기초 (0) | 2020.08.10 |
열역학 - 물리적 원인, 비율방정식, 전도 (0) | 2020.08.10 |
[서평] 경영, 과학에게 길을 묻다 (0) | 2020.07.30 |
댓글