리스트관련 질문입니다.
바나나
질문 제목 :삭제함수에 이상이있는지 여부좀 판단해주세요 ㅡdelete 함수에 문제가있다는데 뭔지 모르겟스빈다.질문 내용 : 컴파일 여부보다 로직적으로 문제가 있는지좀 봐주시면 감사하겠습니다.
typedef struct{
listnode *head;
int length;
}linkedlisttype;
void clear(linkedlisttupe * list)
{
int i;
for(i=0;ilist-length;i++)
delete(list, i);
}
void delete(linkedlisttype * list, int pos)
{
if(!is_empty(list) && (pos=0) && (poslist-length)){
listnode * p = get_node_at(list, pos-1);
remove_node(&(list-head),p,(p!=null)?p-link:null)
list-length--;
}
}
listnode * p = get_node_at(linkedlisttype *list, int pos)
{
int i;
listnode * tmp_node = list-head;
if( pos 0 ) return null;
for(i=0; ipos; i++)
tmp_node = tmp_node-link;
return tmp_node;
}
void remove_node(listnode **phead, listnode *p, listnode *removed) // p는 삭제되는 노드 앞노드의 포인터입니다.
{
if(p==null)
*phead = (*phaed)-link;
else
p-link = removed -link;
free(removed);
}
-
초코향
혹시 clear 함수에서 pos값이 0부터 시작되기때문에 delete 함수에서 pos-1 이 문제가 되는걸까요?