I'm trying to insert a new node at the end of a linked list. However, when I try, I get a segmentation fault at what would be the point of insertion. I know that the preferred method is the 'head -> next' style, but for the assignment, we're stuck doing it long hand. Help?
Thanks!
#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;
}