自动产生N位格雷码算法

最近算法课上,老师布置了一个题目:
  “格雷码”(Gray code)是一个长度为2n的序列,满足

A) 每个元素都是长度为n比特的串。

B) 序列中无相同元素。

C) 连续的两个元素恰好只有1比特的不同。例如,n=2时{00011110 

(1) 构造n=3时的“格雷码”。

(2)  利用分治策略设计一个算法对任意的n构造相应的“格雷码”

我是用了一个递归的方法来解决这个问题,下面是代码:

//////////////////////////////////////////
// 文件名:GrayCode.h 
// 功  能:自动产生N位格雷码算法头文件
// 作  者:石头
// 创建日期:2006-03-05
// 修改日期:2006-03-05
//////////////////////////////////////////
#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;

//////////////////////////////////////////
// 文件名:GrayCode.cpp 
// 功  能:自动产生N位格雷码算法实现文件
// 作  者:石头
// 创建日期:2006-03-05
// 修改日期:2006-03-05
//////////////////////////////////////////
#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(
"\n");
            PrintGrayCode( L );        
//打印编码
        }

        printf(
"\n");
    }

    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;

总的来说这个算法对数据结构的应用还是比较成功的,但是在性能方面还有改进的空间,当N达到100的时候就会比较慢,下次有时间要把这个算法重写一下,以得到更高的效率.
posted on 2006-03-09 23:15  Stone_石头  阅读(9045)  评论(5编辑  收藏  举报