求出数组中所有字符的全排列

#include  < stdio.h >
#include 
< malloc.h >
/*
 *求出数组中所有字符的组合
 *
 
*/

 typedef 
struct  Node {
     
char ch;
     
struct Node* next;

}
Node;
typedef 
struct {
    Node 
*head;
    Node 
*tail;
}
List;
typedef List
*  LinkedList;
typedef 
enum   bool {false,true} bool ;

// 检查链表是否为空
bool  isEmpty(LinkedList list) {
    
if(list->head==NULL)return true;
    
else return false;
}

// 在链表尾部添加结点
bool  add(LinkedList list, char  ch) {
    Node 
*pnode=(Node*)malloc(sizeof(Node));
    
if(!pnode)return false;
    pnode
->ch=ch;
    pnode
->next=NULL;
    
if(list->head==NULL){
        list
->head=pnode;
        list
->tail=pnode;
        
return true;
    }

    
else{
        list
->tail->next=pnode;
        list
->tail=pnode;
    }

}

// 打印链表
void  printList(LinkedList list) {
    Node
* pnode=list->head;
    
while(pnode!=NULL){
        printf(
"%c",pnode->ch);
        pnode
=pnode->next;
    }

    printf(
" ");
}

Node
*  delLast(LinkedList list) {
    Node
* before,*current;
    
if(list->head==NULL)return NULL;
    current
=list->head;
    
while(current!=list->tail){
        before
=current;
        current
=current->next;
    }

    
if(current==list->head){
        list
->tail=list->head=NULL;
        
return current;
    }

    list
->tail=before;
    list
->tail->next=NULL;
    
return current;    
}

 
void  zuhe(LinkedList result,LinkedList remain) {
      Node 
*last=NULL,*first=NULL,*current=NULL;                  
         
while(first==NULL||current!=first){
             
if(first==NULL){
                 first
=remain->head;
                 
if(first==NULL)return;
                 }

                 current
=remain->head;                 
                 
if(current==NULL)return;
                 
//printf("!!! %c ",current->ch);
             if(result->head==NULL){
                 result
->tail=result->head=current;                 
             }
else{
                 result
->tail->next=current;
                 result
->tail=current;
             }

             remain
->head=remain->head->next;             
             result
->tail->next=NULL;
             
             printList(result);             
             zuhe(result,remain);             
             last
=delLast(result);
             
if(last==NULL)return;
             
if(remain->head==NULL){
                 remain
->tail=remain->head=last;                 
             }
else{
                 remain
->tail->next=last;
                 remain
->tail=last;
             }

             remain
->tail->next=NULL;
             current
=remain->head;
       }

}

int  main ()
{
    LinkedList list;
    LinkedList list1;
    
int i;
  list
=(List*)malloc(sizeof(List));
  list
->head=NULL;
  list1
=(List*)malloc(sizeof(List));
  list1
->head=NULL;
  
for(i=48;i<51;++i){
      add(list,(
char)i);
  }

  printf(
" ");
  printf(
"参与排列的数 ");
  printList(list);
  zuhe(list1,list);
  
return 0;
}

 
程序运行结果:
参与排列的数    012
0
01
012
02
021
1
12
120
10
102
2
20
201
21
210
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值