数据结构上机源程序.docx
《数据结构上机源程序.docx》由会员分享,可在线阅读,更多相关《数据结构上机源程序.docx(44页珍藏版)》请在冰豆网上搜索。
数据结构上机源程序
7.设C={a1,b1,a2,b2,……,an,bn}为一线性表,采用带头结点的Hc单链表存放,编写一个就地算法,将其拆分成两个线性表,使得:
A={a1,a2,…..,an}C={b1,b2,….,bn}
#include"stdio.h"
#include"alloc.h"
typedefintElemType;
typedefstructnode
{ElemTypedata;
structnode*next;
}slink;
slink*creslink(intn)
{slink*head,*p,*s;
inti;
if(n<1)returnNULL;
p=head=(slink*)malloc(sizeof(slink));
for(i=1;i<=n;i++)
{s=(slink*)malloc(sizeof(slink));
scanf("%4d",&s->data);
p->next=s;
p=s;
}
p->next=NULL;
returnhead;
}
voidcaifen(slink*A,slink**B,slink**C)
{inti=1;
slink*p,*q,*r;
p=A->next;
q=*B=(slink*)malloc(sizeof(slink));
r=*C=(slink*)malloc(sizeof(slink));
while(p!
=NULL)
{if(i%2==0)
{q->next=p;
q=p;
p=p->next;
}
else
{r->next=p;
r=p;
p=p->next;
}
i++;}
q->next=NULL;
r->next=NULL;
}
voidlist(slink*head)
{slink*p;
p=head->next;
while(p!
=NULL)
{printf("%4d",p->data);
p=p->next;
}
printf("\n");
}
main()
{slink*a,*b,*c;
inta1=10;
clrscr();
a=creslink(a1);
caifen(a,&b,&c);
list(b);
list(c);
}_
08.编写出判断带头结点的双向循环链表L是否对称相等的算法(回文数)
#include"stdio.h"
#include"alloc.h"
typedefintElemType;
typedefstructnode
{ElemTypedata;
structnode*next;
structnode*prior;
}dlink;
dlink*initlist(intn)
{dlink*head,*p,*s;
inti;
if(n<1)returnNULL;
p=head=(dlink*)malloc(sizeof(dlink));
for(i=1;i<=n;i++)
{s=(dlink*)malloc(sizeof(dlink));
scanf("%4d",&s->data);
s->prior=p;
p->next=s;
p=s;
}
p->next=head;head->prior=p;
returnhead;
}
voidhuiwen(dlink*head)
{dlink*p,*q;
p=head->next;
q=head->prior;
while(p->next!
=head)
if(p->data==q->data)
{p=p->next;
q=q->prior;}
else{printf("itiswrong");break;}
if(p->next==head)
printf("Itishuiwen");
}
main()
{dlink*p;
intn=10;
clrscr();
p=initlist(n);
huiwen(p);
}_
09.设计一算法,将一带头结点的数据域依次为a1,a2,…,an(n>=3)的单链表的所有结点逆置。
#include"stdio.h"
#include"alloc.h"
typedefintElemType;
typedefstructnode
{ElemTypedata;
structnode*next;
}slink;
slink*creslink(intn)
{slink*head,*p,*s;
inti;
if(n<1)returnNULL;
p=head=(slink*)malloc(sizeof(slink));
for(i=1;i<=n;i++)
{s=(slink*)malloc(sizeof(slink));
scanf("%4d",&s->data);
p->next=s;
p=s;
}
p->next=NULL;
returnhead;
}
voidnizhi(slink*head)
{slink*p,*q,*r;
p=head->next;
while(p->next!
=NULL)
p=p->next;
r=head->next;
while(r!
=p)
{head->next=r->next;
r->next=p->next;
p->next=r;
r=head->next;
}
}
voidlist(slink*head)
{slink*p;
p=head->next;
while(p!
=NULL)
{printf("%4d",p->data);
p=p->next;
}
printf("\n");
}
main()
{slink*p;
intn=10;
clrscr();
p=creslink(n);
list(p);
nizhi(p);
list(p);
}_
10.设ha=(a1,a2,……,an)和hb=(b1,b2,……,bm)是两个带头结点的循环单链表,编写将这两个表合并为带头结点的循环单链表hc的算法。
#include"stdio.h"
#include"alloc.h"
typedefintElemType;
typedefstructnode
{ElemTypedata;
structnode*next;
}slink,*sl;
slink*creslink(intn)
{slink*p,*head,*s;
inti;
if(n<1)return0;
p=head=(slink*)malloc(sizeof(slink));
for(i=1;i<=n;i++)
{s=(slink*)malloc(sizeof(slink));
scanf("%4d",&s->data);
p->next=s;
p=s;
}
p->next=head;
returnhead;
}
voidlianjie(sl*A,sl*B,sl*C)
{slink*p,*q,*r,*k;
*C=(slink*)malloc(sizeof(slink));
q=*C;
for(p=(*B)->next;p!
=*B;p=p->next)
{q->next=p;
q=q->next;
}
for(r=(*A)->next;r!
=*A;r=r->next)
{q->next=r;
q=q->next;
}
q->next=*C;
}
voidlist(slink*head)
{slink*p;
p=head->next;
while(p!
=head)
{printf("%4d",p->data);
p=p->next;
}
printf("\n");
}
main()
{slp,q,r;
intn=5;
clrscr();
p=creslink(n);
list(p);
q=creslink(n);
list(q);
}
11.设A和B是两个单链表(带头结点),其表中元素递增有序。
试写一算法将A和B归并成一个按元素值递增有序的单链表C,要求辅助空间为O
(1),并分析算法的时间复杂度。
#include"stdio.h"
#include"alloc.h"
typedefintElemType;
typedefstructnode
{ElemTypedata;
structnode*next;
}slink;
slink*creslink(intn)
{slink*head,*p,*s;
inti;
if(n<1)returnNULL;
p=head=(slink*)malloc(sizeof(slink));
for(i=1;i<=n;i++)
{s=(slink*)malloc(sizeof(slink));
scanf("%4d",&s->data);
p->next=s;
p=s;
}
p->next=NULL;
returnhead;
}
voidlianjie(slink*A,slink*B,slink**C)
{slink*p,*q,*r;
*C=(slink*)malloc(sizeof(slink));
r=*C;
p=A->next;
q=B->next;
while(p!
=NULL&&q!
=NULL)
if(p->datadata)
{r->next=p;
r=p;
p=p->next;}
else
{r->next=q;
r=q;
q=q->next;}
if(p==NULL)
while(q!
=NULL)
{r->next=q;
r=q;
q=q->next;}
else
while(p!
=NULL)
{r->next=p;
r=p;
p=p->next;
}
r->next=NULL;
}
voidlist(slink*head)
{slink*p;
p=head->next;
while(p!
=NULL)
{printf("%4d",p->data);
p=p->next;
}
printf("\n");
}
main()
{slink*p,*q,*r;
intn=5;
clrscr();
p=creslink(n);
list(p);
q=creslink(n);
list(q);
lianjie(p,q,&r);
list(r);
}
printf("\n");
lianjie(&p,&q,&r);
list(r);
}
12.假设在长度大于1的单循环链表中,即无头结点指针也无首数据结点指针。
S为指向链表中某个结点的指针,试编写算法删除结点*S的直接前趋结点。
#include"stdio.h"
#include"alloc.h"
typedefintElemType;
typedefstructnode
{ElemTypedata;
structnode*next;
}slink;
slink*creslink(intn)
{slink*head,*p,*s;
inti;
if(n<1)returnNULL;
p=head=(slink*)malloc(sizeof(slink));
for(i=1;i<=n;i++)
{s=(slink*)malloc(sizeof(slink));
scanf("%4d",&s->data);
p->next=s;
p=s;
}
p->next=head;
returnhead;
}
intdelete(slink*head,ElemTypex)
{slink*p,*q;
for(p=head->next;p!
=head&&p->next->next->data!
=x;p=p->next);
if(p==head)return0;
q=p->next;
p->next=q->next;
free(q);
return1;
}
voidlist(slink*head)
{slink*p;
p=head->next;
while(p!
=head)
{printf("%4d",p->data);
p=p->next;
}
printf("\n");
}
main()
{slink*p;
intn=10,a=3;
clrscr();
p=creslink(n);
delete(p,a);
list(p);
}
13.已知单链表L(带头结点)是一个递增有序表,试写一高效算法,删除表中值大于min且小于max的结点(若表中有这样的结点),同时释放被删结点的空间,这里min和max是两个给定的参数。
#include"stdio.h"
#include"alloc.h"
typedefintElemType;
typedefstructnode
{ElemTypedata;
structnode*next;
}slink;
slink*creslink(intn)
{slink*head,*p,*s;
inti;
if(n<1)returnNULL;
p=head=(slink*)malloc(sizeof(slink));
for(i=1;i<=n;i++)
{s=(slink*)malloc(sizeof(slink));
scanf("%4d",&s->data);
p->next=s;
p=s;
}
p->next=NULL;
returnhead;
}
voiddelete(slink*head,intmin,intmax)
{slink*p,*q;
p=head;
while(p->next!
=NULL)
if(p->next->datanext->data>min)
{q=p->next;
p->next=q->next;
free(q);}
elsep=p->next;
}
voidlist(slink*head)
{slink*p;
p=head->next;
while(p!
=NULL)
{printf("%4d",p->data);
p=p->next;
}
printf("\n");
}
main()
{slink*p;
intn=10,a=1,b=5;
clrscr();
p=creslink(n);
list(p);
delete(p,a,b);
list(p);
}
14.编写一算法将单链(带头结点)中值重复的结点删除,使所得的结果表中各结点值均不相同。
#include"stdio.h"
#include"alloc.h"
typedefintElemType;
typedefstructnode
{ElemTypedata;
structnode*next;
}slink;
slink*creslink(intn)
{slink*head,*p,*s;
inti;
if(n<1)returnNULL;
p=head=(slink*)malloc(sizeof(slink));
for(i=1;i<=n;i++)
{s=(slink*)malloc(sizeof(slink));
scanf("%4d",&s->data);
p->next=s;
p=s;
}
p->next=NULL;
returnhead;
}
voiddelete(slink*head)
{slink*p,*q,*r;
q=head->next;
p=head->next;
while(q->next!
=NULL)
{p=q;
while(p->next!
=NULL)
if(p->next->data==q->data)
{r=p->next;
p->next=r->next;
free(r);}
elsep=p->next;
q=q->next;}
}
voidlist(slink*head)
{slink*p;
p=head->next;
while(p!
=NULL)
{printf("%4d",p->data);
p=p->next;
}
printf("\n");
}
main()
{slink*p;
intn=10;
clrscr();
p=creslink(n);
list(p);
delete(p);
list(p);}
15.有一递增单链表(允许出现值域重复的结点),设计一个算法删除值域重复的结点。
#include"stdio.h"
#include"alloc.h"
typedefintElemType;
typedefstructnode
{ElemTypedata;
structnode*next;
}slink;
slink*creslink(intn)
{slink*head,*p,*s;
inti;
if(n<1)returnNULL;
p=head=(slink*)malloc(sizeof(slink));
for(i=1;i<=n;i++)
{s=(slink*)malloc(sizeof(slink));
scanf("%4d",&s->data);
p->next=s;
p=s;
}
p->next=NULL;
returnhead;
}
voiddelete(slink*head)
{slink*p,*q,*r;
q=head->next;
p=head->next;
while(q->next!
=NULL)
{p=q;
while(p->next!
=NULL)
if(p->next->data==q->data)
{r=p->next;
p->next=r->next;
free(r);}
elsebreak;
q=q->next;}
}
voidlist(slink*head)
{slink*p;
p=head->next;
while(p!
=NULL)
{printf("%4d",p->data);
p=p->next;
}
printf("\n");
}
main()
{slink*p;
intn=10;
clrscr();
p=creslink(n);
list(p);
delete(p);
list(p);
}_
16.已知由单链表表示的线性表中,含有三类字符的数据元素(如:
字母字符,数字字符和其他字符),试编写算法构造三个以循环链表表示的线性表,使每个表中只含同一类的字符,且利用原表中的结点空间作为这三个表的结点空间,头结点可另开辟空间。
#include"stdio.h"
#include"alloc.h"
typedefcharElemType;
typedefstructnode
{ElemTypedata;
structnode*next;
}slink;
slink*creslink(intn)
{slink*head,*p,*s;
inti;
if(n<1)returnNULL;
p=head=(slink*)malloc(sizeof(slink));
for(i=1;i<=n;i++)
{s=(slink*)malloc(sizeof(slink));
s->data=getchar();
getchar();
p->next=s;
p=s;
}
p->next=NULL;
returnhead;
}
voidcaifen(slink*head,slink**A,slink**B,slink**C)
{slink*p,*q,*r,*s;
q=*A=(slink*)malloc(sizeof(slink));
r=*B=(slink*)malloc(sizeof(slink));
s=*C=(slink*)malloc(sizeof(slink));
for(p=head->next;p!
=NULL;p=p->next)
if(p->data>='1'&&p->data<='9'){q->next=p;q=p;}
elseif(p->data>='a'&&p->data<='z'||p->data>='A'&&p->data<='Z')
{r->next=p;r=p;}
else{s->next=p;s=p;}
q->next=*A;
r->next=*B;
s->next=*C;}
voidlist(slink*head)
{slink*p;
p=head->next;
while(p!
=head)
{printf("%4c",p->data);
p=p->next;
}
printf("\n");
}
main()
{slink*p,*q,*r,*s;
intn=10;
clrscr();
p=creslink(n);
caifen(p