回 帖 发 新 帖 刷新版面

主题:各位高手帮帮忙啊,怎么写这两题算法感激不尽

用(C语言版)的数据结构写算法
1    用线性表的顺序存储结构实现线性表的就地逆置,并将结果输出
分别以不同存储结构实现线性表的就地逆置。线性表的就地逆置就是在原表的存储空间内将线性表(a1,a2,a3,…,an)逆置为(an,an-1,…,a2,a1)。
2     括号匹配的检验
【问题描述】
  假设表达式中允许有两种括号:圆括号和方括号,其嵌套的顺序随意,即(()【 】)或【(【 】 【 】)】等为正确格式,【( 】)或(((】均为不正确的格式。检验括号是否匹配的方法可用“期待的紧迫程度”这个概念来描
读入圆括号和方括号的任意序列,输出“匹配”或“此串括号匹配不合法”。
 因为初学,所以不怎么懂。在此,小女子我谢谢各位
高手拉

回复列表 (共7个回复)

沙发

板凳

1)#define max_size 1024
  typedef struct
  {  int data[max_size];//array for the data
     int len;//the real length of the data list
   }sque
   sque change(sque linklist)
   {
      int i;
      int temp;
      int n;
      if(linklist.len>maxsize)
      {
            printf(overflow!error!\n")
            exit(0);
       }
      if(linklist.len<0)
      {
             printf("error!\n")
             exit(0);
       }
      n=(linklist.len+1)/2;
      for(i=0;i<n;i++)
      {
             temp=a[i];
             a[i]=a[linklist.len-1-i];
             a[linklist.len-1-i]=temp;
       }
       return linklist;
}

3 楼

2
Process(char a[],int i)
{
 while(a[i])
   {
    if(a[i]=='(' || a[i]=='[')
      Push(S,a[i++]);
    if(a[i]==')')
      {
       p=GetTop(S);
       if(a[i]!='(' || StackEmpty(S))
         return ERROR;
       else
         {
          Pop(S,e);
          i++;
         }
      }
    if(a[i]==')')
      {
       p=GetTop(S);
       if(a[i]!='(' || StackEmpty(S))
         return ERROR;
       else
         {
          Pop(S,e);
          i++;
         }
      }
    if(a[i]==')')
      {
       p=GetTop(S);
       if(a[i]!='(' || StackEmpty(S))
         return ERROR;
       else
         {
          Pop(S,e);
          i++;
         }
      }
     if(a[i]==')')
      {
       p=GetTop(S);
       if(a[i]!='(' || StackEmpty(S))
         return ERROR;
       else
         {
          Pop(S,e);
          i++;
         }
      }
     if(a[i]==']')
      {
       p=GetTop(S);
       if(a[i]!='[' || StackEmpty(S))
         return ERROR;
       else
         {
          Pop(S,e);
          i++;
         }
      }
   }
 if(StackEmpty(S))
   return ERROR;     //OK表示匹配,ERROR表示不匹配
 return OK;
}

4 楼

void judge(char* string)/*only give my thinking*/
 {
     char* p;
     char* q;
     int i,j;
     int sum1,sum2;
     int woke;/*flag of judge*/
     p=string;
     
     sum1=0;
     sum2=0;
     while(p!=NULL)/*judge the string end or not,maybe is woring,you can change it for youself*/
     {
         if((*p=='(')||(*p=='[')) sum1++;
         else if((*p==')')||(*p==']')) sum2++;
              else 
              {
                  printf("something serious wrong\n");
                  exit(0);
              }
              p++;
     }
     if(sum1!=sum2)/*the base judging for the matching bracket*/ 
     {
         printf("somthing serious wrong\n");
         exit(0);
     }
     else/*judge by the ')' or the ']'*/
     {
         p=string;
         work=0;
         for(i=0;i<2*sum1;i++,p++)
         {
             if(*p==')')
             {
                 *p=0;
                 q=--p;
                 if(*q=='[')/*as we know '[' and ')' can't be together*/
                 {
                     work=1;
                     printf("error\n");
                     exit(0);
                 }
                 for(j=i-1;j>=0;j--)/*find the near '(' to match */
                 {
                     if(*q=='(') 
                     {
                         *q='0';
                         work=0;
                         break;
                     }
                     else work=1;/*judge '(' exist or not*/
                     q--;
                 }
                 if(work==1) 
                 {
                     printf("error\n");
                     exit(0);
                 }
             }
             if(*p==']')
             {
                 *p=0;
                 q=--p;
                 if(*q=='(')/*as we know '(' and ']' can't be together*/
                 {
                     work=1;
                     printf("error\n");
                     exit(0);
                 }
                 for(j=i-1;j>=0;j--)/*find the near '[' to match the ']'*/
                 {
                     if(*q=='[') 
                     {
                         *q='0';
                         work=0;
                         break;
                     }
                     else work=1;/*judge '[' exist or not*/
                     q--;
                 }
                 if(work==1) 
                 {
                     printf("error\n");
                     exit(0);
                 }
             }
         }
     }
     if(work==0)  printf("the bracket matched\n");
 }

5 楼

谢谢各位鼎立相助了,呵呵!!

6 楼

都是没有头文件和存储结构的,难怪实现不了

7 楼

要加上头文件和存储结构才能实现的,记住哦

我来回复

您尚未登录,请登录后再回复。点此登录或注册