struct Monitor {
int codMonitor;
char* producator;
float diagonala;
int numarPorturi;
};
struct nodls {
Monitor info;
nodls* next;
};
nodls* creareNod(Monitor m) { --create node
nodls* nou = (nodls*)malloc(sizeof(nodls));
nou->info.codMonitor = m.codMonitor;
nou->info.producator = (char*)malloc(sizeof(char)*(strlen(m.producator) + 1));
strcpy(nou->info.producator, m.producator);
nou->info.diagonala = m.diagonala;
nou->info.numarPorturi = m.numarPorturi;
nou->next = nou;
return nou;
}
nodls* inserare(nodls* cap, Monitor m) { -- insert
nodls* nou = creareNod(m);
if (cap == NULL) {
cap = nou;
cap->next = cap;
}
else
{
nodls* temp = cap;
while (temp->next != cap)
temp = temp->next;
temp->next = nou;
nou->next = cap;
}
return cap;
}
void afisareMonitor(Monitor m) { -- display struct
printf("\nMonitorul cu codul %d, producatorul %s, diagonala %f, numarul de porturi %d",
m.codMonitor, m.producator, m.diagonala, m.numarPorturi);
}
void traversare(nodls** cap) { --display function
nodls* temp = *cap;
if (cap == NULL)
printf("\nLista este goala");
while (temp->next != *cap) {
afisareMonitor(temp->info);
temp = temp->next;
}
afisareMonitor(temp->info);
}
void stergereNod(nodls* cap) --delete node function
{
.......
}
void dezalocare(nodls* cap) { free allocate space
............
}
How I can convert using the following code, my binary tree into a simple linked list. This can be done with recursion maybe.
getLeavesList(root) {
if root is NULL: return
if root is leaf_node: add_to_leaves_list(root)
getLeavesList(root -> left)
getLeavesList(root -> right)
}
So, if the root is NULL, this is, if the function received no valid pointer, then return an error message.
If the root is a leaf, this is, if both left and right child nodes are NULL, the you have to add it to the list of leaf nodes.
Then you recursively call the function with the left and right child nodes.