Add link description
According to the above blog, I tested this program. There are two points to explain. First, the blogger said to use c language, and I compiled with. c file for the first time, and error: expected ';', ',' or ')' before '&' token appears
This is a concept that uses reference &, but there is no reference in C language. In C language, address symbol is used. Later, it can be run by changing to. cpp file
The second point is: fflush(stdin)
fflush(stdin) is a computer technical term. Its function is to clear the input buffer. It is usually used to ensure that subsequent data reading is not affected (for example, after reading a string, another character is to be read immediately. In this case, fflush(stdin) should be executed first). Because I need to read some parameters from the input buffer, use this function to clear the buffer to prevent the last input from affecting the next result.
Here is the code that can be run. Make some comments accordingly
#include<stdio.h> #include<stdlib.h> #define MAXNODE 20 typedef char ElemType; typedef struct SCNode{//Child node int childnode; struct SCNode *nextchild; }CNode; typedef struct { ElemType data; CNode *firstchild; int r;//r serial number pointing to parents }CTBox; typedef struct{ CTBox tree[MAXNODE];//Number of nodes int n; }CTree; void InitCtree(CTree &t){ //Initialization tree int i; printf("Please enter the number of nodes in the tree:\n"); scanf("%d\n",&t.n); printf("Input each node in turn"); for(i=0;i<t.n;i++){ fflush(stdin); t.tree[i].data=getchar(); t.tree[i].r=0; t.tree[i].firstchild=NULL; } } void AddChild(CTree &t){ //Add children int i,j,k; printf("Add children:\n"); for(k=0;k<t.n-1;k++){ fflush(stdin); printf("Please enter the serial number of each child node and parent node"); scanf("%d%d",&i,&j); fflush(stdin); CNode *p=(CNode *)malloc(sizeof(CNode)); p->childnode=i; p->nextchild=NULL; t.tree[i].r=j;//Find parents //If parents don't have children, the added child is the eldest child if(!t.tree[j].firstchild) t.tree[j].firstchild=p; else{ //If parents have children, the added children should be next child in time CNode *temp=t.tree[j].firstchild; while(temp->nextchild)//If there is more than one child, then recursion makes p written as the last child temp=temp->nextchild; temp->nextchild=p; } } } void FindChild(CTree &t){ //Find child node int i,n; printf("\n Please enter the serial number of the node to query\n"); scanf("%d",&n); if(!t.tree[n].firstchild)//There are no children at this node printf("node%c Childless node\n",t.tree[n].data); else{ CNode *p=t.tree[n].firstchild; printf("%c The node's child number is:\n",t.tree[i].data); while(p){//This is the case where there is more than one child at the node printf("%d",p->childnode); p=p->nextchild; } } } void FindParent(CTree &t){ //Find parent node int i,n; printf("Please enter the serial number of the node to query\n"); scanf("%d",&n); if(!n) printf("node%c No parent node\n",t.tree[n].data); else printf("node%c Parent node of%c\n",t.tree[n].data,t.tree[t.tree[n].r].data);//The last parameter is the data of parents } int main(){ CTree t; InitCtree(t); AddChild(t); FindChild(t); return 0; }