나는 연결된 목록 예제를 통해 작업하고 있습니다. 나는 특정 요소를 찾기 위해 목록을 탐색하는 찾기 및 제거 기능에 있습니다. 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);
}
}
}
}
"작동하지 않는다"는 것을 의미합니까? –
find_remove 함수에 6을 전달하여 목록의 마지막 요소를 찾아 제거하면 find_remove 함수로 작동합니다. 목록 끝에없는 숫자를 전달하면 응용 프로그램이 충돌합니다. –