(持续更新)造福GDUT计院的小伙伴们,数据结构anyview题目+答案,代码搓大牛们勿喷

GDUT数据结构anyview题目+答案,代码搓大牛们勿喷,大家随意就好!

/**********
【题目】试写一算法,实现链栈的判空操作。
链栈的类型定义为:
typedef struct LSNode {
 ElemType data;       // 数据域
 struct LSNode *next; // 指针域
} LSNode, *LStack;    // 结点和链栈类型
***********/
Status StackEmpty_L(LStack S)
/* 对链栈S判空。若S是空栈,则返回TRUE;否则返回FALSE */
{
    if(S == NULL) return TRUE;
    else return FALSE;
}

/**********
【题目】试写一算法,实现链栈的取栈顶元素操作。
链栈的类型定义为:
typedef struct LSNode {
  ElemType data;       // 数据域
  struct LSNode *next; // 指针域
} LSNode, *LStack;    // 结点和链栈类型
***********/
Status GetTop_L(LStack S, ElemType &e)
/* 取链栈S的栈顶元素到e,并返回OK; */
/* 若S是空栈,则失败,返回ERROR。  */
{
    if(S == NULL) return ERROR;
    else
    {
        e = S->data;
        return OK;
    }
}

/**********
【题目】试写一算法,实现链队列的判空操作。
链队列的类型定义为:
typedef struct LQNode {
  ElemType  data;
  struct LQNode  *next;
} LQNode, *QueuePtr; // 结点和结点指针类型
typedef struct {
  QueuePtr  front;  // 队头指针
  QueuePtr  rear;   // 队尾指针
} LQueue;  // 链队列类型
***********/
Status QueueEmpty_LQ(LQueue Q)
/* 判定链队列Q是否为空队列。           */
/* 若Q是空队列,则返回TRUE,否则FALSE。*/
{
    if(Q.front==NULL && Q.rear==NULL) return TRUE;
    else return FALSE;
}

/**********
【题目】试写一算法,实现链队列的求队列长度操作。
链队列的类型定义为:
typedef struct LQNode {
  ElemType  data;
  struct LQNode  *next;
} LQNode, *QueuePtr; // 结点和结点指针类型
typedef struct {
  QueuePtr  front;  // 队头指针
  QueuePtr  rear;   // 队尾指针
} LQueue;  // 链队列类型
***********/
int QueueLength_LQ(LQueue Q)
/* 求链队列Q的长度并返回其值 */
{
    int count = 0;
    LQNode* p;
    p = Q.front;
    if(Q.front == NULL && Q.rear == NULL) return count;
    while(p != NULL)
    {
        count++;
        p = p->next;
    }
    return count;
}

/**********
【题目】假设以带头结点的循环链表表示队列,并且
只设一个指针指向队尾元素结点(注意不设头指针),
试编写相应的队列初始化、入队列和出队列的算法。
带头结点循环链队列CLQueue的类型定义为:
typedef struct LQNode {
  ElemType data;
  struct LQNode *next;
} LQNode, *CLQueue;
**********/
Status InitCLQueue(CLQueue &rear) // 初始化空队列
{
    LQNode *head;

    head = (LQNode*)malloc(sizeof(LQNode));
    rear = head;
    head->next = rear;
    return OK;
}

Status EnCLQueue(CLQueue &rear, ElemType x) // 入队
{
    LQNode *head = rear->next;
    LQNode *p;
    p = (LQNode*)malloc(sizeof(LQNode));
    p->data = x;
    p->next = head;
    rear->next = p;
    rear = p;
    return OK;
}

Status DeCLQueue(CLQueue &rear, ElemType &x) // 出队
{
    LQNode *p;
    LQNode *head;
    head = rear->next;
    if(rear == NULL || head->next == rear->next) return ERROR;
    p = head->next;
    x = p->data;
    head->next = p->next;
    free(p);

    return OK;
}
/**********
【题目】试写一算法,实现带头结点单链表的判空操作。

单链表的类型定义为:
typedef struct LNode {
  ElemType  data;
  struct LNode  *next;
} LNode, *LinkList; // 结点和结点指针类型
***********/
Status ListEmpty_L(LinkList L)
/* 判定带头结点单链表L是否为空链表。   */
/* 若L是空链表,则返回TRUE,否则FALSE。*/
{
    if(L == NULL || L->next == NULL)  return TRUE;
    else return FALSE;
}

/**********
【题目】试写一算法,实现带头结点单链表的销毁操作。
单链表的类型定义为:
typedef struct LNode {
  ElemType  data;
  struct LNode  *next;
} LNode, *LinkList; // 结点和结点指针类型
***********/
Status DestroyList_L(LinkList &L)
/* 销毁带头结点单链表L,并返回OK。*/
{

    LNode *q;
    if(!L) return OK;
    while(L != NULL)
    {
        q = L;
        L = L->next;
        free(q);

    }
    return OK;
}


/**********
【题目】试写一算法,实现带头结点单链表的清空操作。

单链表的类型定义为:
typedef struct LNode {
  ElemType  data;
  struct LNode  *next;
} LNode, *LinkList; // 结点和结点指针类型
***********/
Status ClearList_L(LinkList &L)
/* 将带头结点单链表L置为空表,并返回OK。*/
/* 若L不是带头结点单链表,则返回ERROR。 */
{
    if(L == NULL) return ERROR;
    else
    {
        L->next = NULL;
        return OK;
    }
}


/**********
【题目】试写一算法,实现带头结点单链表的求表长度操作。
单链表的类型定义为:
typedef struct LNode {
  ElemType  data;
  struct LNode  *next;
} LNode, *LinkList; // 结点和结点指针类型
***********/
int ListLength_L(LinkList L)
/* 求带头结点单链表L的长度,并返回长度值。*/
/* 若L不是带头结点单链表,则返回-1。      */
{
    int count = 0;
    LNode *p = L;
    if(L == NULL) return -1;
    while(p->next != NULL)
    {
        count++;
        p = p->next;
    }
    return count;

}


/**********
【题目】试写一算法,在带头结点单链表L插入第i元素e。
带头结点单链表的类型定义为:
typedef struct LNode {
  ElemType      data;
  struct LNode *next;
} LNode, *LinkList;
**********/
Status Insert_L(LinkList L, int i, ElemType e)
/* 在带头结点单链表L插入第i元素e,并返回OK。*/
/* 若参数不合理,则返回ERROR。              */
{
    int count = 0;
    LNode* p ;
    LNode* q;
    q = (LNode*)malloc(sizeof(LNode));
    p = L;
    while(p!=NULL && count1)
    {
        p = p->next;
        count++;
    }
    if(!p || count>i-1) return ERROR;
    q->data = e;
    q->next = p->next;
    p->next = q;

    return OK;
}

/**********
【题目】试写一算法,在带头结点单链表删除第i元素到e。
带头结点单链表的类型定义为:
typedef struct LNode {
  ElemType      data;
  struct LNode *next;
} LNode, *LinkList;
**********/
Status Delete_L(LinkList L, int i, ElemType &e)
/* 在带头结点单链表L删除第i元素到e,并返回OK。*/
/* 若参数不合理,则返回ERROR。                */
{
    LNode* p = L;
    LNode* q;
    int count = 0;
    while(p->next != NULL && count1)
    {
        p = p->next;
        count++;
    }
    if(p->next == NULL || count>=i) return ERROR;
    e = p->next->data;
    q = p->next;
    p->next = q->next;
    free(q);
    return OK;
}

/**********
【题目】试写一算法,在带头结点单链表的第i元素起的
所有元素从链表移除,并构成一个带头结点的新链表。
带头结点单链表的类型定义为:
typedef struct LNode {
  ElemType      data;
  struct LNode *next;
} LNode, *LinkList;
**********/
Status Split_L(LinkList L, LinkList &Li, int i)
/* 在带头结点单链表L的第i元素起的所有元素 */
/* 移除,并构成带头结点链表Li,返回OK。   */
/* 若参数不合理,则Li为NULL,返回ERROR。  */
{
    int count = 0;
    LNode* head;
    LNode* p;
    head = (LNode*)malloc(sizeof(LNode));
    p = L;
    while(p->next && count1)
    {
        p = p->next;
        count++;
    }
    if(!(p->next)|| count>=i)
    {
        Li = NULL;
        return ERROR;
    }
    head->next = p->next;
    p->next = NULL;
    Li = head;
    return OK;
}

/**********
【题目】试写一算法,在带头结点单链表删除第i元素
起的所有元素。
带头结点单链表的类型定义为:
typedef struct LNode {
  ElemType      data;
  struct LNode *next;
} LNode, *LinkList;
**********/
Status Cut_L(LinkList L, int i)
/* 在带头结点单链表L删除第i元素起的所有元素,并返回OK。*/
/* 若参数不合理,则返回ERROR。                         */
{
    int count = 0;
    LNode* p;
    //head = (LNode*)malloc(sizeof(LNode));
    p = L;
    while(p->next && count1)
    {
        p = p->next;
        count++;
    }
    if(!(p->next)|| count>=i)
    {
        return ERROR;
    }
    p->next = NULL;
    return OK;
}

/**********
【题目】试写一算法,删除带头结点单链表中所有值
为x的元素,并释放被删结点空间。
单链表类型定义如下:
typedef struct LNode {
  ElemType      data;
  struct LNode *next;
} LNode, *LinkList;
**********/
Status DeleteX_L(LinkList L, ElemType x)
/* 删除带头结点单链表L中所有值为x的元素,      */
/* 并释放被删结点空间,返回实际删除的元素个数。*/
{
    LNode* p;
    LNode* q;
    p = L;
    int count = 0;
    if(!L) return 0;
    while(p->next)
    {
        if(p->next->data == x)
        {
            q = p->next;
            p->next = q->next;
            free(q);
            count++;
        }
        else p = p->next;
    }
    return count;
}


/**********
【题目】试写一算法,删除带头结点单链表中所有值
小于x的元素,并释放被删结点空间。
单链表类型定义如下:
typedef struct LNode {
  ElemType      data;
  struct LNode *next;
} LNode, *LinkList;
**********/
Status DeleteSome_L(LinkList L, ElemType x)
/* 删除带头结点单链表L中所有值小于x的元素,    */
/* 并释放被删结点空间,返回实际删除的元素个数。*/
{
    LNode* p;
    LNode* q;
    p = L;
    int count = 0;
    if(!L) return 0;
    while(p->next)
    {
        if(p->next->data < x)
        {
            q = p->next;
            p->next = q->next;
            free(q);
            count++;
        }
        else p = p->next;
    }
    return count;
}

/**********
【题目】试以顺序表L的L.rcd[L.length+1]作为监视哨,
改写教材5.2节中给出的升序直接插入排序算法。
顺序表的类型RcdSqList定义如下:
typedef struct {
   KeyType key;
   ...
} RcdType;
typedef struct {
   RcdType rcd[MAXSIZE+1]; // r[0]闲置
   int length;
} RcdSqList;
**********/
int a[100];
int cmp (int a,int b)
{
    return avoid swap(int x,int y)
{
    int t=a[x];
    a[x]=a[y];
    a[y]=t;
}
void InsertSort(RcdSqList &L)
{
    int i,j;
    int cnt=0;
    for(i=1; i<=L.length; i++)
    {
        a[i]=L.rcd[i].key;
    }
    for(i=1; ifor(j=i+1; j<=L.length; j++)
        {
            if(a[i]>a[j])
            {
                swap(i,j);
            }
        }
    }
    for(i=1; i<=L.length; i++)
    {
        L.rcd[i].key=a[i];
    }
}

/**********
【题目】如下所述,改写教材1.5节的冒泡排序算法:
将算法中用以起控制作用的布尔变量change改为一个整型
变量,指示每一趟排序中进行交换的最后一个记录的位置,
并以它作为下一趟起泡排序循环终止的控制值。
顺序表的类型RcdSqList定义如下:
typedef struct {
   KeyType key;
   ...
} RcdType;
typedef struct {
   RcdType rcd[MAXSIZE+1]; // r[0]闲置
   int length;
} RcdSqList;
**********/
void BubbleSort(RcdSqList &L)
/* 元素比较和交换必须调用如下定义的比较函数和交换函数:*/
/* Status LT(RedType a, RedType b);   比较:"<"        */
/* Status GT(RedType a, RedType b);   比较:">"        */
/* void Swap(RedType &a, RedType &b); 交换             */
{
    int i,j,final = L.length+1,k;
    for(i = 1; i < L.length; i++)
    {
        for(j = 2,k = 0; j < final; j++)
        {
            if(GT(L.rcd[j-1],L.rcd[j]))
            {
                Swap(L.rcd[j-1],L.rcd[j]);
                k = j;
            }
        }
        final = k;
        if(final <= 2)break;
    }
}

/**********
【题目】已知记录序列L.rcd[1..L.length]中的关键
字各不相同,可按如下所述实现计数排序:另设数组
c[1..n],对每个记录a[i], 统计序列中关键字比它
小的记录个数存于c[i],则c[i]=0的记录必为关键字
最小的记录,然后依c[i]值的大小对序列中记录进行
重新排列。试编写算法实现上述排序方法。
顺序表的类型RcdSqList定义如下:
typedef struct {
   KeyType key;
   ...
} RcdType;
typedef struct {
   RcdType r[MAXSIZE+1]; // r[0]闲置
   int     length;
} RcdSqList;
**********/
void CountSort(RcdSqList &L)
/* 采用顺序表存储结构,在函数内自行定义计数数组c */
{
    int c[MAXSIZE + 1];
    int i,j,k,tempI;
    RcdType temp;

    for(i = 1; i <= L.length; i++)
    {
        for(j = 1; j<=L.length; j++)
        {
            if(L.rcd[j].key < L.rcd[i].key)
            {
                c[i]++;
            }
        }
    }
    for(i = 1; i<=L.length; i++)
    {
        j = i;
        for(k = i+1; k <= L.length; k++)
        {
            if(c[k] < c[j])
                j = k;
        }
        if(j != i)
        {
            temp = L.rcd[i];
            L.rcd[i] = L.rcd[j];
            L.rcd[j] = temp;
            tempI = c[i];
            c[i] = c[j];
            c[j] = tempI;
        }
    }
}

/**********
【题目】已知某哈希表的装载因子小于1,哈希函数H(key)
为关键字(标识符)的第一个字母在字母表中的序号,处理
冲突的方法为线性探测开放定址法。试编写一个按第一个
字母的顺序输出哈希表中所有关键字的算法。
哈希表的类型HashTable定义如下:
#define SUCCESS    1
#define UNSUCCESS  0
#define DUPLICATE -1
typedef char StrKeyType[4];
typedef struct {
   StrKeyType key; // 关键字项
   int    tag;     // 标记 0:空;1:有效; -1:已删除
   void  *any;     // 其他信息
} RcdType;
typedef struct {
  RcdType *rcd;    // 存储空间基址
  int      size;   // 哈希表容量
  int      count;  // 表中当前记录个数
} HashTable;
**********/
int a[30];
int cmp(HashTable ht,int i,int j)
{
    int k;
    for(k=0; ht.rcd[i].key[k]; k++)
    {
        if(ht.rcd[i].key[k]==ht.rcd[j].key[k])
        {
            continue;
        }
        else if(ht.rcd[i].key[k]>=ht.rcd[j].key[k])
        {
            return 1;
        }
    }
    return 0;
}
void PrintKeys(HashTable ht, void(*print)(StrKeyType))
/* 依题意用print输出关键字 */
{
    int i,j,cnt=0,tmp;
    //printf("%d\n",ht.count);
    for(i=0; i//print(ht.rcd[i].key);
        if(ht.rcd[i].key[0]>='A'&&ht.rcd[i].key[0]<='Z')
        {
            a[++cnt]=i;
        }
        if(ht.rcd[i].tag==1)
        {
            //a[++cnt]=i;
        }
    }
    for(i=1; ifor(j=i+1; j<=cnt; j++)
        {
            //if(cmp(ht,i,j)==1)
            if(ht.rcd[a[i]].key[0]>ht.rcd[a[j]].key[0])
                //if(strcmp(ht.rcd[i].key,ht.rcd[j].key)>0)
            {
                tmp=a[i];
                a[i]=a[j];
                a[j]=tmp;
            }
            else if(ht.rcd[a[i]].key[0]==ht.rcd[a[j]].key[0]&&
                    ht.rcd[a[i]].key[1]>=ht.rcd[a[j]].key[1])
            {
                tmp=a[i];
                a[i]=a[j];
                a[j]=tmp;
            }
        }
    }
    for(i=1; i<=cnt; i++)
    {
        print(ht.rcd[a[i]].key);
    }
}

/**********
【题目】已知某哈希表的装载因子小于1,哈希函数H(key)
为关键字(标识符)的第一个字母在字母表中的序号,处理
冲突的方法为线性探测开放定址法。试编写一个按第一个
字母的顺序输出哈希表中所有关键字的算法。
哈希表的类型HashTable定义如下:
#define SUCCESS    1
#define UNSUCCESS  0
#define DUPLICATE -1
typedef char StrKeyType[4];
typedef struct {
   StrKeyType key; // 关键字项
   int    tag;     // 标记 0:空;1:有效; -1:已删除
   void  *any;     // 其他信息
} RcdType;
typedef struct {
  RcdType *rcd;    // 存储空间基址
  int      size;   // 哈希表容量
  int      count;  // 表中当前记录个数
} HashTable;
**********/
int a[30];
int cmp(HashTable ht,int i,int j)
{
    int k;
    for(k=0; ht.rcd[i].key[k]; k++)
    {
        if(ht.rcd[i].key[k]==ht.rcd[j].key[k])
        {
            continue;
        }
        else if(ht.rcd[i].key[k]>=ht.rcd[j].key[k])
        {
            return 1;
        }
    }
    return 0;
}
void PrintKeys(HashTable ht, void(*print)(StrKeyType))
/* 依题意用print输出关键字 */
{
    int i,j,cnt=0,tmp;
    for(i=0; iif(ht.rcd[i].tag!=-1&&ht.rcd[i].key[0]>='A'&&ht.rcd[i].key[0]<='Z')
        {
            a[++cnt]=i;
        }
    }
    for(i=1; ifor(j=i+1; j<=cnt; j++)
        {
            if(ht.rcd[a[i]].key[0]>ht.rcd[a[j]].key[0])
            {
                tmp=a[i];
                a[i]=a[j];
                a[j]=tmp;
            }
            else if(ht.rcd[a[i]].key[0]==ht.rcd[a[j]].key[0]&&
                    ht.rcd[a[i]].key[1]>=ht.rcd[a[j]].key[1])
            {
                tmp=a[i];
                a[i]=a[j];
                a[j]=tmp;
            }
        }
    }
    for(i=1; i<=cnt; i++)
    {
        print(ht.rcd[a[i]].key);
    }
}

/**********
【题目】试编写如下定义的递归函数的递归算法:
    g(m,n) = 0             当m=0,n>=0
    g(m,n) = g(m-1,2n)+n   当m>0,n>=0
**********/
int G(int m, int n)
/* 如果 m<0 或 n<0 则返回 -1 */
{
    if(m<0||n<0)
    {
        return -1;
    }
    if(m==0&&n>=0)
    {
        return 0;
    }
    return G(m-1,2*n)+n;
}

/**********
【题目】试写出求递归函数F(n)的递归算法:
    F(n) = n+1      当n=0
    F(n) = nF(n/2)  当n>0
**********/
int F(int n)
/* 如果 n<0 则返回 -1 */
{
    if(n<0)return -1;
    if(n==0)return n+1;
    return n*F(n/2);
}

/**********
【题目】求解平方根 的迭代函数定义如下:
  sqrt(A,p,e) = p                   当|p*p-A|=e
其中,p是A的近似平方根,e是结果允许误差。试写出相
应的递归算法。
**********/
float Fabs(float a,float b)
{
    return a>b?a-b:b-a;
}
float Sqrt(float A, float p, float e)
{
    if(Fabs(p*p,A)>-e&&Fabs(p*p,A)return p;
    else
    {
        return Sqrt(A,(p+A/p)/2,e);
    }
}

/**********
【题目】已知Ackerman函数的定义如下:
   akm(m,n) = n+1                 当m=0
   akm(m,n) = akm(m-1,1)          当m!=0,n=0
   akm(m,n) = akm(m-1,akm(m,n-1)) 当m!=0,n!=0
请写出递归算法。
**********/
int Akm(int m, int n)
/* 若 m<0 或 n<0 则返回-1  */
{
    if(m<0||n<0)return -1;
    if(m==0)return n+1;
    if(m!=0&&n==0)return Akm(m-1,1);
    if(m!=0&&n!=0)return Akm(m-1,Akm(m,n-1));
}

/**********
【题目】试写出求递归函数F(n)的非递归算法:
    F(n) = n+1      当n=0
    F(n) = nF(n/2)  当n>0
**********/
int F(int n)
/* 如果 n<0 则返回 -1 */
{
    if(n<0)return -1;
    if(n==0)return n+1;
    return n*F(n/2);
}

/**********
【题目】若两棵二叉树T1和T2皆为空,或者皆不空
且T1的左、右子树和T2的左、右子树分别相似,则
称二叉树T1和T2相似。试编写算法,判别给定两棵
二叉树是否相似。
二叉链表类型定义:
typedef struct BiTNode {
  TElemType  data;
  struct BiTNode  *lchild, *rchild;
} BiTNode, *BiTree;
**********/
Status Similar(BiTree T1, BiTree T2)
/* 判断两棵二叉树是否相似的递归算法 */
{
    if(T1==NULL&&T2==NULL)
    {
        return TRUE;
    }
    else if(T1&&T2)
    {
        return Similar(T1->lchild,T2->lchild)
               &&Similar(T1->rchild,T2->rchild);
    }
    else
    {
        return FALSE;
    }
}

/**********
【题目】编写递归算法,求对二叉树T先序遍历时
第k个访问的结点的值。
二叉链表类型定义:
typedef struct BiTNode {
  TElemType data;
  struct BiTNode  *lchild, *rchild;
} BiTNode, *BiTree;
**********/
TElemType Get(BiTree bt,int &count,TElemType &e)
{
    if(bt)
    {
        if(1==count)
        {
            e=bt->data;
            return 0;
        }
        else
        {
            if(bt->lchild)
            {
                --count;
                Get(bt->lchild,count,e);
            }
            if(bt->rchild)
            {
                --count;
                Get(bt->rchild,count,e);
            }
        }
    }
}
TElemType PreOrderK(BiTree T, int k)
/* 求对二叉树T先序遍历时第k个访问的结点的值。*/
/* 若失败,则返回'#'                         */
{
    TElemType ans='#';
    Get(T,k,ans);
    return ans;
}

/**********
【题目】编写递归算法,计算二叉树T中叶子结点的数目。
二叉链表类型定义:
typedef struct BiTNode {
  TElemType  data;
  struct BiTNode  *lchild, *rchild;
} BiTNode, *BiTree;
**********/
int ans;
void fuck(BiTree T)
{
    if(T->lchild==NULL&&T->rchild==NULL)
    {
        ans++;
    }
    else
    {
        if(T->lchild)fuck(T->lchild);
        if(T->rchild)fuck(T->rchild);
    }
}
int Leaves(BiTree T)
/* 计算二叉树T中叶子结点的数目 */
{
    ans=0;
    if(T)fuck(T);
    return ans;
}

/**********
【题目】试利用栈及其基本操作写出二叉树T的非递归
的遍历先序算法。
二叉链表类型定义:
typedef struct BiTNode {
  TElemType  data;
  struct BiTNode  *lchild,*rchild;
} BiTNode, *BiTree;
可用栈类型Stack的相关定义:
typedef BiTree SElemType;   // 栈的元素类型
Status InitStack(Stack &S);
Status StackEmpty(Stack S);
Status Push(Stack &S, SElemType e);
Status Pop(Stack &S, SElemType &e);
Status GetTop(Stack S, SElemType &e);
**********/
void PreOrder(BiTree T, void (*visit)(TElemType))
/* 使用栈,非递归先序遍历二叉树T,     */
/* 对每个结点的元素域data调用函数visit */
{
    Stack S;
    BiTree p;
    InitStack(S);
    Push(S,T);
    while(!StackEmpty(S))
    {
        while(GetTop(S,p)&&p)
        {
            visit(p->data);
            Push(S,p->lchild);
        }
        Pop(S,p);
        if(!StackEmpty(S))
        {
            Pop(S,p);
            Push(S,p->rchild);
        }
    }
}

/**********
【题目】试利用栈及其基本操作写出二叉树T的非递归
的后序遍历算法(提示:为分辨后序遍历时两次进栈的
不同返回点,需在指针进栈时同时将一个标志进栈)。
二叉链表类型定义:
typedef struct BiTNode {
  TElemType  data;
  struct BiTNode  *lchild,*rchild;
} BiTNode, *BiTree;
可用栈类型Stack的相关定义:
typedef struct {
  struct BiTNode *ptr; // 二叉树结点的指针类型
  int      tag; // 0..1
} SElemType;    // 栈的元素类型
Status InitStack(Stack &S);
Status StackEmpty(Stack S);
Status Push(Stack &S, SElemType e);
Status Pop(Stack &S, SElemType &e);
Status GetTop(Stack S, SElemType &e);
**********/
void PostOrder(BiTree T, void (*visit)(TElemType))
/* 使用栈,非递归后序遍历二叉树T,     */
/* 对每个结点的元素域data调用函数visit */
{
    /*if(T)
    {
      if(T->lchild)
      {
        PostOrder(T->lchild,visit);
      }
      if(T->rchild)
      {
        PostOrder(T->rchild,visit);
      }
      visit(T->data);
    }*/
    Stack S;
    BiTree cur=T,pre=NULL;
    SElemType tmp;
    InitStack(S);
    while(cur!=NULL||!StackEmpty(S))
    {
        while(cur!=NULL)
        {
            tmp.ptr=cur;
            Push(S,tmp);
            cur=cur->lchild;
        }
        GetTop(S,tmp);
        cur=tmp.ptr;
        if(cur->rchild==NULL||cur->rchild==pre)
        {
            pre=cur;
            visit(cur->data);
            tmp.ptr=cur;
            Pop(S,tmp);
            cur=NULL;
        }
        else
        {
            cur=cur->rchild;
        }
    }
}

/**********
【题目】试利用栈及其基本操作写出二叉树T的非递归
的后序遍历算法(提示:为分辨后序遍历时两次进栈的
不同返回点,需在指针进栈时同时将一个标志进栈)。
二叉链表类型定义:
typedef struct BiTNode {
  TElemType  data;
  struct BiTNode  *lchild,*rchild;
} BiTNode, *BiTree;
可用栈类型Stack的相关定义:
typedef struct {
  struct BiTNode *ptr; // 二叉树结点的指针类型
  int      tag; // 0..1
} SElemType;    // 栈的元素类型
Status InitStack(Stack &S);
Status StackEmpty(Stack S);
Status Push(Stack &S, SElemType e);
Status Pop(Stack &S, SElemType &e);
Status GetTop(Stack S, SElemType &e);
**********/
void PostOrder(BiTree T, void (*visit)(TElemType))
/* 使用栈,非递归后序遍历二叉树T,     */
/* 对每个结点的元素域data调用函数visit */
{
    /*if(T)
    {
      if(T->lchild)
      {
        PostOrder(T->lchild,visit);
      }
      if(T->rchild)
      {
        PostOrder(T->rchild,visit);
      }
      visit(T->data);
    }*/
    Stack S;
    BiTree cur=T,pre=NULL;
    SElemType tmp;
    InitStack(S);
    while(cur!=NULL||!StackEmpty(S))
    {
        while(cur!=NULL)
        {
            tmp.ptr=cur;
            Push(S,tmp);
            cur=cur->lchild;
        }
        GetTop(S,tmp);
        cur=tmp.ptr;
        if(cur->rchild==NULL||cur->rchild==pre)
        {
            pre=cur;
            visit(cur->data);
            tmp.ptr=cur;
            Pop(S,tmp);
            cur=NULL;
        }
        else
        {
            cur=cur->rchild;
        }
    }
}

/**********
【题目】编写递归算法,将二叉树中所有结点的
左、右子树相互交换。
二叉链表类型定义:
typedef struct BiTNode {
  TElemType data;
  struct BiTNode  *lchild, *rchild;
} BiTNode, *BiTree;
**********/
BiTree tmp;
void Fxxx(BiTree T)
{
    if(T->lchild||T->rchild)
    {
        tmp=T->lchild;
        T->lchild=T->rchild;
        T->rchild=tmp;
    }
    if(T->lchild)
    {
        Fxxx(T->lchild);
    }
    if(T->rchild)
    {
        Fxxx(T->rchild);
    }
}
void ExchangeSubTree(BiTree &T)
/* 将二叉树中所有结点的左、右子树相互交换 */
{
    Fxxx(T);
}

/**********
【题目】编写递归算法:求二叉树中以元素值
为x的结点为根的子树的深度。
二叉链表类型定义:
typedef struct BiTNode {
  TElemType data;
  struct BiTNode  *lchild, *rchild;
} BiTNode, *BiTree;
**********/
BiTree pos;
int Fxxx(BiTree T)
{
    int x,y;
    if(!T->lchild&&!T->rchild)
    {
        return 0;
    }
    else
    {
        x=0;
        y=0;
        if(T->lchild)
        {
            x=Fxxx(T->lchild);
        }
        if(T->rchild)
        {
            y=Fxxx(T->rchild);
        }
        return x>y?x+1:y+1;
    }
}
void Findx(BiTree T,TElemType x)
{
    if(pos!=NULL)return;
    if(T->data==x)
    {
        pos=T;
        return;
    }
    if(T->lchild)
    {
        Findx(T->lchild,x);
    }
    if(T->rchild)
    {
        Findx(T->rchild,x);
    }
}
int Depthx(BiTree T, TElemType x)
/* 求二叉树中以值为x的结点为根的子树深度 */
{
    pos=NULL;
    Findx(T,x);
    if(pos==NULL)return 0;
    return Fxxx(pos)+1;
}

/**********
【题目】编写递归算法:对于二叉树中每一个元素值为x
的结点,删去以它为根的子树,并释放相应的空间。
二叉链表类型定义:
typedef struct BiTNode {
  TElemType data;
  struct BiTNode  *lchild, *rchild;
} BiTNode, *BiTree;
**********/
BiTree pos;
void Fxxx(BiTree T,char x)
{
    if(T->lchild&&T->lchild->data==x)
    {
        T->lchild=NULL;
        free(T->lchild);
        return;
    }
    if(T->rchild&&T->rchild->data==x)
    {
        T->rchild=NULL;
        free(T->rchild);
        return;
    }
    if(!T->lchild)Fxxx(T->lchild,x);
    if(!T->rchild)Fxxx(T->rchild,x);
}
void ReleaseX(BiTree &T, char x)
/* 对于二叉树T中每一个元素值为x的结点, */
/* 删去以它为根的子树,并释放相应的空间 */
{
    Fxxx(T,x);
}

/**********
【题目】编写复制一棵二叉树的递归算法。
二叉链表类型定义:
typedef char TElemType; // 设二叉树的元素为char类型
typedef struct BiTNode {
  TElemType data;
  struct BiTNode  *lchild, *rchild;
} BiTNode, *BiTree;
**********/
void CopyBiTree(BiTree T, BiTree &TT)
/* 递归复制二叉树T得到TT */
{
    BiTree LC,RC;
    if(!T)
    {
        TT=NULL;
    }
    else
    {
        CopyBiTree(T->lchild,LC);
        CopyBiTree(T->rchild,RC);
        TT=(BiTree)malloc(sizeof(BiTNode));
        TT->data=T->data;
        TT->lchild=LC;
        TT->rchild=RC;
    }
}

/**********
【题目】编写算法判别给定二叉树是否为完全二叉树。
二叉链表类型定义:
typedef struct BiTNode {
  TElemType data;
  struct BiTNode  *lchild, *rchild;
} BiTNode, *BiTree;
可用队列类型Queue的相关定义:
typedef BiTree QElemType; // 设队列元素为二叉树的指针类型
Status InitQueue(Queue &Q);
Status EnQueue(Queue &Q, QElemType e);
Status DeQueue(Queue &Q, QElemType &e);
Status GetHead(Queue Q, QElemType &e);
Status QueueEmpty(Queue Q);
**********/
int max(int x,int y)
{
    return x>y?x:y;
}
Status CompleteBiTree(BiTree T)
/* 判别二叉树T是否为完全二叉树 */
{
    if(T!=NULL)
    {
        Queue Q;
        int cnt=0,num;
        InitQueue(Q);
        BiTree p=T;
        p->data=1;
        EnQueue(Q,p);
        cnt++;
        num=1;
        while(OK==DeQueue(Q,p))
        {
            if(p->lchild)
            {
                p->lchild->data=p->data*2;
                num=max(p->lchild->data,num);
                EnQueue(Q,p->lchild);
                cnt++;
            }
            if(p->rchild)
            {
                p->rchild->data=p->data*2+1;
                num=max(p->rchild->data,num);
                EnQueue(Q,p->rchild);
                cnt++;
            }
        }
        return num==cnt;
    }
    else
    {
        return true;
    }
}

/**********
【题目】试编写一个二叉排序树的判定算法。
二叉排序树的类型BSTree定义如下:
typedef struct {
  KeyType key;
  ... ...   // 其他数据域
} TElemType;
typedef struct BiTNode {
  TElemType data;
  struct BSTNode  *lchild, *rchild;
}BSTNode, *BSTree;
**********/
int ans;
void fuck(BSTree T)
{
    if(T->lchild)
    {
        if(T->data.key < T->lchild->data.key)
        {
            ans=0;
        }
        else
        {
            fuck(T->lchild);
        }
    }
    if(T->rchild)
    {
        if(T->data.key>T->rchild->data.key)
        {
            ans=0;
        }
        else
        {
            fuck(T->rchild);
        }
    }
}
Status IsBSTree(BSTree T)
/* 判别二叉树T是否为二叉排序树。*/
/* 若是,则返回TRUE,否则FALSE  */
{
    ans=1;
    if(T)fuck(T);
    return ans;
}

/**********
【题目】假设以二维数组g[1..m][1..n]表示一个图像
区域,g[i][j]表示该区域中点(i,j)所具颜色,其值
为从0到k的整数。试编写递归算法,将点(i0,j0)所在
区域的颜色置换为颜色c。约定与(i0,j0)同色的上、
下、左、右的邻接点为同色区域的点。

表示图像区域的类型定义如下:
typedef char GTYPE[m+1][n+1];
**********/
int dx[6]= {0,0,0,-1,1};
int dy[6]= {0,-1,1,0,0};
int vis[25][25];
void fuck(GTYPE g,int m,int n,int i0,int j0,int cx,int cy)
{
    int k,nx,ny;
    vis[cx][cy]=1;
    for(k=1; k<=4; k++)
    {
        nx=cx+dx[k];
        ny=cy+dy[k];
        if(nx>=1&&nx<=m&&ny>=1&&ny<=n&&!vis[nx][ny]&&g[nx][ny]==g[i0][j0])
        {
            fuck(g,m,n,i0,j0,nx,ny);
        }
    }
}
void ChangeColor(GTYPE g, int m, int n,
                 char c, int i0, int j0)
/* 在g[1..m][1..n]中,将元素g[i0][j0] */
/* 所在的同色区域的颜色置换为颜色c    */
{
    int i,j;
    for(i=1; i<=m; i++)
    {
        for(j=1; j<=n; j++)
        {
            vis[i][j]=0;
        }
    }
    fuck(g,m,n,i0,j0,i0,j0);
    for(i=1; i<=m; i++)
    {
        for(j=1; j<=n; j++)
        {
            if(vis[i][j])
            {
                g[i][j]=c;
            }
        }
    }
}

/**********
【题目】编写递归算法,从大到小输出给定二叉排序树
中所有关键字不小于x的数据元素。
二叉排序树的类型BSTree定义如下:
typedef struct {
    KeyType key;
    ... ...   // 其他数据域
} TElemType;
typedef struct BSTNode {
  TElemType  data;
  struct BSTNode  *lchild,*rchild;
}BSTNode, *BSTree;
**********/
void OrderOut(BSTree T, KeyType k, void(*visit)(TElemType))
/* 调用visit(T->data)输出  */
{
    if(T->rchild)
    {
        OrderOut(T->rchild,k,visit);
    }
    if(T->data.key>=k)
    {
        visit(T->data);
    }
    if(T->lchild)
    {
        OrderOut(T->lchild,k,visit);
    }
}

/**********
【题目】试写一非递归算法,在二叉查找树T中插入元素e。
二叉查找树的类型BSTree定义如下:
typedef struct {
  KeyType key;
    ... ...   // 其他数据域
} TElemType;
typedef struct BSTNode {
  TElemType data;
  struct BSTNode  *lchild,*rchild;
} BSTNode, *BSTree;
**********/
Status InsertBST_I(BSTree &T, TElemType k)
/* 在二叉查找树T中插入元素e的非递归算法 */
{
    BSTNode *s,*p;
    if(T==NULL)
    {
        s=(BSTNode *)malloc(sizeof(BSTNode));
        s->data=k;
        s->lchild=s->rchild=NULL;
    }
    else
    {
        s=T;
        while(1)
        {
            if(s->data.key == k.key)return -1;
            if(s->data.key>k.key)
            {
                if(s->lchild)
                {
                    s=s->lchild;
                }
                else
                {
                    break;
                }
            }
            else
            {
                if(s->rchild)
                {
                    s=s->rchild;
                }
                else
                {
                    break;
                }
            }
        }
        p=(BSTNode*)malloc(sizeof(BSTNode));
        p->data.key=k.key;
        p->lchild=p->rchild=NULL;
        if(s->data.key>k.key)
        {
            s->lchild=p;
        }
        else
        {
            s->rchild=p;
        }
    }
}

/**********
【题目】试编写算法,求二叉树T中结点a和b的最近共同祖先。
二叉链表类型定义:
typedef struct BiTNode {
  TElemType data;
  struct BiTNode  *lchild,*rchild;
} BiTNode, *BiTree;
可用栈类型Stack的相关定义:
typedef struct {
  BiTNode *ptr; // 二叉树结点的指针类型
  int      tag; // 0..1
} SElemType;      // 栈的元素类型
Status InitStack(Stack &S);
Status StackEmpty(Stack S);
int StackLength(SqStack S);
Status Push(Stack &S, SElemType e);
Status Pop(Stack &S, SElemType &e);
Status GetTop(Stack S, SElemType &e);
**********/
BiTree CommAncestor(BiTree T, TElemType a, TElemType b)
/* 求二叉树T中结点a和b的最近共同祖先 */
{
    BiTree s[50],t,A[50],B[50];
    int sizeA,sizeB,i;
    int tag[50];
    int top=-1;
    t=T;
    while(t!=NULL||top!=-1)
    {
        if(t->data==a)
        {
            printf("A[]= ");
            for(sizeA=0; sizeA<=top; sizeA++)
            {
                A[sizeA]=s[sizeA];
                printf("%c ",s[sizeA]->data);
            }
            printf("\n");
        }
        if(t->data==b)
        {
            printf("B[]= ");
            for(sizeB=0; sizeB<=top; sizeB++)
            {
                B[sizeB]=s[sizeB];
                printf("%c ",s[sizeB]->data);
            }
            printf("\n");
        }
        while(t!=NULL)
        {
            s[++top]=t;
            tag[top]=0;
            t=t->lchild;
            if(t->data==a)
            {
                printf("A[]= ");
                for(sizeA=0; sizeA<=top; sizeA++)
                {
                    A[sizeA]=s[sizeA];
                    printf("%c ",s[sizeA]->data);
                }
                printf("\n");
            }
            if(t->data==b)
            {
                printf("B[]= ");
                for(sizeB=0; sizeB<=top; sizeB++)
                {
                    B[sizeB]=s[sizeB];
                    printf("%c ",s[sizeB]->data);
                }
                printf("\n");
            }
        }
        while(top!=-1&&tag[top]==1)
        {
            //printf("%c ",s[top]->data);
            top--;
        }
        if(top!=-1)
        {
            tag[top]=1;
            t=s[top]->rchild;
            if(t->data==a)
            {
                printf("A[]= ");
                for(sizeA=0; sizeA<=top; sizeA++)
                {
                    A[sizeA]=s[sizeA];
                    printf("%c ",s[sizeA]->data);
                }
                printf("\n");
            }
            if(t->data==b)
            {
                printf("B[]= ");
                for(sizeB=0; sizeB<=top; sizeB++)
                {
                    B[sizeB]=s[sizeB];
                    printf("%c ",s[sizeB]->data);
                }
                printf("\n");
            }
        }
    }
    for(i=0; i//printf(" %c",A[i]->data);
    }
    //printf("\n");
    for(i=0; i//printf(" %c",B[i]->data);
    }
    //printf("\n");
    for(i=0; iif(A[i]->data!=B[i]->data)
        {
            break;
        }
    }
    if(i==0)return NULL;
    return A[i-1];
}

/**********
【题目】在平衡二叉排序树的每个结点中增设一个
lsize域,其值为该结点的左子树中的结点数加1。
试编写时间复杂度为O(logn)的算法,求树中第k小
的结点的位置。
平衡二叉查排序树的类型BBSTree定义如下:
typedef char KeyType;

typedef struct BBSTNode {
  KeyType key;
  struct BBSTNode  *lchild,*rchild;
  int bf;    // 平衡因子
  int lsize; // 新增域,值为左子树的结点数+1
} BBSTNode, *BBSTree;
**********/
BBSTNode *Ranking(BBSTree T, int k)
/* 在含lsize域的平衡二叉排序树T中,*/
/* 求指向T中第k小的结点的指针      */
{
    if(k>T->lsize)
    {
        if(!T->rchild) return NULL;
        return(Ranking(T->rchild,k-T->lsize));
    }
    else if(klsize)
        return (Ranking(T->lchild,k));
    return T;
}

/**********
【题目】假设二叉排序树T的每个结点的平衡因子域bf当前
均为0。试编写算法,求二叉排序树T的深度,并为每个结点
的bf域赋予正确的平衡因子值。
平衡二叉排序树的类型BBSTree定义如下:
typedef char KeyType;
typedef struct BBSTNode {
  KeyType key;
  int bf;    // 平衡因子
  struct BBSTNode  *lchild,*rchild;
} BBSTNode, *BBSTree;
**********/
int Depth_BF(BBSTree T)
/* 求二叉排序树T的深度,并为每个结点 */
/* 的bf域赋予正确的平衡因子值。      */
{
    if(T->lchild==NULL&&T->rchild==NULL)
    {
        return 1;
    }
    int ld=1,rd=1;
    if(T->lchild)
    {
        ld+=Depth_BF(T->lchild);
    }
    if(T->rchild)
    {
        rd+=Depth_BF(T->rchild);
    }
    T->bf=ld-rd;
    return ld>rd?ld:rd;
}

/**********
【题目】编写平衡二叉排序树的右平衡处理算法。
平衡二叉排序树的类型BBSTree定义如下:
typedef char KeyType;
typedef struct BBSTNode {
  KeyType key;
  int  bf;    // 平衡因子
  struct BBSTNode  *lchild,*rchild;
} BBSTNode, *BBSTree;
可调用下列旋转调整操作:
void L_Rotate(BBSTree &p); // 对最小失衡子树p做左旋调整
void R_Rotate(BBSTree &p); // 对最小失衡子树p做右旋调整
**********/
void RightBalance(BBSTree &T)
/* 实现对二叉树T的右平衡处理 */
{
    BBSTree rc,ld;
    rc=T->rchild;
    switch(rc->bf)
    {
    case RH:
        T->bf=rc->bf=EH;
        L_Rotate(T);
        break;
    case LH:
        ld=rc->lchild;
        switch(ld->bf)
        {
        case LH:
            T->bf=EH;
            rc->bf=RH;
            break;
        case EH:
            T->bf=rc->bf=EH;
            break;
        case RH:
            T->bf=LH;
            rc->bf=EH;
            break;
        }
        ld->bf=EH;
        R_Rotate(T->rchild);
        L_Rotate(T);
        break;
    }
}

/**********
【题目】试编写算法,对一棵以孩子兄弟链表表示
的树统计叶子的个数。
孩子兄弟链表类型定义:
typedef struct CSTNode {
  TElemType  data;
  struct CSTNode  *firstChild, *nextSibling;
} CSTNode, *CSTree;
**********/
int ans;
void solve(CSTree T)
{
    if(!T->firstChild)
    {
        printf("%c ",T->data);
        ans++;
    }
    if(T->firstChild)
    {
        solve(T->firstChild);
    }
    if(T->nextSibling)
    {
        solve(T->nextSibling);
    }
}
int Leave(CSTree T) /* 统计树T的叶子数 */
{
    ans=0;
    solve(T);
    return ans;
}

/**********
【题目】试编写算法,求一棵以孩子兄弟链表表示的树的度。
孩子兄弟链表类型定义:
typedef struct CSTNode {
  TElemType  data;
  struct CSTNode  *firstChild, *nextSibling;
} CSTNode, *CSTree;
**********/
int ans;
void solve(CSTree T,int cur)
{
    ans=cur>ans?cur:ans;
    if(T->firstChild)
    {
        solve(T->firstChild,1);
    }
    if(T->nextSibling)
    {
        solve(T->nextSibling,cur+1);
    }
}
int Degree(CSTree T) /* 求树T的度 */
{
    if(T==NULL)return 0;
    ans=0;
    solve(T,0);
    return ans;
}

/**********
【题目】试编写算法,对以双亲表示法存储的树计算深度。
typedef struct {
  TElemType data;
  int     parent;  // 双亲位置
} PTNode; // 结点类型
typedef struct {
  PTNode nodes[MAX_TREE_SIZE]; // 结点存储空间
  int  n, r; // 结点数和根的位置
} PTree;
**********/
int PTreeDepth(PTree T) /* 求树T的深度 */
{
    int ans=0;
    int i;
    int res,cur;
    for(i=0; i0;
        cur=T.nodes[i].parent;
        while(cur!=-1)
        {
            res++;
            cur=T.nodes[cur].parent;
        }
        if(res>ans)ans=res;
    }
    return ans+1;
}

/**********
【题目】试编写算法,对以双亲孩子表示法存储的树计算深度。
孩子链表类型定义:
typedef struct ChildNode {  // 孩子结点
  int childIndex;
  struct ChildNode *nextChild;
} ChildNode; // 孩子结点类型
typedef struct  {
  TElemType data;
  int     parent;  // 双亲位置
  struct ChildNode *firstChild; // 孩子链表头指针
} PCTreeNode; // 结点类型
typedef struct {
  PCTreeNode *nodes; // 结点存储空间
  int  n, r; // 结点数和根的位置
} PCTree;
**********/
int PCTreeDepth(PCTree T) /* 求树T的深度 */
{
    int ans=0;
    int i;
    int res,cur;
    ChildNode *p;
    if(T.n==1)return 1;
    for(i=0; iwhile(p!=NULL)
        {
            T.nodes[p->childIndex].parent=i;
            p=p->nextChild;
        }
    }
    for(i=0; i1;
        cur=T.nodes[i].parent;
        printf("cur=%d\n",cur);
        while(cur!=T.r)
        {
            res++;
            cur=T.nodes[cur].parent;
        }
        if(res>ans)ans=res;
    }
    return ans+1;
}

/**********
【题目】试编写算法,对以孩子-兄弟链表表示的树计算深度。
孩子兄弟链表类型定义:
typedef struct CSTNode {
  TElemType  data;
  struct CSTNode  *firstChild, *nextSibling;
} CSTNode, *CSTree;
**********/
int TreeDepth(CSTree T)
/* 求树T的深度 */
{
    if(T==NULL)return 0;
    int d=1;
    int x=1;
    if(T->firstChild)
    {
        d+=TreeDepth(T->firstChild);
    }
    if(T->nextSibling)
    {
        x=TreeDepth(T->nextSibling);
    }
    return d>x?d:x;
}

/**********
【题目】已知一棵树的由根至叶子结点按层次输出的
结点序列及每个结点的度。试编写算法,构造此树的
孩子兄弟链表。
孩子兄弟链表类型定义:
typedef struct CSTNode {
  TElemType  data;
  struct CSTNode  *firstChild, *nextSibling;
} CSTNode, *CSTree;
**********/
struct xxx
{
    int l,r;
} seg[100];
int i,j,k,n,last,cnt,res,nl,nr;
CSTNode* a[25];
CSTree p;
void debug(int n)
{
    int i;
    for(i=0; i<=n; i++)
    {
        printf("l=%d r=%d\n",seg[i].l,seg[i].r);
    }
}
void match(int x,int y,char xx,char yy)
{
    int i;
    int posx,posy;
    for(i=0; iif(a[i]->data==xx)
        {
            posx=i;
        }
        if(a[i]->data==yy)
        {
            posy=i;
        }
    }
    a[posx]->nextSibling=a[posy];
}
void match2(int x,int y,char xx,char yy)
{
    int i;
    int posx,posy;
    for(i=0; iif(a[i]->data==xx)
        {
            posx=i;
        }
        if(a[i]->data==yy)
        {
            posy=i;
        }
    }
    a[posx]->firstChild=a[posy];
}
void BuildCSTree(CSTree &T, char *node, int *degree)
/* 由结点的层序序列node和各结点的度degree构造树的孩子兄弟链表T */
{
    for(n=0; node[n]; n++)
    {
        a[n]=(CSTNode*)malloc(sizeof(CSTNode));
        a[n]->data=node[n];
        a[n]->firstChild=a[n]->nextSibling=NULL;
        //printf("n=%d %c\n",n,a[n]->data);
    }
    if(n==1)
    {
        T=a[0];
        return;
    }
    seg[0].l=seg[0].r=0;
    last=0;
    cnt=1;
    res=degree[0];
    while(1)
    {
        seg[cnt].l=seg[cnt-1].r+1;
        seg[cnt].r=seg[cnt-1].r+res;
        res=0;
        for(i=seg[cnt].l; i<=seg[cnt].r; i++)
        {
            res+=degree[i];
        }
        if(seg[cnt].r==n-1)break;
        cnt++;
    }
    debug(cnt);
    for(i=0; i<=cnt; i++)
    {
        if(i0;
            for(j=seg[i].l; j<=seg[i].r; j++)
            {
                for(k=seg[i+1].l+res; k1].l+res+degree[j]-1; k++)
                {
                    printf("match %d -> %d \n",k,k+1);
                    match(k,k+1,node[k],node[k+1]);
                }
            }
        }
        if(i0;
            for(j=seg[i].l; j<=seg[i].r; j++)
            {
                if(degree[j]==0)continue;
                printf("match2 %d -> %d \n",j,seg[i+1].l+res);
                match2(j,seg[i+1].l+res,node[j],node[seg[i+1].l+res]);
                res+=degree[j];
            }
        }
    }
    //T=a[0];
    for(i=0; i"n=%d i=%d %c\n",n,i,a[i]->data);
        if(a[i]->data == node[0])
        {
            T=a[i];
        }
    }
    for(i=0; i"%c ",a[i]->data);
    }
    printf("\n");
    printf("********************\n");
    return;
}


/**********
【题目】试编写非递归算法,实现并查集带路径压缩的
查找操作。
并查集的类型定义如下:
typedef struct {
  int *parent;
  int  n;
} MFSet;
**********/
int find(MFSet S, int i)
/* 并查集带路径压缩的查找的非递归实现 */
{
    int x=i,j;
    if(i<0||i>=S.n)
    {
        return -1;
    }
    while(S.parent[x]>=0)
    {
        x=S.parent[x];
    }
    while(S.parent[i]>=0)
    {
        j=i;
        i=S.parent[i];
        S.parent[j]=x;
    }
    return x;
}

/**********
【题目】编写算法,创建有向图的邻接数组存储结构。
图的邻接数组存储结构的类型定义如下:
#define UNVISITED  0
#define VISITED    1
#define INFINITY MAXINT // 计算机允许的整数最大值,即∞
typedef char VexType;
typedef enum {DG,DN,UDG,UDN} GraphKind; // 有向图,有向网,无向图,无向网
typedef struct {
  VexType *vexs; // 顶点数组
  int **arcs; // 关系数组,对无权图,用0或1表示相邻否,
              // 对带权图,则为权值或INFINITY
  int n, e;   // 顶点数和边(弧)数
  GraphKind kind; // 图的类型
  int *tags;  // 标志数组,可用于在图的遍历中标记顶点访问与否
} MGraph; // 邻接数组类型
typedef struct {
  VexType v, w;
  int info;
} ArcInfo;
可调用以下基本操作:
Status InitGraph(MGraph &G, GraphKind kind, int n);
  // 初始化含n个顶点空间的kind类的空图G
int LocateVex(MGraph G, VexType v); // 查找顶点v在图G中的位序
**********/
Status CreateDG(MGraph &G, VexType *vexs, int n,
                ArcInfo *arcs, int e)
/* 创建含n个顶点和e条边的有向图G,vexs为顶点信息,arcs为边信息 */
{
    int i,j,k;
    if(OVERFLOW==InitGraph(G,G.kind,n))return ERROR;
    VexType v,w;
    printf("n=%d e=%d\n",n,e);
    G.n=n;
    G.e=e;
    for(i=0; i"%d %c\n",i,vexs[i]);
    }
    for(k=0; k"v=%c w=%c\n",v,w);
        i=LocateVex(G,v);
        j=LocateVex(G,w);
        printf("i=%d j=%d\n",i,j);
        if(i<0||j<0)
        {
            return ERROR;
        }
        G.arcs[i][j].adj=1;
        //G.arcs[i][j].info=1;
    }
    return OK;
}


/**********
【题目】编写算法,计算以邻接表方式存储的有向图G中k顶点的出度。
图的邻接表存储结构的类型定义如下:
#define UNVISITED  0
#define VISITED    1
#define INFINITY MAXINT // 计算机允许的整数最大值,即∞
typedef char VexType;
typedef enum {DG,DN,UDG,UDN} GraphKind; // 有向图,有向网,无向图,无向网
typedef struct AdjVexNode {
  int adjvex;  // 邻接顶点在顶点数组中的位序
  struct AdjVexNode *next; // 指向下一个邻接顶点(下一条边或弧)
  int info;    // 存储边(弧)相关信息,对于非带权图可不用
} AdjVexNode, *AdjVexNodeP; // 邻接链表的结点类型
typedef struct VexNode {
  VexType data;    // 顶点值,VexType是顶点类型,由用户定义
  struct AdjVexNode *firstArc; // 邻接链表的头指针
} VexNode; // 顶点数组的元素类型
typedef struct {
  VexNode *vexs;  // 顶点数组,用于存储顶点信息
  int n, e;       // 顶点数和边(弧)数
  GraphKind kind; // 图的类型
  int *tags;      // 标志数组
} ALGraph;  // 邻接表类型
**********/
int outDegree(ALGraph G, int k)
/* 求有向图G中k顶点的出度。若k顶点不存在,则返回-1 */
{
    int cnt=0;
    AdjVexNodeP p=G.vexs[k].firstArc;
    if(k<0||k>G.n-1)return -1;
    while(p!=NULL)
    {
        cnt++;
        p=p->next;
    }
    return cnt;
}

/**********
【题目】编写算法,计算以邻接表方式存储的有向图G中
k顶点的入度。
图的邻接表存储结构的类型定义如下:
#define UNVISITED  0
#define VISITED    1
#define INFINITY MAXINT // 计算机允许的整数最大值,即∞
typedef char VexType;
typedef enum {DG,DN,UDG,UDN} GraphKind; // 有向图,有向网,无向图,无向网
typedef struct AdjVexNode {
  int adjvex;  // 邻接顶点在顶点数组中的位序
  struct AdjVexNode *next; // 指向下一个邻接顶点(下一条边或弧)
  int info;    // 存储边(弧)相关信息,对于非带权图可不用
} AdjVexNode, *AdjVexNodeP; // 邻接链表的结点类型
typedef struct VexNode {
  VexType data;    // 顶点值,VexType是顶点类型,由用户定义
  struct AdjVexNode *firstArc; // 邻接链表的头指针
} VexNode; // 顶点数组的元素类型
typedef struct {
  VexNode *vexs;  // 顶点数组,用于存储顶点信息
  int n, e;       // 顶点数和边(弧)数
  GraphKind kind; // 图的类型
  int *tags;      // 标志数组
} ALGraph;  // 邻接表类型
**********/
int inDegree(ALGraph G, int k)
/* 求有向图G中k顶点的入度。若k顶点不存在,则返回-1 */
{
    int cnt[100];
    int i;
    AdjVexNodeP p;
    //if(k<0||k>G.n-1)return -1;
    memset(cnt,0,sizeof(cnt));
    for(i=0; iwhile(p!=NULL)
        {
            cnt[p->adjvex]++;
            p=p->next;
        }
    }
    return cnt[k];
}

/**********
【题目】编写算法,创建有向图的邻接表存储结构。
图的邻接表存储结构的类型定义如下:
#define UNVISITED  0
#define VISITED    1
#define MAX_VEX_NUM  4
#define INFINITY MAXINT // 计算机允许的整数最大值,即∞
typedef char VexType;
typedef enum {DG,DN,UDG,UDN} GraphKind; // 有向图,有向网,无向图,无向网
typedef struct AdjVexNode {
  int adjvex;  // 邻接顶点在顶点数组中的位序
  struct AdjVexNode *next; // 指向下一个邻接顶点(下一条边或弧)
  int info;    // 存储边(弧)相关信息,对于非带权图可不用
} AdjVexNode, *AdjVexNodeP; // 邻接链表的结点类型
typedef struct VexNode {
  VexType data;    // 顶点值,VexType是顶点类型,由用户定义
  struct AdjVexNode *firstArc; // 邻接链表的头指针
} VexNode; // 顶点数组的元素类型
typedef struct {
  VexNode vexs[MAX_VEX_NUM];  // 顶点数组,用于存储顶点信息
  int n, e;       // 顶点数和边(弧)数
  GraphKind kind; // 图的类型
  int *tags;      // 标志数组
} ALGraph;  // 邻接表类型

可调用以下基本操作:
int LocateVex(ALGraph G, VexType v); // 查找顶点v在图G中的位序
**********/
Status CreateDG(ALGraph &G, VexType *vexs, int n,
                ArcInfo *arcs, int e)
/* 创建含n个顶点和e条边的有向图G,vexs为顶点信息,arcs为边信息 */
{
    int i,j,k;
    VexType v,w;
    AdjVexNodeP p;
    G.n=n;
    G.e=e;
    //G.vexs=(VexNode*)malloc(sizeof(VexNode)*n);
    G.tags=(int*)malloc(sizeof(int)*n);
    for(i=0; ifor(k=0; kif(i<0||j<0)
        {
            return ERROR;
        }
        p=(AdjVexNode*)malloc(sizeof(AdjVexNode));
        if(NULL==p)
        {
            return OVERFLOW;
        }
        p->adjvex=j;
        p->next=G.vexs[i].firstArc;
        G.vexs[i].firstArc=p;
    }
    return OK;
}

/**********
【题目】编写算法,创建无向图的邻接表存储结构。
图的邻接表存储结构的类型定义如下:
#define UNVISITED  0
#define VISITED    1
#define MAX_VEX_NUM  4
#define INFINITY MAXINT // 计算机允许的整数最大值,即∞
typedef char VexType;
typedef enum {DG,DN,UDG,UDN} GraphKind; // 有向图,有向网,无向图,无向网
typedef struct AdjVexNode {
  int adjvex;  // 邻接顶点在顶点数组中的位序
  struct AdjVexNode *next; // 指向下一个邻接顶点(下一条边或弧)
  int info;    // 存储边(弧)相关信息,对于非带权图可不用
} AdjVexNode, *AdjVexNodeP; // 邻接链表的结点类型
typedef struct VexNode {
  VexType data;    // 顶点值,VexType是顶点类型,由用户定义
  struct AdjVexNode *firstArc; // 邻接链表的头指针
} VexNode; // 顶点数组的元素类型
typedef struct {
  VexNode vexs[MAX_VEX_NUM]; //*vexs; 顶点数组,用于存储顶点信息
  int n, e;       // 顶点数和边(弧)数
  GraphKind kind; // 图的类型
  int *tags;      // 标志数组
} ALGraph;  // 邻接表类型

可调用以下基本操作:
int LocateVex(ALGraph G, VexType v); // 查找顶点v在图G中的位序
**********/
Status CreateUDG(ALGraph &G, VexType *vexs, int n,
                 ArcInfo *arcs, int e)
/* 创建含n个顶点和e条边的无向图G,vexs为顶点信息,arcs为边信息 */
{
    int i,j,k;
    VexType v,w;
    AdjVexNodeP p;
    G.n=n;
    G.e=e;
    //G.vexs=(VexNode*)malloc(sizeof(VexNode)*n);
    G.tags=(int*)malloc(sizeof(int)*n);
    for(i=0; ifor(k=0; kif(i<0||j<0)
        {
            return ERROR;
        }
        p=(AdjVexNode*)malloc(sizeof(AdjVexNode));
        if(NULL==p)
        {
            return OVERFLOW;
        }
        p->adjvex=j;
        p->next=G.vexs[i].firstArc;
        G.vexs[i].firstArc=p;
        p=(AdjVexNode*)malloc(sizeof(AdjVexNode));
        if(NULL==p)
        {
            return OVERFLOW;
        }
        p->adjvex=i;
        p->next=G.vexs[j].firstArc;
        G.vexs[j].firstArc=p;
    }
    return OK;
}

/**********
【题目】编写算法,在图G中,相对于k顶点的当前
邻接顶点m顶点,求下一个邻接顶点。
图的邻接数组存储结构的类型定义如下:
#define UNVISITED  0
#define VISITED    1  
#define MAX_VEX_NUM  4
#define INFINITY MAXINT // 计算机允许的整数最大值,即∞
typedef int VRType;
typedef char InfoType;
typedef char VexType;
typedef enum {DG,DN,UDG,UDN} GraphKind; // 有向图,有向网,无向图,无向网
typedef struct {
    VRType adj; // 顶点关系类型。对无权图,用1(是)或0(否)表示相邻否;
                // 对带权图,则为权值类型
    InfoType *info; // 该弧相关信息的指针(可无)
}ArcCell;//,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct {
  ArcCell arcs[MAX_VEX_NUM][MAX_VEX_NUM]; // 关系数组
  VexType vexs[MAX_VEX_NUM]; // 顶点数组
  int n, e;   // 顶点数和边(弧)数
  GraphKind kind; // 图的类型
} MGraph; // 邻接数组类型
**********/
int NextAdjVex(MGraph G, int k, int m)
/* 在图G中,相对于k顶点的当前邻接顶点m顶点,求下一个邻接顶点 */
{
    //直接跑
}

/**********
【题目】编写算法,在图G中置顶点v到顶点w的弧或边。
图的邻接数组存储结构的类型定义如下:
#define UNVISITED  0
#define VISITED    1  
#define MAX_VEX_NUM  4
#define INFINITY MAXINT // 计算机允许的整数最大值,即∞
typedef int VRType;
typedef char InfoType;
typedef char VexType;
typedef enum {DG,DN,UDG,UDN} GraphKind; // 有向图,有向网,无向图,无向网
typedef struct {
    VRType adj; // 顶点关系类型。对无权图,用1(是)或0(否)表示相邻否;
                // 对带权图,则为权值类型
    InfoType *info; // 该弧相关信息的指针(可无)
}ArcCell;//,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct {
  ArcCell arcs[MAX_VEX_NUM][MAX_VEX_NUM]; // 关系数组
  VexType vexs[MAX_VEX_NUM]; // 顶点数组
  int n, e;   // 顶点数和边(弧)数
  GraphKind kind; // 图的类型
} MGraph; // 邻接数组类型
可调用以下基本操作:
int LocateVex(MGraph G, VexType v); // 查找顶点v在图G中的位序
**********/
Status SetArc(MGraph &G, VexType v, VexType w, ArcCell info)
/* 在图G中置顶点v到顶点w的弧或边 */
{
    int vv,ww;
    vv=LocateVex(G,v);
    ww=LocateVex(G,w);
    if(vv<0||ww<0||vv==ww)return ERROR;
    G.arcs[vv][ww].adj=1;
    return OK;
}

你可能感兴趣的:(Something)