I define a linked list in the same way as it is commonly used, i.e. with a data part and a
self referencing pointer. My logic of insertion is as follows:
struct node
{
int data; //or any type.
struct node *nextPtr;
}*start = NULL;
//main
struct *newPtr = (struct node *)malloc(sizeof(struct node *));
scanf("%d", newPtr->data); //or cout
newPtr->nextPtr = NULL;
if(start == NULL)
start = newPtr;
else
{
while(tempPtr->nextPtr != NULL)
{
tempPtr = tempPtr->nextPtr;
}
tempPtr->nextPtr = newPtr;
}
i) Is this logic correct?
ii) a) I possibly get a run-time error, when I insert two nodes (in one system) or three nodes(in another).
b) The nodes are inserted in the correct order, every time I insert a node.
Is the run-time error as a result of this code…???
1 Answer