I have an algorithm that should create a singly linked list from a textbook. It barely touched on any examples, so I would need some help figuring it out (still new to C.)
Essentially, the algorithm runs as follows:
Algorithm: CREATE (HEAD, ITEM)
1. [Create NEW node]
a) Allocate memory for NEW node.
b) IF NEW = NULL then Print: “Memory not Available” and Return
c) Set NEW→DATA = ITEM
d) Set NEW→LINK = NULL
2. [Whether List is empty, head is the content of HEADER]
If HEAD = NULL then Set HEAD = NEW
3. Else
a) Set Temp = HEAD
b) While Temp→LINK ≠ NULL do
Set Temp = Temp→LINK
[End of while]
c) Set Temp→LINK = NEW
[End of IF]
4. Return
Here is what I have tried so far, but I could not understand the arrow mappings in the algorithm. Are these existing C features?
#include <stdlib.h>
#include <stdio.h>
typedef struct node {
int DATA;
struct node * LINK;
} node_t;
node_t *create(int head, int item){
node_t* new = NULL;
new = (node_t *)malloc(5*sizeof(node_t));
if(new == NULL){
prtinf("Memory not available");
return -1;
}
}