This repository aims to collect the most useful tips and tricks when dealing with linked lists.
At the moment the code is italian commented.
When dealing with creation of a list, insering and deleting a node, there are recurring functions patterns:
node *tail=NULL;
while ( /*CONDITION TO STOP NODE CREATION*/ )
{
if (head == NULL)
{
head = malloc(sizeof(node));
tail = head;
head->value = /*VALUE*/ ;
head->nextNode = NULL;
}
else
{
node *temp = malloc(sizeof(node));
tail->nextNode = temp;
tail = temp;
temp->value = /*VALUE*/ ;
temp->nextNode = NULL;
}
//...OTHER INSTRUCTION FOR CONDITION
}
return head;
node *recursive(node *head, /*VARIABLE FOR ITERATION*/ )
{
if ( /*CONDITION TO STOP NODE CREATION*/ )
{
return NULL;
}
if (head == NULL)
{
head = malloc(sizeof(node));
head->value = /*VALUE*/ ;
head->nextNode = recursive(head->nextNode,
/*MODIFIED VARIABLE FOR ITERATION*/);
return head;
}
else
{
node *temp = malloc(sizeof(node));
temp->value = /*VALUE*/;
temp->nextNode = recursive(head->nextNode,
/*MODIFIED VARIABLE FOR ITERATION*/);
return temp;
}
}
node *temp = head;
node *previous = NULL;
node *newHead = head;
while (temp != NULL)
{
if (/*NODE CONDITION TO GET DELETED*/)
{
if (previous != NULL)
{
previous->nextNode = temp->nextNode;
node *toFREE=temp;
temp = temp->nextNode;
free(toFREE);
}
else
{
node *toFREE=temp;
temp = temp->nextNode;
newHead = temp;
free(toFREE);
}
}
else
{
previous = temp;
temp = temp->nextNode;
}
}
return newHead;
node *insertion(node *head)
{
if(head==NULL)
{
return NULL;
}
else
{
if(/*INSERT CONDITION*/)
{
node *toINSERT = malloc(sizeof(node));
toINSERT->value= /*VALUE*/;
toINSERT->nextNode=head->nextNode;
head->nextNode=toINSERT;
}
return insertion(head->nextNode);
}
}
All functions are written in lib.c file. Below are explained.
- description:
Recursively print all linked nodes form a given head pointer. - param:
node *head
: pointer to the first node of the linked list. - return:
void - see:
prints onlynode
type of nodes.
Space complexity | Time complexity |
O ( len( list ) ) where len() is a function returning the node number of the list and list is the object representing the linked list.Max activation record invoked are given by the number of nodes in the linked list. |
O ( len( list ) ) where len() is a function returning the node number of the list and list is the object representing the linked list.The number of iterations is equal to the time complexity of the function, since this number is dependent by the maximum number of records invoked, those values are the same. |
- description:
Recursively print all linked nodes form a given head pointer. - param:
nodeC *head
: pointer to the first node of the linked list. - return:
void - see:
prints onlynodeC
type of nodes.
Space complexity | Time complexity |
O ( len( list ) ) where len() is a function returning the node number of the list and list is the object representing the linked list.Max activation record invoked are given by the number of nodes in the linked list. |
O ( len( list ) ) where len() is a function returning the node number of the list and list is the object representing the linked list.The number of iterations is equal to the time complexity of the function, since this number is dependent by the maximum number of records invoked, those values are the same. |
Execution:
see viewlist.
- description:
Sequentially create a list with random value between 0 and GENMAX. - param:
node *head
: pointer to the first node of the linked list.
int pos
: number of nodes to generate. - return:
node *
- see:
-
Space complexity | Time complexity |
O ( 1 ) The fuction is called once. |
O ( pos ) pos + 1 is the maximum number of iterations (due to number of comparison in the while loop), with high values the +1 term will not affect the curve described by the function, so its negligible. |
- description:
Recursively create a list with random value between 0 and GENMAX. - param:
node *head
: pointer to the first node of the linked list.
int pos
: number of nodes to generate. - return:
node *
- see:
-
Space complexity | Time complexity |
O ( pos ) Max activation record invoked are given by the number of nodes in the linked list. |
O ( pos ) pos + 1 is the maximum number of comparison made, at least once when the function is called. At high values of pos, +1 will be negligible. |
- description:
Sequentially create a list using value inserted by user. - param:
node *head
: pointer to the first node of the linked list. - return:
node *
- see:
-
Space complexity | Time complexity |
O ( 1 ) The fuction is called once. |
O ( mod(val) ) where mod() is a function that returns the number of modification to the value in input. Actually it would be O(mod(val-1)), beacause the first assignment is indipendent by the while loop.
|
Execution:
See createLISTseq
- description:
Recursively create a list using value inserted by user. - param:
node *head
: pointer to the first node of the linked list. - return:
node *
- see:
-
Space complexity | Time complexity |
O ( mod( val ) ) where mod() is a function that returns the number of modification to the value in input.
Activation records are invoked till val value is 0.
|
O ( mod( val ) ) where mod() is a function that returns the number of modification to the value in input.
This number is dependent by the maximum number of records invoked which is dependent by user input.
|
Execution:
See createLISTrec
- description:
Sequentially create a list by characters in a file. - param:
nodeC *head
: pointer to the first node of the linked list.
char path[]
: pointer to the char array containing the path of the file. - return:
nodeC *
- see:
-
Space complexity | Time complexity |
O ( 1 ) The fuction is called once. |
O ( charnumber(fp) ) where charnumber() is a function that returns the number of ASCII characters in the file pointer fp .
|
Execution:
See createLISTseq
- description:
Recursively create a list by characters in a file. - param:
nodeC *head
: pointer to the first node of the linked list.
FILE *fp
: pointer to the file stream. - return:
nodeC *
- see:
-
Space complexity | Time complexity |
O ( charnumber(fp) ) where charnumber() is a function that returns the number of ASCII characters in the file pointer fp .
Max activation record invoked are given by the final number of nodes in the linked list +1.
|
O ( charnumber(fp) ) where charnumber() is a function that returns the number of ASCII characters in the file pointer fp .
|
Execution:
See createLISTrec
- description:
Recursively count number of nodes containingval
in a given linked list. - param:
node *head
: pointer to the first node of the linked list.
int val
: value to find. - return:
int
- see:
-
Space complexity | Time complexity |
O ( len( list ) ) where len() is a function returning the node number of the list and list is the object representing the linked list.Max activation record invoked are given by the number of nodes in the linked list. |
O ( len( list ) ) where len() is a function returning the node number of the list and list is the object representing the linked list. |
- description:
Remove all nodes containing the valueval
from the linked list pointed byhead
- param:
node *head
: pointer to the first node of the linked list.
int val
: value to delete. - return:
node *
- see:
-
Space complexity | Time complexity |
O ( 1 ) The fuction is called once. |
O ( len( list )) where len() is a function returning the node number of the list and list is the object representing the linked list. |
- description:
Recursively count number of nodes containingc
character in a given linked list. - param:
nodeC *head
: pointer to the first node of the linked list.
char c
: character to find - return:
int
- see:
-
Space complexity | Time complexity |
O ( len( list ) ) where len() is a function returning the node number of the list and list is the object representing the linked list.Max activation record invoked are given by the number of nodes in the linked list. |
O ( len( list ) ) where len() is a function returning the node number of the list and list is the object representing the linked list. |
Execution:
See findval
- description:
Recursively transform allc
characters into capital letters in a given linked list. - param:
nodeC *head
: pointer to the first node of the linked list.
char c
: character to up. - return:
nodeC *
- see:
-
Space complexity | Time complexity |
O ( len( list ) ) where len() is a function returning the node number of the list and list is the object representing the linked list.Max activation record invoked are given by the number of nodes in the linked list. |
[Time complexity] |
Execution:
See findval