算法設計題彙總
one
統計出單鏈表中節點的值等於給定的值 X 的節點數
int Count(LNode *HL,ElemType x){
int i = 0;
LNode *p = HL;
while(p!=NULL){
if (P->data==x)
{
i++;
}
p=p->next;
}
return i;
}
two
排序:將一組無序的數劃分紅兩部分呢,前一部分均小於 k,後一部分均大於 k(快速排序的一趟)
void quickpass(int r[],int s,int t){
int i = s,j = t, x = r[s];
while(i<j){
while(i<j&&r[j]>x) j = j-1;
if(i<j){
r[i] = r[j];
j = j+1;
}
while(i<j&&r[i]<x) i =i+1;
if(i<j){
r[j] = r[i];
j = j-1;
}
}
r[i] = x;
}
鏈表求兩個集合的交集
typedef struct node
{
int data;
struct node *next;
}lklist;
void intersection(lklist *ha ,lklist *hb, lklist *&hc){
lklist *p,*q,*s;
for (p = ha,hc = 0;p!=0;p=p->next)
{
for(q = hb;q!=0;q = q->next){
if(q->data==p->data) break;
}
if(q!=0){
t = (lklist *)malloc(sizeof(lklist));
t->data = p->data;
t->next = hc;
hc = t;
}
}
}
three
設計在單鏈表中刪除相同的多餘節點的算法
typedef int datatype;
typedef struct node
{
datatype data;
struct node *next;
}lklist;
void delredundant(lklist *&head)
{
lklist *p,*q,*s;
for(p = head;p!=0;p=p->next)
{
for(q=p->next,s=q;q!=0)
if(q->data==p->data)
{
s->next=q->next;
free(q);
q=s->next;
}else{
s=q;
q=q->next;
}
}
}
設計求節點x在二叉樹中的雙親節點的算法
typedef struct node
{
datatype data;
struct node *lchild,*rchild;
}bitree;
bitree *q[20];
int r=0,f=0,flag=0;
void preorder(bitree *bt,char x)
{
if(bt!=0&&flag==0)
if(bt->data==x)
{
flag=1;
return;
}else{
r=(r+1)%20;
q[r] = bt;
preorder(bt->lchild);
preorder(bt->rchild);
}
}
void parent(bitree *bt,char x)
{
int i;
preorder(bt ,x);
for(i=f+1;i<=r;i++)
{
if(q[i]->lchild->data==x||q[i]->rchild->data) break;
}
if(flag==0) cout<<"not found\n";
else if(i<=r) cout<<bt->dataendl;
else cout<<"not parent\n";
}
four
鏈表分類存儲(統計結果分別存入三個鏈表中)
typedef char datatype;
typedef struct node
{
datatype data;
struct node *next;
}lklist;
void split(lklist *head,lklist *&ha,lklist *&hb,lklist *&hc)
{
lklist *p;ha=0,hb=0,hc=0;
for(p=head;p!=0;p=head)
{
head = p->next;
p->next=0;
if(p->data>='A'&&p->data<='Z')
{
p->next = ha;ha = p;
}else if(p->data>='0'&&p->data<='9'){
p->next=hb;
hb=p;
}else{
p->next=hc;
hc=p;
}
}
}
設計鏈表實現交換左右子樹
typedef struct node
{
int data;
struct node *lchild,*rchild;
}bitree;
void swapbitree(bitree *bt)
{
bitree *p;
if(bt == 0) return;
swapbitree(bt->lchild);
swapbitree(bt->rchild);
p=bt->lchild;
bt->lchild = bt->rchild;
bt->rchild = p;
}
鏈表實現創建二叉樹
#define n 10
typedef struct node
{
int key;
struct node *lchild,*rchild;
}bitree;
void bstinsert(bitree *&bt,int key)
{
if(bt==0)
{
bt=(bitree *)malloc(sizeof(bitree));
bt->key = key;
bt->lchild = bt->rchild=0;
}else if(bt->key>key){
bstinsert(bt->lchild,key)
}else{
bstinsert(bt->rchild,key);
}
}
void createbsttree(bitree *&bt)
{
int i;
for (int i = 0; i <= n; ++i)
{
bstinsert(bt,random(100));
}
}
five
判斷兩個二叉樹是否相同
typedef struct node
{
datatype data;
struct node *lchild,*rchild;
}bitree;
int judgebitree(bitree *bt1,bitree *bt2)
{
if(bt1==0&&bt2==0) return 1;
else if(bt1==0||bt2==0||bt1->data!=bt2->data)
{
return 0;
}else return(judgebitree(bt1->lchild,bt2->lchild)*judgebitree(bt1->rchild,bt2->rchild))
}
兩個有序單鏈表的合併算法
void mergelklist(lklist *ha,lklist *hb,lklist *&hc)
{
lklist *s = hc = 0;
while(ha!=0&&hb!=0)
{
if(ha->data<hb->data)
{
if(s==0) hc = s = ha;
else{
s->next = ha;
s=ha;
}
ha=ha->next;
}else{
if(s==0) hc = s = hb;
else{
s->next = hb;
s = hb;
}
hb = hb->next;
}
if(ha==0) s->next = hb;
else s->next = ha;
}
}