2013-04-12 3 views
-1

나는 연결된 목록 예제를 통해 작업하고 있습니다. 나는 특정 요소를 찾기 위해 목록을 탐색하는 찾기 및 제거 기능에 있습니다. find_remove 함수가 작동하지 않습니다. 응용 프로그램 내 다른 모든 기능 작동 (head_return, tail_return 등)head_return 및 tail_return을 사용하여 연결된 목록에서 찾기 및 제거

find_remove를 사용하여 내가 어디로 잘못 가고 있는지 지적 할 수 있으면 고맙겠습니다.

#include <iostream> 
using namespace std; 

struct node_ll 
{ 
    int payload; 
    node_ll* next;//Pointer to the next node 
}; 

void head_insert(node_ll** list, int pload) 
{ 
    node_ll* temp = new node_ll;//Create a new node, and let temp be the address of that node. 
    temp->payload = pload;//Set the payload of the struct whose address is temp to pload. 
    temp->next = *list;//Set the next of the struct whose address is temp to the address of the old head of the list. 
    *list = temp;//The address of the old head of the list is changed to the address of the struct temp. 
}; 

void tail_insert(node_ll** list, int pload) 
{ 
    if (*list == NULL) 
    { 
     head_insert(list, pload); 
    } 
    else 
    { 
     node_ll* temp = new node_ll; 
     for (temp = *list; temp->next; temp = temp->next); 
     temp->next = new node_ll; 
     temp->next->payload = pload; 
     temp->next->next = NULL; 
    } 
} 

int head_return (node_ll** list) 
{ 
    if (*list != NULL) 
    { 
    int temp = (*list)->payload; 
     node_ll* trash = *list; 
     *list = (*list)->next; 
     delete trash; 
     return temp; 
    } 
    else 
    { 
     return 0; 
    } 
} 

int tail_return (node_ll** list) 
{ 
    if (*list != NULL) 
    { 
     if ((*list)->next == NULL) 
     { 
      return head_return(list); 
     } 
     else 
     { 
     node_ll* trash; 
      for (trash = *list; trash->next->next; trash = trash->next); 
      int temp = trash->next->payload; 
      delete trash->next; 
      trash->next = NULL; 
      return temp; 
     } 
    } 
    else 
    { 
     return 0; 
    } 
} 

void find_remove (node_ll** list, int pload) 
{ 
    if (*list != NULL) 
    { 
     node_ll* temp;//Declared before loop for use after loop. 
     for (temp = *list; temp->next; temp = temp->next) 
     { 
      if (temp->payload == pload) 
      { 
       int trash = head_return(&temp); 
      } 
     } 
     if (temp->payload == pload) 
     { 
      int trash = tail_return(list); 
     } 
    } 
} 

void print_ll (node_ll** list) 
{ 
    node_ll* temp = *list;//Let temp be the address of the node that is the head of the list. 
    while(temp)// != NULL 
    { 
     cout << temp->payload << endl;//Print out payload of the struct whose address is temp. 
     temp = temp->next;//Set the address of temp equal to the address stored in next of the struct whose address is temp. 
    } 
} 

int main() 
{ 
    node_ll *blist = NULL; 
    tail_insert(&blist, 2); 
    tail_insert(&blist, 4); 
    tail_insert(&blist, 6); 
    find_remove(&blist, 4); 
    print_ll(&blist); 
    cout << '\n'; 

    system("PAUSE"); 
    return 0; 
} 

편집 :

이 여기에 코드를 다시 작성에서 내 시도이다.

void find_remove (node_ll** list, int pload) 
{ 
    if (*list != NULL) 
    { 
     while (*list && (*list)->payload == pload) 
     { 
      head_return(list); 
     } 
     if (*list != NULL) 
     { 
      node_ll* temp; 
      for (temp = *list; temp->next->next; temp = temp->next) 
      { 
       if (temp->next->payload == pload) 
       { 
        node_ll* trash = temp->next; 
        temp->next = temp->next->next; 
        head_return(&trash); 
       } 
      } 
     } 
    } 
} 

편집 2 : 감사합니다! 이 새로운 솔루션은 처음에 여러 개의 matchine 노드가 있고 중간에 일치하는 여러 노드가 있거나 끝에 여러 개의 일치 노드가있는 경우 작동합니다.

void find_remove (node_ll** list, int pload) 
{ 
    if (*list != NULL) 
    { 
     while (*list && (*list)->payload == pload) 
     { 
      head_return(list); 
     } 
     if (*list != NULL) 
     { 
      node_ll* temp; 
      for (temp = *list; temp->next; temp = temp->next) 
      { 
       while (temp->next->next != NULL && temp->next->payload == pload) 
       { 
        node_ll* trash = temp->next; 
        temp->next = temp->next->next; 
        head_return(&trash); 
       } 
      } 
      if (temp->next == NULL && temp->payload == pload) 
      { 
       tail_return(list); 
      } 
     } 
    } 
} 
+0

"작동하지 않는다"는 것을 의미합니까? –

+0

find_remove 함수에 6을 전달하여 목록의 마지막 요소를 찾아 제거하면 find_remove 함수로 작동합니다. 목록 끝에없는 숫자를 전달하면 응용 프로그램이 충돌합니다. –

답변

1

충돌이 루프 논리가 정확하지 때문이다, 또한 당신이 올바르게 연결리스트를 재설정하지 않는 것을 의미 이전 노드를 추적하지 않는,이 간단한 수정은 다음과 같습니다

void find_remove (node_ll** list, int pload) 
{ 
    if (*list != NULL) 
    { 
     node_ll* temp, *prev=NULL;//Declared before loop for use after loop. 
     for (temp = *list; temp != NULL; temp = temp->next) 
     { 
      if (temp->payload == pload) 
      { 
       if(NULL != prev) 
       { 
        prev->next = temp->next ; 
       } 
       int trash = head_return(&temp); 
      } 

      prev=temp ; 
     } 
    } 
} 

솔직히 코드에는 많은 스타일 문제가 있으며 C++ 스타일 코드보다 C 스타일 코드와 훨씬 비슷합니다. Code Review을 확인해보십시오.

업데이트 : 당신의 수정 된 코드에서 변경할 경우

작동 :

for (temp = *list; temp->next != NULL; temp = temp->next) 
        ^^^^^^^^^^^^^^^^^^ 

루프의 상단에 temp->next이 NULL 그래서 temp->next->next을하는 것은 유효 적이없는 경우 당신은 결코 알 수 없다.

+0

코드 검토에 대한 답변과 권장 사항에 감사드립니다. 귀하의 코드를 다시 작성하려고 시도했으며 다른 오류가 발생했습니다.node_ll * temp; 위한 \t \t \t (온도 = *리스트; temp->하는 next-> 다음, 온도 = temp-> 다음) \t \t \t \t { \t \t \t 경우 (temp->하는 next-> 페이 == PLOAD) \t \t \t \t \t { \t \t \t \t node_ll * 쓰레기 = temp-> 다음; \t \t \t \t \t temp-> next = temp-> next-> next; \t \t \t \t \t head_return (& trash); \t \t \t \t} \t \t \t} 주석에 넣어 코드를 읽기가 정말 어렵 @DannyRancher –

+0

, 나는 업데이트를 게시해야하는 경우 업데이트 섹션 질문을 업데이트 권합니다. 잠깐 살펴보면, 일반적으로'temp-> next-> next'는'temp-> next'가 유효한 지 먼저 확인해야하기 때문에 좋은 생각이 아닙니다. –

+0

원래 게시물을 편집했습니다. temp-> next는 루프에서 유효한 것이기 때문에 temp-> next-> next가 null이 아니 었음을 테스트합니다. –

관련 문제