⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 6_8.txt

📁 C语言数据结构知识原代码 C语言数据结构知识原代码C语言数据结构知识原代码
💻 TXT
字号:
#include<stdio.h>
#define maxsize 100
typedef struct node{
  char data;
  struct node *firstchild,*nextsibling;
  }cstree;
 typedef struct q{
   cstree *data[maxsize];
   int front,rear;
 }queue;   
createtree(cstree **t)
 {
  int i,j,k;cstree *p,*temp;
  char ch,*s;
  queue qu;
  qu.front=qu.rear=0;                  /*队列初始化*/
  printf("please input root node:\n");
  ch=getchar();getchar();              /*输入的字符为空格,则为空结点*/
  if(ch==' ')return 1;
  *t=(cstree *)malloc(sizeof(cstree)); 
  (*t)->data=ch;
  (*t)->nextsibling=NULL;
  qu.rear=qu.rear+1;
  qu.data[qu.rear]=*t;
  while(qu.front!=qu.rear){
   qu.front=(qu.front+1)%maxsize;
   temp=qu.data[qu.front];
   printf("input the childs of node %c",temp->data);
   gets(s);i=1;
   if(s[0]!='\0' && s[0]!=' '){
         p=(cstree *)malloc(sizeof(cstree));
         p->data=s[0];
        temp->firstchild=p;
       qu.rear=(qu.rear+1)%maxsize;
       qu.data[qu.rear]=p;
      temp=p;
      }
    else
   if(s[0]==' ')temp->firstchild=NULL;      /*输入的字符为空格,则为空指针*/
   
   while(s[0]!='\0' && s[i]!='\0'){
         p=(cstree *)malloc(sizeof(cstree));
         p->data=s[i];
         p->nextsibling=NULL;
         qu.rear=(qu.rear+1)%maxsize;
         qu.data[qu.rear]=p;
         temp->nextsibling=p;
         temp=p;
         i++;
        }
    }
 }
cstree *searchchild(cstree *t,char ch)
 {
 int i;cstree *p,*temp;
  queue qu;
  qu.front=qu.rear=0;                  /*队列初始化*/
  if(!t)return NULL;
   qu.rear=1;
  qu.data[qu.rear]=t;
  while(qu.front!=qu.rear){
   qu.front=(qu.front+1)%maxsize;
   temp=qu.data[qu.front];
   if(temp->data==ch)return temp;
   if(temp->firstchild){
     qu.rear=(qu.rear+1)%maxsize;
     qu.data[qu.rear]=temp->firstchild;
     temp=temp->firstchild;
     temp=temp->nextsibling;
     while(temp){
     if(temp->data==ch)return temp;
     qu.rear=(qu.rear+1)%maxsize;
     qu.data[qu.rear]=temp;
     temp=temp->nextsibling;
     }/*while*/
   } /*if*/
  } /*while*/
  return NULL;
 }
getchilds(cstree *p)
 {
  cstree *temp;
  if(!p)return 0;
  temp=p->firstchild;
  if(temp){
   printf("%c ",temp->data);
   temp=temp->nextsibling;
   while(temp){
    printf("%c ",temp->data);
    temp=temp->nextsibling;
   }
  }
}
main()
{
 cstree *h,*p;char ch;
 createtree(&h);
 printf("input the data of node:\n");
 scanf("%c",&ch);
 getchar();
 p=searchchild(h,ch);
 getchilds(p);
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -