728x90

정렬 삽입 SInsert

- LInsert 시에 정렬 기준이 등록되어 있다면 SInsert를 실행

void LInsert(List * plist, LData data)
{
	if(plist->comp == NULL)
		FInsert(plist, data);
	else
		SInsert(plist, data);
}

- 정렬 기준 comp 함수를 설정하기 위해서는 SetSortRule 함수를 실행하여 전달

void SetSortRule(List * plist, int (*comp)(LData d1, LData d2))
{
	plist->comp = comp;
}

- SInsert 

    -> pred : 삽입하려는 노드의 위치를 찾기위해 다음 노드와의 비교를 위한 노드

    -> 위치를 찾으면 pred와 pred->next 사이에 넣으면 됨

void SInsert(List * plist, LData data)
{
	Node * newNode = (Node*)malloc(sizeof(Node));
	Node * pred = plist->head;
	newNode->data = data;

	while(pred->next != NULL && plist->comp(data, pred->next->data) != 0)
	{
		pred = pred->next;
	}

	newNode->next = pred->next;
	pred->next = newNode;

	(plist->numOfData)++;
}

728x90

+ Recent posts