1、廣度優(yōu)先搜索二叉樹
typedef struct btree
{
int data;
btree *L;
btree *R;
}btree;
void BST(btree *Tree)
{
queue<btree*> q;
btree *p=Tree;
q.push(p);
while(!q.empty())
{
p=q.front();
q.pop();
printf("%d",p->data);
if(p->L)
q.push(p->L);
if(p->R)
q.push(p->R);
}
}
2、輸出一個(gè)字符串的所有排列(字符串可能有重復(fù)字符)
void search(char s[], int i, int n)
{
int j;
char temp;
for(j=0;j<n;++j)
{
if(j!=0 &&s[j]==s[j-1])
;
else if(s[j]!='@')
{
p[i]=s[j];
s[j]='@';
if(i==n-1)
{
p[n]='\0';
printf("%s\n", p);
}
else
{
search(s,i+1,n);
}
s[j]=p[i];
}
}
}
3、判斷一個(gè)鏈表是否有環(huán)
typedef struct node
{
int data;
node *next;
}node;
bool IsLoop(node *head)
{
node *p=head;
node *q=head->next;
node *tmp;
if(q->next==p)
return true;
while(p!=NULL&&q!=NULL&&p!=q)
{
p=p->next;
tmp=q->next;
q=tmp->next;
}
if(p==NULL||q==NULL)
return false;
else
return true;
}
4、一個(gè)0-n的數(shù)組的元素大小為0-n,判斷是否有重復(fù)
bool IfDuplicate(int *a,int n)
{
for(int i=0;i<n;i++)
{
while(a[i]!=i&&a[i]!=-1)
{
if(a[a[i]]==-1)
return true;
int j=a[i];
a[i]=a[a[i]];
a[j]=-1;
}
if(a[i]==i)
a[i]=-1;
}
return false;
}
|
posted on 2010-07-28 16:49
ccyy 閱讀(443)
評論(1) 編輯 收藏 引用