【发布时间】:2020-08-04 19:24:28
【问题描述】:
我正在为我的大学课程做作业,我正在努力弄清楚为什么我的 push_front() 函数的代码可以正常工作,但我的 push_back() 总是给出:
“抛出异常:读取访问冲突。st 为 nullptr。”
struct NodeDate
{
int day, month, year; //Structure's data
struct NodeDate* nextNode; //Pointer to point to next node
}*start; //Not sure why we do this, but all examples contain it
class LinkedListDate
{
public:
//Prototype of member functions
NodeDate* create_node(NodeDate);
void push_front();
void pop_front();
void remove_front();
void search();
void display();
LinkedListDate();
void push_back();
void remove_back();
void pop_back();
};
LinkedListDate::LinkedListDate()
{
start = NULL; //Our first created pointer will be set to NULL
}
NodeDate* LinkedListDate::create_node(struct NodeDate newDate) //Function that creates a new node and returns said node
{
struct NodeDate* tempNode, *s;
tempNode = new(struct NodeDate); //Reserving memory for our temp node
if (tempNode == NULL) //If the node is empty
{
std::cout << "Memory not allocated " << std::endl;
return 0;
}
else //Otherwise assign parameter node value to temporary node
{
tempNode->day = newDate.day;
tempNode->month = newDate.month;
tempNode->year = newDate.year;
tempNode->nextNode = NULL; //Remember these are all values contained within the date struct
return tempNode; //Return the node
}
}
void LinkedListDate::push_front() //Function that will insert a node at the start of our linkedlist
{
struct NodeDate* tempNode, *st, newNode;
std::cout << "Enter the day: ";
std::cin >> newNode.day;
std::cout << "Enter the month: ";
std::cin >> newNode.month;
std::cout << "Enter the year: ";
std::cin >> newNode.year;
tempNode = create_node(newNode); //Creating a new node for date with the create_node function
if (start == NULL) //Checks if the starting (Head) node is NULL then first node to insert
{
start = tempNode; //Start node points to our temporary node
start->nextNode = NULL; //Assign null to start->next
}
else //Otherwise nodes are already available in the list
{
st = start; //Assign start to st
start = tempNode; //Start points to temporary node
start->nextNode = st; //Start next point to st
}
std::cout << "Element Inserted at beginning" << std::endl;
}
void LinkedListDate::push_back() //Function to insert a new node at the end of the linkedlist
{
struct NodeDate* tempNode, *st, newNode;
std::cout << "Enter the day: ";
std::cin >> newNode.day;
std::cout << "Enter the month: ";
std::cin >> newNode.month;
std::cout << "Enter the year: ";
std::cin >> newNode.year;
tempNode = create_node(newNode); //Creating a new node for date with the create_node function
st = start;
while (st->nextNode != NULL) //Move til we reach end of the list
{
st = st->nextNode; //Move to the next node
}
tempNode->nextNode = NULL; //Assign null to temporary node next
st->nextNode = tempNode; //st next points to temporary node
std::cout << "Element Inserted at last" << std::endl;
}
再次,push_front() 有效,但 push_back() 无效(程序运行,但在输入第一个节点的日期后出现异常。
我一直在尝试很多事情,但我似乎无法弄清楚我到底做错了什么。
【问题讨论】:
-
如果
start为空怎么办?你从不检查。考虑一个minimal reproducible example,这样我们就可以看到你做事情的顺序。你也可以考虑在调试器中跟踪,这样你就可以准确地看到哪里出错了。我还建议将输入处理代码移动到一个函数中,这样您就不会将其复制/粘贴到多个地方。 -
push_back没有push_front所做的“空列表”处理。 -
这里有一些 C 习惯用法:在 C++ 中,如果分配失败,
new不会返回NULL。它会引发异常。因此,检查NULL是没有用的。在现代 C++ 中,你会说nullptr而不是NULL。您不需要每次都使用struct限定类型。只需说tempNode = new NodeDate;。它不仅保留内存(如malloc()),还初始化它(如果你有构造函数)。最后,new不是函数。它是一个运算符。你不需要括号。
标签: c++ class linked-list