2012-12-07 5 views
2
CAR *removing(int *numberofstructures,CAR *first) 
{ 
    char categorytoerase[51]; 
    CAR *helpnode,*actual; 
    int i; 
    int number_1=0; 
    helpnode=(CAR*)malloc(sizeof(CAR)); 
    actual=(CAR*)malloc(sizeof(CAR)); 
    actual=first; 
    number_1=*numberofstructures; 
    helpnode=NULL; 
    scanf("%s",categorytoerase); 
    for(i=1;i<=number_1;i++) 
    { 
     if (actual->znacka==categorytoerase) 
     { 
      if (helpnode != NULL) { 
       helpnode->next=actual->next; 
       free((void *)actual); 
       actual=helpnode->next; 
      } 
      else 
      { 
       first = actual -> next; 
       free((void *)actual); 
       actual = first; 
      } 
     } 
     else{ 
      helpnode=actual; 
      actual=actual->next; 
     } 
    } 
    return first; 
} 

문자열을 입력해야하는 링크 된 목록에서 노드를 제거하는 함수를 만들고 싶습니다. 입력 된 문자열과 같은 자동차 카테고리 이름을 가진 노드를 지워야합니다.링크 된 목록 지우기 노드

+1

http://stackoverflow.com/questions/69209/deleting-a-middle-node-from-a-single-linked-list-when-pointer-to-the-previous-no, http : // stackoverflow .com/questions/13744946/delete-node-linked-list-recursively, http://stackoverflow.com/questions/13656061/delete-node-from-linked-list-with-specific-value –

답변

1

이것은 마치 숙제처럼 보입니다 .... 그래서 당신을 위해 대답을 쓰지 않는 그 성기라는 정신으로 나는 노드를 삭제할 생각을 전하겠습니다.

노드는 데이터와 다음 노드를 가리키는 주소를 포함합니다.

그래서 당신은, 당신은 그 방법 ... 머리에

시작을 만들 수 있다는 것을 알고 현재 노드와 이전 노드에 대한 참조를 가지고 있기 때문에

당신은에 대한 목록을 검색 노드를 삭제해야하는 경우에는 항상 현재 및 이전 노드 변수를 순환하는 입니다.

찾고있는 노드를 찾을 때 이전 노드의 다음 주소 포인터 을 삭제하려는 노드의 다음 주소 포인터로 설정하십시오.

행운의 수석!

0

AMR이 맞습니다. 이중 연결 목록이있는 경우 노드를 삭제하는 것이 더 쉬워 지므로 노드의 구조에 이전 포인터와 다음 포인터를 모두 포함하십시오. 기본적으로, 여기 삭제가 (당신이 발견 한 후에 삭제하려는 노드에 대한 포인터가) 의사에 일어날 방법은 다음과 같습니다

IF todelete.prev != NULL THEN 
    todelete.prev.next = todelete.next 
ELSE 
    list.head = todelete.next 
END IF 
IF todelete.next != NULL THEN 
    todelete.next.prev = todelete.prev 
ELSE 
    list.tail = todelete.prev 
END IF 

FREE todelete 

조건이 중요하다 경우; 그렇지 않으면 프로그램이 충돌하고 논리가 실제로 작동하지 않습니다. 존재하지 않는 것을 대체 할 수는 없습니다.