#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;
}
#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