📄 6_6.txt
字号:
#include<stdio.h>
typedef struct node{
int child;
struct node *next;
}Child;
typedef struct pnode{
int parent;
char data;
Child *headptr;
}Pnode;
searchnode(Pnode h[],int n,char ch)
{
int i;
for(i=0;i<n;i++)
if(h[i].data==ch)return i;
return -1;
}
main()
{
int n,i,j,k;char *s;Pnode *h; Child *p;
printf("node number in tree:\n");
do{
scanf("%d",&n);}
while(n<=0);
getchar();
h=(Pnode *)malloc(sizeof(Pnode)*n);
printf("input each node:\n");
gets(s);
for(i=0;i<n;i++)
{
h[i].data=s[i];
h[i].headptr=NULL;
}
printf("input the relation between nodes:\n");
for(i=1;i<=n-1;i++)
{
printf("input format:a->b:\n");
gets(s);
if((j=searchnode(h,n,s[0]))>-1 &&(k=searchnode(h,n,s[3]))>-1){
p=(Child *)malloc(sizeof(Child));
p->child=k;
p->next=h[j].headptr;
h[j].headptr=p;
h[k].parent=j;
}
}
printf("input any node:\n");
scanf("%d",&i);
if(i>=0 && i<=n){
printf("parent node of %c is %c.\n",h[i].data,h[h[i].parent].data);
printf("the subnodes of %c are follows:\n",h[i].data);
p=h[i].headptr;
while(p){
printf("%c ",h[p->child].data);
p=p->next;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -