数据结构---串实现 13-15函数未实现

#include <stdio.h>
#include <stdlib.h>
/*定义单链表结构体*/
#define MAXSTRLEN 255 
#define TRUE 1
#define FALSE 0

struct sstring
{
 char *ch;
   int length ;
};
void StrAssign (struct sstring *T,char x)//初始条件:chars 是串常量,操作结果:赋于串T的值为 chars。
{
 if(T->ch==NULL)
 {
  T->ch = malloc(MAXSTRLEN*sizeof(char)+1);
  T->length = 1;
  T->ch[T->length-1] = x;
 }
 else
 {
  if(T->length == MAXSTRLEN+1)
  {
    printf("The Lord string is full, please quit!\n");
    exit(1);
  }
  else
  {
    T->length++;
    T->ch[T->length-1] = x;
  }
 }

 return ;
}
void StrCopy (struct sstring *T, char s[])//初始条件:串 s 存在,操作结果:由串 s 复制得串 T。
{
 int sum = T->length,end = 0,i = 0;
 while(*s!='\0')
 {
  T->ch[i] = *s;
  printf("%c",T->ch[i]);
  i++;
  s++;
  end++;
 }
 T->length = end;
 for(i = 0;i<T->length;i++)
 {
  T->ch[end+i] = '\0';
 }
 return ;
}
void DestroyString (struct sstring *T)//初始条件:串 T 存在,操作结果:串 T 被销毁。
{
 if(T->ch!=NULL)
 {
  free(T->ch);
  T->ch = NULL;
  T->length = 0;
  return ;
 }
 else
 {
  printf("The string as empty strings, quit!\n");
        return ;
 }
}
int StrEmpty (struct sstring *T)  //初始条件:串 T 存在,操作结果:若 T 为空串,则返回 true,否则返回 false。
{
 if(*(T->ch)!='\0')
 {
  return FALSE;
 }
 else
 {
  return TRUE;
 }
}
int StrCompare (struct sstring *T, char s[]) //初始条件:串 T 和 S 存在,操作结果:若T>S,则返回值>0;若T=S,则返回值=0;若T<S,则返回值<0。
{
   int sum = 0;
   while(*s!='\0')
   {
    s++;
    sum++;
   }
   if(T->length > sum)
   {
    return 1;
   }
   else if(T->length = sum)
   {
    return 0;
   }
   else
   {
    return -1;
   }

}
int StrLength (struct sstring *T)//返回主串T元素
{
 if(T->length>=1)
 {
  return T->length;
 }
 else
 {
  return 0;
 }
}
void ClearString (struct sstring *T) //初始条件:串 S 存在,操作结果:将 S 清为空串。
{
 if(T->ch!=NULL)
 {
  free(T->ch);
  T->ch = NULL;
  T->length = 0;
 }
 T->ch = malloc(MAXSTRLEN*sizeof(char)+1);
 return ;
}
void Concat (struct sstring *T,char s1[], char s2[]) //初始条件:串 S1 和 S2 存在,操作结果:用 T 返回由 S1 和 S2 联接而成的新串。
{
 int s1sum = 0,s2sum = 0;
 int s1i = 0,s2i = 0;
 T->length = 0;
 if(*s1!='\0'&&*s2!='\0')
 {
   while(s1[s1sum]!='\0')
   {
    s1sum++;
   }
   s1sum++;
      while(s2[s2sum]!='\0')
   {
    s2sum++;
   }
   s2sum++;
   if((s1sum+s2sum)>MAXSTRLEN)
   {
    if(s1sum>s2sum)
    {
     while(s1[s1i]!='\0')
     {
       T->ch[s1i]=s1[s1i];
    T->length++;
     }
     while(T->length!=MAXSTRLEN)
     {
      T->ch[s1i]=s2[s2i];
      s1i++;
      s2i++;
      T->length++;
     }
    }
    else if(s1sum<s2sum)
    {
     while(s1[s1i]!='\0')
     {
       T->ch[s1i]=s1[s1i];
    T->length++;
     }
     while(T->length!=MAXSTRLEN)
     {
      T->ch[s1i]=s2[s2i];
      s1i++;
      s2i++;
      T->length++;
     }

    }
    else
    {
       while(s1[s1i]!='\0')
     {
       T->ch[s1i]=s1[s1i];
    T->length++;
    s1i++;
     }
     while(T->length!=MAXSTRLEN)
     {
      T->ch[s1i]=s2[s2i];
      s1i++;
      s2i++;
      T->length++;
     }

    }
   }
   else
   {
       while(s1[s1i]!='\0')
     {
       T->ch[s1i]=s1[s1i];
    T->length++;
    s1i++;
     }
     while(s2[s2i]!='\0')
     {
      T->ch[s1i]=s2[s2i];
      s1i++;
      s2i++;
      T->length++;
     }

   }
      return ;
 }
 else
 {
  printf("The string as empty strings, quit!\n");
        return ;
 }
}
char* SubString (struct sstring T,char sum[],int pos,int len)//初始条件:串T存在,1≤pos≤StrLength(S)且0≤len≤StrLength(S)-pos+1,操作结果:用 Sub 返回串T的第 pos 个字符起长度为 len 的子串。
{
 int com = 0;
 if(*T.ch!='\0')
 {
  if(pos>=1&&pos<=T.length)
  {
   if(len>=0&&len<=T.length-pos+1)
   {
    while(com!=len)
    {
      *sum++ = T.ch[pos-1+com];  
      com ++;
 
    }
    return sum;
   }
   else
   {
    printf("please enter len is 0---%d\n",(T.length-pos+1));
          return NULL;
   }
  }
  else
  {
   printf("please enter pos is 0---%d\n",T.length);
   return NULL;
  }
 }
 else
 {
  printf("The string as empty strings, quit!\n");
        return NULL;
 }

}
int Index (struct sstring T,char s[]) //初始条件:串T和S存在,S 是非空串,1≤pos≤StrLength(S),操作结果:若主串T中存在和串S值相同的子串,则返回它在主串T中第pos个字符之后第一次出现的位置;否则函数值为0。
{
 int sum = 0,pos = 1,i = 0,j=0,end = 1;
 if(T.ch!=NULL&&*s!='\0')
 {
  while(s[i]!='\0')
  {
   i++;
   sum ++;
  }
  i = 1;
  while((T.length-pos+1)>=sum)
  {
    while(T.ch[pos-1]!=s[j])
    {
    end = pos++;
    if(T.ch[pos]=='\0')
    {
     return 0;
    }
    }
    if((T.length-pos+1)>=sum)
    {
     if(i==sum)
    {
       return pos;
    }
     while(T.ch[pos+i-1]==s[i])
     {
      if(i=sum)
      {
       return pos;
      }
      i++;
      pos++;
     }
     
    }
    i = 1;
    end++;
    pos = end;
       
  }
      return 0;
 }
 else
 {
  printf("The initial conditions are not met, quit\n");
  exit(1);
 }
 return -1;
}
void Replace (struct sstring *T,char S[], char V[])//初始条件:串 T,S 和 V 存在,S 是非空串,操作结果:用V替换主串T中出现的所有与S相等的不重叠的子串。
{
 int sum = 0,and = 0,end = 0;
 int h1 = 0,h2 = 0;
 if(!StrEmpty(T)&&*S!='\0'&&*V!='\0')
 {
  
  while(S[and]!='\0')
  {
   and++;
  }
  while(V[end]!='\0')
  {
   end++;
  }
        sum = Index(*T,S);
        while(sum!=0)
  {
   if(and>end)
   {
    while(h2!=end)
    {
      T->ch[sum+h1-1] = V[h2];
      h1++;
      h2++;
    }
    T->ch[sum+h2-(and-end)]=T->ch[sum+and-2];

   }
   else if(and<end)
   {
    while(h2!=and)
    {
      T->ch[sum+h1-1] = V[h2];
      h1++;
      h2++;
    }

   }
   else
   {
    while(h2!=and)
    {
      T->ch[sum+h1-1] = V[h2];
      h1++;
      h2++;
    }

   }
   sum = Index(*T,S);
  }
 
 }
    return ;
}
void StrInsert (struct sstring *T,int pos, char S[]) //初始条件:串 T 和 S 存在,1≤pos≤StrLength(S)+1,操作结果:在串 T 的第 pos 个字符之前插入串 S。
{
  int and =1,T1 = 0,S1 = 0;
     if(!StrEmpty(T)&&*S!='\0')
  {
   if(pos>=1&&pos<=(T->length+1))
   {
    while(S[and-1]!='\0')
    {
        and++;
    }
    if(and>pos)
    {
     if(and+T->length<=MAXSTRLEN-1)
     {
       while(T1!=T->length)
       {
            T->ch[pos+T1] = T->ch[T1];
      T1++;
       }
       while(S1!=pos)
       {
        T->ch[S1] = S[S1];
        S1++;
       }
     }
     else
     {
      printf("Situation go wrong!\n");
      exit(1);
     }
    }
    else if(and<pos)
    {
      if(and+T->length<=MAXSTRLEN-1)
     {
       while(T1!=T->length)
       {
            T->ch[and+T1] = T->ch[T1];
      T1++;
       }
       while(S1!=and)
       {
        T->ch[S1] = S[S1];
        S1++;
       }
     }
     else
     {
      printf("Situation go wrong!\n");
      exit(1);
     }

    }
    else
    {
      if(and+T->length<=MAXSTRLEN-1)
     {
       while(T1!=T->length)
       {
            T->ch[and+T1] = T->ch[T1];
      T1++;
       }
       while(S1!=and)
       {
        T->ch[S1] = S[S1];
        S1++;
       }
     }
     else
     {
      printf("Situation go wrong!\n");
      exit(1);
     }

    }
    
   }
   else
   {
         printf("The initial conditions are not met, quit\n");
      exit(1);
   }
  }
    else
 {
    printf("The initial conditions are not met, quit\n");
    exit(1);
 }
}
void StrDelete (struct sstring *T,int pos, int len)//初始条件:串T 存在,1≤pos≤StrLength(S)-len+1,操作结果:从串 T中删除第 pos 个字符起长度为 len 的子串。
{
 int i = 0,j = 0;
 if(pos>=1&&pos<=T->length-len+1)
 {
  while(i!=len)
  {
            T->ch[pos-1+i] = '\0';
  }
        while((pos+len+j)!=T->length)
  {
   T->ch[pos-1+j] = T->ch[pos+len+j-1];
   j++;
  }
 }
  else
 {
    printf("The initial conditions are not met, quit\n");
    exit(1);
 }

}
void main()
{
 char s[MAXSTRLEN]="";
 char v[MAXSTRLEN]="";
 char secondary[MAXSTRLEN]="";
 char transition ;
 struct sstring T;
 int  choice = 0,length = 0,i = 0,pos = 0 ,len = 0;
 T.ch = NULL;
 T.length = 0;
    while(1)
 {
  printf("-----------------串算法菜单-----------------\n");
  printf("1->构造主串T(动态在末尾添加,如需情况请选4)\n");
  printf("2->构造子串S(长度以最大的为准,如需清空请选4)\n");
  printf("3->构造子串V(长度以最大的为准,如需清空请选4)\n");
  printf("4->清空串选择\n");
  printf("5->遍历串选择\n");
  printf("6->返回串长度选择\n");
  printf("7->判空串选择\n");
  printf("8->复制子串S到主串T\n");
  printf("9->连结子串S和子串V到主串T\n");
  printf("10->返回主串T第pos位置len长度字符数组\n");
  printf("11->比较子串S或子串V与主串T大小\n");
  printf("12->定位子串S或子串V与主串T中第一次相等位置\n");
  printf("13->用子串S或V替换与主串T相等子串V或S所有不重叠的地方\n");
  printf("14->在主串T第pos位置之前插入子串S\n");
  printf("15->删除主串T第pos位置后len长度的字符\n");
  printf("-----------------串算法菜单-----------------\n");
  printf("请选择操作:");
  fflush(stdin);
  scanf("%d",&choice);
  switch(choice)
  {
      case 1:
    printf("请输入构造主串T长度:");
    scanf("%d",&length);
    printf("\n");
    printf("请输入主串T元素:");
    fflush(stdin);
                for(i=0;i<length;i++)
    {
      scanf("%c",&transition);
      StrAssign(&T,transition);
                  
    }
    printf("主串字符录入成功!\n");
    length = 0;
    break;
   case 2:
    printf("请输入构造子串S长度:");
    scanf("%d",&length);
    printf("\n");
    printf("请输入子串S元素:");
    fflush(stdin);
                for(i=0;i<length;i++)
    {
      scanf("%c",&transition);
      s[i] = transition;
                  
    }
    printf("子串字符录入成功!\n");
    length = 0;
    break;
            case 3:
    printf("请输入构造子串V长度:");
    scanf("%d",&length);
    printf("\n");
    printf("请输入子串V元素:");
    fflush(stdin);
                for(i=0;i<length;i++)
    {
      scanf("%c",&transition);
      v[i] = transition;
                  
    }
    printf("子串字符录入成功!\n");
    length = 0;
    break;
   case 4:
    printf("请选择清空字符串代号T->主串,S->子串,V->子串,其他->清空所有子串:");
    fflush(stdin);
    scanf("%c",&transition);
    switch(transition)
    {
       case 'T':case 't':
         ClearString(&T);
          if(!StrEmpty(&T))
       {
       printf("主串T成功清空!");
       getchar();
       getchar();
       }
          else
       {
       printf("主串T清空失败!");
       getchar();
       getchar();
       }
          break;
     case 'S':case 's':
          choice = 0;
         while(s[choice]!='\0')
         {
         s[choice] = '\0';
         choice++;
         } 
                           printf("子串S成功清空!");
         getchar();
         getchar();
         break;
     case 'V':case 'v':
      choice = 0;
      while(v[choice]!='\0')
      {
         v[choice] = '\0';
         choice++;
       }
                        printf("子串v成功清空!");
      getchar();
      getchar();
      break;
     default :
         ClearString(&T);
          if(!StrEmpty(&T))
       {
       printf("主串T成功清空!\n");
       }
          else
       {
       printf("主串T清空失败!\n");
       }
         choice = 0;
         while(s[choice]!='\0')
         {
         s[choice] = '\0';
         choice++;
         } 
                           printf("子串S成功清空!\n");
          choice = 0;
         while(v[choice]!='\0')
         {
         v[choice] = '\0';
         choice++;
         }
                           printf("子串v成功清空!\n");
         getchar();
         getchar();
         break;
    }
    break;
   case 5:
    printf("请选择遍历字符串代号T->主串,S->子串,V->子串,其他->遍历所有子串:\n");
    fflush(stdin);
    scanf("%c",&transition);
       switch(transition)
    {
       case 'T':case 't':
      choice = 0;
  
       printf("遍历主串T:");
       while(choice!=T.length)
         {
       printf("%c",T.ch[choice]); 
       choice++;
         }
        getchar();
        getchar();
           break;
  
     case 'S':case 's':
      choice = 0;
      printf("遍历子串S:");
      while(s[choice]!='\0')
      {
            printf("%c",s[choice]); 
       choice++;
      } 
      getchar();
         getchar();
      break;
     case 'V':case 'v':
      choice = 0;
      printf("遍历子串V:");
      while(v[choice]!='\0')
      {
          printf("%c",v[choice]); 
       choice++;
      } 
      getchar();
         getchar();
      break;
    default :
       choice = 0; 
       printf("遍历主串T:");
       while(choice!=T.length)
         {
       printf("%c",T.ch[choice]); 
       choice++;
         }
    
      printf("\n");
        choice = 0;
      printf("遍历子串S:");
      while(s[choice]!='\0')
      {
          printf("%c",s[choice]); 
       choice++;
      } 
      printf("\n");
      choice = 0;
      printf("遍历子串V:");
      while(v[choice]!='\0')
      {
          printf("%c",v[choice]); 
       choice++;
      } 
        getchar();
        getchar();
        break;
    }
    break;
    
   case 6:
    printf("请选择返回字符串长度代号T->主串,S->子串,V->子串,其他->返回所有子串长度:\n");
    fflush(stdin);
    scanf("%c",&transition);
       switch(transition)
    {
     case 'T':case 't':
           printf("主串T长度为:%d",T.length);
        getchar();
        getchar();
           break;
     case 'S':case 's':
      choice = 0;
      while(s[choice]!='\0')
      {
       choice++;
      } 
      printf("子串S长度为:%d",choice);
      getchar();
         getchar();
      break;
     case 'V':case 'v':
      choice = 0;
      while(v[choice]!='\0')
      {
       choice++;
      } 
      printf("子串V长度为:%d",choice);
      getchar();
         getchar();
      break;
    default :
      printf("主串T长度为:%d",T.length);
      printf("\n");
       choice = 0;
      while(s[choice]!='\0')
      {
       choice++;
      } 
      printf("子串S长度为:%d",choice);
      printf("\n");
      choice = 0;
      while(v[choice]!='\0')
      {
       choice++;
      } 
      printf("子串V长度为:%d",choice);
      getchar();
         getchar();
      break;
    }
    break;
   case 7:
     printf("请选择判空字符串长度代号T->主串,S->子串,V->子串,其他->判空所有子串:\n");
    fflush(stdin);
    scanf("%c",&transition);
       switch(transition)
    {
     case 'T':case 't':
        if(T.length!=0)
        {
               printf("主串T非空!");
         getchar();
         getchar();
        }
        else
        {
                         printf("主串T为空!");
        }
           break;
     case 'S':case 's':
      choice = 0;
      while(s[choice]!='\0')
      {
       choice++;
      } 
       if(choice!=0)
        {
               printf("子串S非空!");
         getchar();
         getchar();
        }
        else
        {
                         printf("子串S为空!");
        }
           break;
     case 'V':case 'v':
       choice = 0;
      while(v[choice]!='\0')
      {
       choice++;
      } 
       if(choice!=0)
        {
               printf("子串V非空!");
         getchar();
         getchar();
        }
        else
        {
                         printf("子串V为空!");
        }
           break;
    default :
        if(T.length!=0)
        {
               printf("主串T非空!");
        }
        else
        {
                         printf("主串T为空!");
        }
        printf("\n");
      choice = 0;
      while(s[choice]!='\0')
      {
       choice++;
      } 
       if(choice!=0)
        {
               printf("子串S非空!");
        }
        else
        {
                         printf("子串S为空!");
        }
        printf("\n");
       choice = 0;
      while(v[choice]!='\0')
      {
       choice++;
      } 
       if(choice!=0)
        {
               printf("子串V非空!");
        
        }
        else
        {
                         printf("子串V为空!");
        }
        getchar();
        getchar();
           break;
    }
    break;
   case 8:
    printf("请选择返回字符串代号替换主串T,S->子串,V->子串,其他->错误:\n");
    fflush(stdin);
    scanf("%c",&transition);
                switch(transition)
    {
     case 'S':case 's':
      StrCopy(&T,s);
                     printf("S子串成功赋值给主串T!\n");
      getchar();
      getchar();
      break;
     case 'V':case 'v':
         StrCopy(&T,v);
                     printf("V子串成功赋值给主串T!\n");
      getchar();
      getchar();
      break;
    default :
     printf("选择错误!\n");
     break;
    
    }
    break;
   case 9:
    printf("请选择连结顺序代号替换主串T,S->S子串+V子串,V->V子串+S子串,其他->错误:\n");
    fflush(stdin);
    scanf("%c",&transition);
                switch(transition)
    {
     case 'S':case 's':
      Concat(&T,s,v);
                     printf("S子串+V子串成功赋值给主串T!\n");
      getchar();
      getchar();
      break;
     case 'V':case 'v':
         Concat(&T,v,s);
                     printf("V子串+S子串成功赋值给主串T!\n");
      getchar();
      getchar();
      break;
    default :
     printf("选择错误!\n");
     break;
    
    }
    break;
   case 10:
    choice = 0;
    printf("请输入主串T第pos位置:");
    scanf("%d",&pos);
    printf("请输入主串T第pos位置第len长度:");
    scanf("%d",&len);
    SubString(T,secondary,pos,len);
                printf("遍历主串T第%d处%dlen长度数组:",pos,len);
    while(secondary[choice]!='\0')
    {
     printf("%c",secondary[choice]);
     secondary[choice] = '\0';
     choice++;
    }
    getchar();
    getchar();
    break;
   case 11:
    printf("请选择与主串T比较字符串代号,S->子串,V->子串,其他->错误:\n");
    fflush(stdin);
    scanf("%c",&transition);
                switch(transition)
    {
     case 'S':case 's':
      if(StrCompare(&T,s)>0)
      {
                       printf("主串T大于子串S!\n");
        getchar();
        getchar();
        break;
      }
      else if(StrCompare(&T,s)<0)
      {
                       printf("主串T小于子串S!\n");
        getchar();
        getchar();
        break;
      }
      else 
      {
                       printf("主串T等于子串S!\n");
        getchar();
        getchar();
        break;
      }
     case 'V':case 'v':
         
      if(StrCompare(&T,v)>0)
      {
                       printf("主串T大于子串V!\n");
        getchar();
        getchar();
        break;
      }
      else if(StrCompare(&T,v)<0)
      {
                       printf("主串T小于子串V!\n");
        getchar();
        getchar();
        break;
      }
      else 
      {
                       printf("主串T等于子串V!\n");
        getchar();
        getchar();
        break;
      }
    default :
     printf("选择错误!\n");
     break;
    
    }
    break;
   case 12:
    printf("请选择比较字符代号与主串T进行定位操作,S->S子串,V->V子串,其他->错误:\n");
    fflush(stdin);
    scanf("%c",&transition);
                switch(transition)
    {
     case 'S':case 's':
      
                     printf("S子串与主串T第一次相等的位置为:%d",Index(T,s));
      getchar();
      getchar();
      break;
     case 'V':case 'v':
         printf("V子串与主串T第一次相等的位置为:%d",Index(T,v));
      getchar();
      getchar();
      break;
    default :
     printf("选择错误!\n");
     break;
    
    }
    break;
   case 13:
    printf("请选择替换字符代号与主串T进行覆盖操作,S->S覆盖V与T相等的字符,V->V覆盖S与T相等的字符,其他->错误:\n");
    fflush(stdin);
    scanf("%c",&transition);
                switch(transition)
    {
     case 'S':case 's':
      Replace(&T,v,s);
                     printf("S覆盖V与T相等的字符成功!");
      getchar();
      getchar();
      break;
     case 'V':case 'v':
      Replace(&T,s,v);
         printf("V覆盖S与T相等的字符成功!");
      getchar();
      getchar();
      break;
    default :
     printf("选择错误!\n");
     break;
    
    }
    break;
   case 14:
    printf("请输入插入主串第pos位置:");
    scanf("%d",pos);
    printf("请选择字符代号与主串T进行插入操作,S->S子串,V->V子串,其他->错误:\n");
    fflush(stdin);
    scanf("%c",&transition);
                switch(transition)
    {
     case 'S':case 's':
      StrInsert(&T,pos,s);
                     printf("S插入T成功!");
      getchar();
      getchar();
      break;
     case 'V':case 'v':
      StrInsert(&T,pos,s);
         printf("V插入T成功!");
      getchar();
      getchar();
      break;
    default :
     printf("选择错误!\n");
     break;
    
    }
    break;
   /*case 15:
   default :*/
   
  }

 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据结构与算法的代码实现涉及到很多不同的问题和挑战。以下是其中一些常见的实现示例: 1. 数组:实现一个支持动态扩容的数组,可以使用动态分配内存的方式来实现数组的动态扩容。当数组空间不足时,可以创建一个更大的数组,将原有的数据复制到新数组中。 2. 链表:可以实现单链表、循环链表和双向链表。单链表包含一个指向下一个节点的指针,循环链表的尾节点指向头节点,双向链表每个节点有一个指向前一个节点和后一个节点的指针。 3. 栈:可以使用数组或链表来实现顺序栈和链式栈。顺序栈使用数组来保存数据,链式栈使用链表来保存数据。在模拟浏览器的前进、后退功能时,可以使用两个栈来实现。 4. 队列:可以使用数组或链表来实现顺序队列和链式队列。顺序队列使用数组来保存数据,链式队列使用链表来保存数据。循环队列则在顺序队列的基础上增加了循环利用空间的功能。 5. 递归:递归是一种函数自己调用自己的方法。可以使用递归来实现斐波那契数列的求值、阶乘的计算以及一组数据集合的全排列等。 6. 排序:可以实现归并排序、快速排序、插入排序、冒泡排序和选择排序等。这些排序算法的实现方式各不相同,但都能实现对一组数据的排序。 7. 二分查找:可以实现对有序数组的二分查找算法。二分查找是一种高效的查找方法,可以在对数时间复杂度内找到目标元素。 8. 散列表:可以实现基于链表法解决冲突问题的散列表。散列表是一种根据关键字直接访问数据的数据结构,可以使用散列函数将关键字映射到对应的数组下标。 9. 字符:可以实现字符集的Trie树和朴素的字符匹配算法。Trie树是一种用于快速检索和匹配字符数据结构,而朴素的字符匹配算法则是一种简单但效率较低的算法。 10. 二叉树:可以实现二叉查找树,并支持插入、删除和查找操作。还可以实现查找二叉查找树中某个节点的后继、前驱节点以及二叉树的前、中、后序遍历和按层遍历。 11. 堆:可以实现小顶堆、大顶堆和优先级队列。堆是一种特殊的完全二叉树,可以用来高效地找到最大或最小元素。 12. 图:可以实现有向图和无向图的邻接矩阵和邻接表表示方式。还可以实现图的深度优先搜索、广度优先搜索以及Dijkstra算法和A*算法等。 13. 回溯:可以利用回溯算法求解八皇后问题和0-1背包问题等。回溯算法是一种通过不断回退和尝试来搜索解空间的方法。 14. 分治:可以利用分治算法求解一组数据的逆序对个数。分治算法是一种将问题分解为子问题并分别求解的方法。 15. 动态规划:可以用动态规划解决0-1背包问题、最小路径和、莱文斯坦最短编辑距离和最长公共子序列等问题。动态规划是一种通过保存子问题的解来避免重复计算的方法。 以上是一些常见的数据结构和算法的代码实现示例,具体的实现方式和代码细节可能会有所不同。在实际应用中,根据具体的问题和需求,选择合适的数据结构和算法来实现

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值