항상이 문제는 책의 연습 문제에서 비롯됩니다. 데이터 구조를 공부하지 않고 책에 관한 것도 아닙니다. 그러나 "동적 데이터 구조"장이 있습니다. 나는이 장을 이미 마쳤다.place_first 함수에서 중복을 피하기 위해 첫 번째 노드의 값을 내 대상 값으로 확인할 수 없습니다.
하지만 삽입에 문제가 있습니다. 필자의 의견으로는, 필자의 기능은 중복 노드를 만드는 것을 제외하고는 올바르게 작동합니다.
나는 예방 조치를했지만 작동하지 않습니다. 어쨌든 내 어리석은 실수로 나를 용서해주십시오. OK, 여기서 구조는 이름 목록을 입력합니다.
typedef struct name_node_s {
char name[11];
struct name_node_s *restp;
}name_node_t;
typedef struct {
name_node_t *headp;
int size;
}name_list_t;
place_first 기능 :이 같은 그 함수를 호출
name_node_t *
place_first(name_list_t *old_listp, char name[11])
{
name_list_t *new_listp, *cur_listp;
name_node_t *new_nodep, *temp_nodep;
temp_nodep = (name_node_t *)malloc(sizeof (name_node_t));
new_listp = (name_list_t *)malloc(sizeof (name_list_t));
cur_listp = (name_list_t *)malloc(sizeof (name_list_t));
new_nodep = (name_node_t *)malloc(sizeof (name_node_t));
cur_listp->headp = old_listp->headp;
temp_nodep = old_listp->headp;
new_listp = old_listp;
if (old_listp->headp->name != name){ // My first precaution for duplication
while(cur_listp->headp->restp != NULL && cur_listp->headp->name != name) // My second precaution for duplication
{
if (old_listp->headp == NULL){
strcpy(new_listp->headp->name, name);
new_listp->headp->restp = NULL;
}
else if (old_listp->headp->name != name) { // Third one.
strcpy(new_nodep->name, name);
new_nodep->restp = NULL;
new_listp->headp = new_nodep;
new_listp->headp->restp = temp_nodep;
++(old_listp->size);
}
cur_listp->headp = cur_listp->headp->restp;
}
}
else{
new_listp->headp = old_listp->headp;
}
return(new_listp->headp);
}
; 이 같은
listp->headp = place_first(listp, "Mustafa");
listp->headp = place_first(listp, "Mustafa");
내 출력 : __Mustafa __Mustafa __Ataturk __Ali __Eisenhower __Kennedy 사전에 감사합니다 ... 당신은 포인터가 아닌를 비교하고 여기에
이 ... 무엇을 매우 지저분하다 'restp'? 이름에 구조체가 필요한 이유는 무엇입니까? – betabandido
코드에는 다른 문제가있을 수 있지만 우선 문자열을 '=='(및'! =')로 비교합니다.이 문자열은 포인터가 같은지 여부를 확인합니다. 'strcmp'를 사용하여 문자열의 내용을 비교하십시오. – dfan