格雷码生成算法

格雷码生成算法

头文件:

/**/ //
//  文件名:GrayCode.h 
//  功  能:产生N位格雷码算法头文件
/**/ //
#include  < iostream >
#include 
< string >
#include 
< cstdio >
#include 
< string >
#include 
< stack >
#include 
< conio.h >

using   namespace  std;

// 定义结点数据结构
typedef  struct  CodeNode {
    
char    *code;        //格雷码串
    struct    CodeNode *next;    //指向上一个码段的指针
}
CodeNode,  * CodeList;

// 函数声明
void  GenerateGrayCode(  int  n );
CodeList CreateCodeList();
void  IncreaseCode( CodeList L );
void  PrintGrayCode( CodeList L );
void  ReleaseGrayCode( CodeList L );
char   * CodeCat(  char   * des,  char   * src );

// 定义全局变量
CodeList L;

cpp文件:

 

/**/ //
//  文件名:GrayCode.cpp 
//  功  能:产生N位格雷码算法实现文件
/**/ //
#include  " GrayCode.h "

void  main()
{
    
int nbits = 1;
    
char c;
    
while( nbits != 0)
    
{
        cout
<<"Please input the bits of GrayCode(0:exit):";
        cin
>>nbits;
        GenerateGrayCode( nbits );    
//产生编码
        cout<<"Done!"<<"     Print Codes(y/n)?";
        
if( ((c = getch()) == 'y'|| ((c = getch()) == 'Y') )
        
{
            printf(
" ");
            PrintGrayCode( L );        
//打印编码
        }

        printf(
" ");
    }

    ReleaseGrayCode( L );            
//释放内存
}


void  GenerateGrayCode(  int  n )         // 算法主体
{
    
if(n == 1)
    
{
        L 
= CreateCodeList();
    }

    
else
    
{
        GenerateGrayCode( n
-1 );
        IncreaseCode( L );
    }

}


CodeList CreateCodeList()        
// 建立链表
{
    CodeList Head 
= (CodeList)malloc(sizeof(CodeNode));
    CodeList p 
= (CodeList)malloc(sizeof(CodeNode));
    CodeList q 
= (CodeList)malloc(sizeof(CodeNode));

    p
->code = "0";    q->code = "1";
    p
->next = q;    q->next = NULL;

    Head
->next = p;

    
return Head;
}


void  ReleaseGrayCode( CodeList L )     // 释放内存空间
{
    CodeList q;    
    
while( L )
    
{
        q 
= L;
        L 
= L->next;
        free(q);
    }

}


void  IncreaseCode( CodeList L)         // 做编码的一位扩展
{
    CodeList p;    
    p 
= L;
    
int count = 0;
    stack 
<char*> CodeStack;    //定义栈

    
while( p->next )
    
{
        CodeStack.push( p
->next->code );
        p
->next->code = CodeCat("0",p->next->code );
        p 
= p->next;
        count
++;
    }


    
while( count > 0 )
    
{
        CodeList q 
= (CodeList)malloc(sizeof(CodeNode));
        q
->code = CodeCat( "1",CodeStack.top() );
        CodeStack.pop();
        q
->next = NULL;
        p
->next = q;
        p 
= p->next;
        count
--;
    }

}


void  PrintGrayCode( CodeList L )     // 打印编码
{
    CodeList p;
    p 
= L->next;
        
    
while(p)
    
{
        cout
<<p->code<<endl;
        p 
= p->next;
    }

}


char   * CodeCat(  char   * des,  char   * src )     // 粘合两个字符串
{
    
char *stone = (char*)malloc((strlen(src)+strlen(des))*sizeof(char));
    strcpy(stone,des);
    strcat(stone,src);

    
return stone;
}

呵呵,就这么多了,谢谢各位!

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值