벡터 및 포인터를 사용하여 내 자신의 링크 된 목록을 구현하려고합니다. 문제는 첫 번째 노드에서 두 번째 노드를 가리킬 수 없다는 것입니다.링크 된 목록의 포인터/벡터
여기 내 코드이고 나는 시도했다 :
struct Node {
Node* previous;
Node* next;
int data;
};
// Initialize: Create Vector size 20 and first node
void LinkedList::init() {
vecList.resize(20, NULL); // Vector of size 20
Node* head = new Node(); // Create head node
head->previous = NULL; // Previous point set to null
head->next = vecList[1]; // Next pointer set to next position
head->data = 0; // Data set at value 0
vecList[0] = head; // Put head node in first position
count = 1; // Increase count by 1
}
// Add Node to array
void LinkedList::push_back(Node* node, int data) {
count += 1;
node = new Node();
node->next = vecList[count + 1];
node->previous = vecList[count - 1];
node->data = data;
vecList[count - 1] = node;
}
데이터가 전달되었으며 의지하여 표시 :
cout << linkedlist.vecList[1]->data << endl;
을하지만이 방법을 시도하는 경우 나 오류가 표시 다음 포인터를 말하는 것은 vecList
처럼 보이는 <Unable to read memory>
cout << linkedlist.vecList[0]->next->data << endl;
당신은 어떻게'LinkedList의 :: push_back'를 호출 ... 당신이 실제로 목록의 장점을 패배 때문에 벡터 또는 배열과 연결리스트를 구현하려고하는 것이 조금 이상하다 ? – ilent2
'LinkedList '란 무엇입니까? 'vecList' 란 무엇입니까? 코드를 어떻게 사용합니까? 어떤 오류가 발생합니까? –
그리고 오류가 빌드 오류가 아닌 경우 디버거에서 한 줄씩 코드를 단계별로 실행하려 했습니까? –