Hello.
Well, for creating linked lists, i always use 2 pointers: one for beginning and one for the end of the list. Is this OK? Or am i required to have just a single pointer pointing to the beginning of the list and then traverse everytime i need to insert? Are there any rules of any sort that say this is how a Data Structure needs to be implemented?
Here is how i usually create a linked list:
struct List {
int num;
struct List *nextNode;
};
struct List *head = NULL;
struct List *tail = NULL;
struct List* getNode()
{
struct List *temp = (struct List *)malloc(sizeof(struct List));
return temp;
}
/* Insert into list */
void insertNode(struct List *node, int num)
{
struct List *temp;
temp = getNode();
if (node == NULL) {
temp->num = num;
temp->nextNode = NULL;
head = temp;
tail = temp;
} else {
head->nextNode = temp;
head = temp;
head->nextNode = NULL;
head->num = num;
}
}