【发布时间】:2016-09-02 00:26:38
【问题描述】:
我正在尝试使用单独的 Node 类和 LinkedList 类创建一个基本的单链表。由于我刚开始学习 C++,所以我几乎不知道自己在做什么,因此非常感谢任何帮助。
代码的 LinkedList 部分自行运行,但我确信那里也需要进行一些更正。我的主要问题是,当尝试添加到链接列表时,我得到(在 LinkedList.h 的第 64 行):
抛出异常:读取访问冲突。 this->head 是 nullptr。
我使用的是 Microsoft Visual Studio 2015。代码如下:
LinkedList.h(它是内联的):
#pragma once
#include <iostream>
using namespace std;
class Node
{
private:
Node *next = NULL;
int data;
public:
Node(int newData) {
data = newData;
next = NULL;
}
Node() {
}
~Node() {
if(next)
delete(next);
}
Node(int newData, Node newNext) {
data = newData;
*next = newNext;
}
void setNext(Node newNext) {
*next = newNext;
}
Node getNext() {
return *next;
}
int getData() {
return data;
}
};
class LinkedList
{
private:
Node *head;
int size;
public:
LinkedList()
{
head = NULL;
size = 0;
}
~LinkedList()
{
}
void add(int numberToAdd)
{
head = new Node(numberToAdd, *head);
++size;
}
int remove()
{
if (size == 0) {
return 0;
}
else {
*head = (*head).getNext();
--size;
return 1;
}
}
int remove(int numberToRemove)
{
if (size == 0)
return 0;
Node *currentNode = head;
for (int i = 0; i < size; i++) {
if ((*currentNode).getData() == numberToRemove) {
*currentNode = (*currentNode).getNext();
return 1;
}
}
}
void print()
{
if (size == 0) {
return;
}
else {
Node currentNode = *head;
for (int i = 0; i < size; i++) {
cout << currentNode.getData();
currentNode = currentNode.getNext();
}
cout << endl;
}
}
};
列出Tester.cpp
// List Tester.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "LinkedList.h"
using namespace std;
int main()
{
LinkedList myList;
myList.add(4);
system("pause");
}
【问题讨论】:
-
建议你尽可能使用标准库容器,在这种情况下使用 std::forward_list 见:en.cppreference.com/w/cpp/container/forward_list
-
请注意
std::forward_list是 C++11 中的新内容(我想这段代码正在使用它)。在早期版本中,只有std::list(这是一个双链表)。
标签: c++ linked-list visual-studio-2015