내 문제는 push_backs()의 사용에 있다고 생각하지만이 문제를 해결하는 간단한 방법은 생각할 수 없습니다. 내가 arguements에 주어진 두 ints에서 노드를 교환해야합니다, 나는 어떤 도움을 주셔서 감사합니다! 나는 대학의 신입생으로 세계에 나가 오늘, 그리고 의견을 공개합니다!Singly Linked List 스왑 함수 (int, int)
void MyList::swap(int i, int j)
{
if (i == j || i > size() || j > size()) return;
Node *temp = head;
delete head; //pretty sure this is what's giving me issues as well
for (unsigned x = 0; x < size(); x++)
{
if (x == i)
{
int y = 0;
for (Node *itt = head; itt; itt = itt->next)
{
if (y == j)
push_back(itt->value);
y++;
}
}
else if (x == j)
{
int y = 0;
for (Node *itt = head; itt; itt = itt->next)
{
if (y == i)
push_back(itt->value);
y++;
}
}
else
{
push_back(temp->value);
}
temp = temp->next;
}
}
은 참조를 위해, 여기에 노드 클래스 내가 당신보다 더 나은 방법이 있다고 생각
using namespace std;
class Node
{
public:
char value;
Node *next;
Node(char value)
:value(value), next(0)
{}
};
예,'head'가 이어질 것입니다 삭제 [* 정의되지 않은 행동 *] (http://en.wikipedia.org/wiki/Undefined_behavior) 때 나중에 head''역 참조. 왜 헤드 노드의 메모리를 해제하고 싶습니까? 특히 나중에 사용하려고 할 때? –
MyList는 "head"로 정의되어 있으므로 전체 목록에 추가 할 수 없습니다. 하하 – Marksman46
[작은 프로그램 디버깅] (http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)에서이 기사를 읽어야합니다. –