我正在尝试在链表的末尾插入一个新节点。但是,当我尝试时,我会在插入点遇到分段错误。我知道首选的方法是“head -> next”样式,但是对于分配,我们一直坚持做它。帮助?
谢谢!
#include <iostream>
using namespace std;
struct NodeType;
typedef NodeType *NodePtr;
struct NodeType
{
int data;
NodePtr next;
};
int main ()
{
NodePtr head;
NodePtr temp;
NodePtr tempTwo;
NodePtr tempThree;
NodePtr tempFour;
head = new NodeType;
(*head).data = 5;
(*head).next = NULL;
temp = new NodeType;
(*temp).data = 8;
(*temp).next = head;
head = temp;
delete temp;
tempTwo = new NodeType;
(*tempTwo).data = 12;
(*tempTwo).next = NULL;
head -> next -> next = tempTwo;
delete tempTwo;
}