검색 알고리즘 구현 (전진 이동법, 전위법, 계수법)
2011-08-05
|
by cornell
2856
LinkedList을 기반으로 해서 알고리즘을 구현했어요. 저처럼 혼자서 공부하시는 분 도움 되라고 올렸어요 ㅋ.
이 책 알고리즘 복습하는 마음으로 잘 읽고 있어요. 책도 마음에 들고요 ^^
그런데 딱 하나 안좋은게 오타가 쪼금 있다는거랑 저자 & 독자 교류 활성화가 잘 안 되어있네요..
저자님 블로그에 질문 올렸었는데 답변도 없으시고..
혹시 저처럼 이 책 공부하시면서 자료나 의견을 주고 받으시고 싶은신 분들은,
http://cafe.naver.com/algorithmkorea
여기로 들어와 주세요. 혼자서 자료 업로드용으로 만든 카페인데 서로 의견공유도 하고 그러면 좋을 꺼 같아서요 ㅋ..
현재는 회원 저 혼자 -_-...
저자님도 기회되번 한 번 들러주세요... ㅋ
첨부파일로 파일 첨부하려고 했는데,,,, 안 되네요;;
http://cafe.naver.com/algorithmkorea/14
여기 들어가셔서 search.zip 다운받으시면 되요.
계수법 구현 sample code (제가 유학생이라서,, 주석을 영어로 달았어요;; 혹시 이해안 되시는 곳 있으시면 댓글달아 주세요)
//계수법 (frequency count method)
Node* FreqCount(Node** Head, int Target)
{
/*
Find the Node containing Target.
Increase the Nodes freq by one.
Reorganize List according to the freq
*/
Node* Current = (*Head);
Node* Prev = NULL; //Previous Node of Current
while(Current->Data != Target && Current != NULL)
{
Prev = Current;
Current = Current->NextNode;
}
//If Target is not found, search returns NULL
if(Current == NULL) return NULL;
/*
Search is succesful, and Current hold Target.
Increase freq of Current by one because it was searched.
Remove Current from List for now.
We will add Current to its right location by Reorganize method.
*/
Current->Freq++;
Prev->NextNode = Current->NextNode;
Reorganize(Head, Current,Current->Freq);
return Current;
}