用一个树实现,左边选,右边不选
#include<iostream.h>
#include<string.h>
#include<stdio.h>
typedef struct node
{
int data;
struct node *next;
}Node;
int ListInsert(Node *list,int k,int a)
{
//int len=ListLen(list);
Node *p,*q;
int i=0;
p=list;
while(p->next&&i<k)
{
i++;
p=p->next;
}
q=new Node[1];
q->data=a;
q->next=p->next;
p->next=q;
return 1;
}
int ListDelete(Node *list)
{
Node *p;
while(list->next)
{
p=list;
list=list->next;
}
p->next=NULL;
return 1;
}
int ListLen(Node *list)
{
int len=0;
while(list)
{
len++;
list=list->next;
}
return len;
}
void print(Node *list)
{
Node *p=list;
while(p)
{
if(p->data!=0)
printf("%d",p->data);
p=p->next;
}
}
int ListGetItem(Node *head,int i)
{
if(i>ListLen(head))
return -1;
int len=0;
while(head->next&&len+1<i)
{
len++;
head=head->next;
}
return head->data;
}
void GetPowerSet(int i,Node *a,Node *b)
{
if(i>ListLen(a))
{
print(b);
cout<<endl;
}
else
{
int x=ListGetItem(a,i);
int k=ListLen(b);
ListInsert(b,k+1,x);
GetPowerSet(i+1,a,b);
ListDelete(b);
GetPowerSet(i+1,a,b);
}
}
void main()
{
Node *head=new Node[1];
head->data=1;
head->next=NULL;
Node *b=new Node[1];
b->data='/0';
b->next=NULL;
ListInsert(head,2,2);
ListInsert(head,100,3);
GetPowerSet(1,head,b);
}