단일 연결리스트 정렬질문입니다...꾸벅
별솔
#includestdio.h
#includestdlib.h
typedef struct _node
{
int key;
struct _node *next;
}node;
node *head,*tail;
void init_insertion_sort()
{
head = (node*)malloc(sizeof(node));
tail = (node*)malloc(sizeof(node));
head-next = tail;
tail-next = tail;
}
node *insert_node(node *p, int k)
{
node *s;
s = (node*)malloc(sizeof(node));
s-key = k;
s-next = p-next;
p-next = s;
return p;
}
node *insertion_sort(node *p)
{
node *s;
node *temp;
temp = head;//임시노드
while(s!=tail){
for(s=p-next; s!=tail; s=s-next){
if(s-key p-key)
{
p-next = s-next;
s-next = temp-next;
temp-next = s;
temp = temp-next;
}
}
}
return p;
}
void print_node(node *p)
{
printf(\n);
while(p != tail)
{
printf(%-8d, p-key);
p=p-next;
}
}
void main(void)
{
init_insertion_sort();
insert_node(head,1);
insert_node(head,3);
insert_node(head,6);
insert_node(head,4);
insert_node(head,5);
insert_node(head,9);
insertion_sort(head-next);
print_node(head-next);
printf(\n);
}
삽입정렬 개념을 도입한거 아니지만..일단 아무렇게나 정렬을 성공하고 선택, 삽입.등등을 생각하고 싶은데..정렬이 안되네요..
많이 고민을 해봤는데.. 링크가 꼬이거나 끊겨서...휴~ 고수님들 좀 알려주시면 감사요..
정렬함수만 보시고 좀 수정좀 부탁드립니다..팁도 좋구요..
-
나려
삽입을 하면서 정렬을 하는건 쉬울듯한데요..일단 이 정렬부터 해결하고 싶어요...
-
해찬나래
제가 봤을때는 삽입후에 다시 정렬을 하는데 차라리 삽입을 하면서 정렬을 하면 괜찮을듯 한데요..
head - tali 에서 들어오면 head.data s.data 이면 next로 넘어가서 다시 비교하구 그렇게요..ㅎ -
큰가람
웁.. 뭔가 보이는거 같으면서 복잡한.. 주석 좀 달아주세요 저두 공부하게요..ㅎㅎ