我正在尝试使用矢量和指针实现我自己的链接列表。我遇到的问题是我无法让第一个节点指向第二个节点。链接列表/矢量中的指针
这里是我的代码和我已经试过:
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;
但是,如果我尝试这种方式来显示我得到错误说下一个指针是<Unable to read memory>
cout << linkedlist.vecList[0]->next->data << endl;
你是如何调用'链表:: push_back' ? – ilent2 2014-10-11 13:41:57
什么是'LinkedList'?什么是'vecList'?你如何使用代码?你会得到什么错误? – 2014-10-11 13:42:16
如果错误不是编译错误,那么您是否尝试在调试器中逐行执行代码? – 2014-10-11 13:43:17