}seqlist;
voidcreate(seqlist*L)*建立一个顺序存储的线性表*
{
inti;
printf("Pleaseinputthelenoftheseqlist:
");*从键盘输入当前顺序表的实际长度*
scanf("%d",&(*L).len);
printf("Pleaseinputtheeveryelementoftheseqlist:
");
for(i=0;i<(*L).len;i++)*从键盘输入顺序表的每个元素*
scanf("%d",&(*L).list[i]);
printf("Outputtheeveryelementoftheseqlist:
\n");
for(i=0;i<(*L).len;i++)*顺序表建立成功后,输出整个顺序表*
printf("%5d",(*L).list[i]);*运行结果
(1)*
printf("\n");
}
voidaccess(seqlist*L,inti)*根据指定位置访问线性表*
{
if((i<0)||(i>(*L).len-1))*判断给定位置是否为合法取值*
printf("Theplaceisnotcorrect!
\n");
else
printf("%d\n",(*L).list[i]);*运行结果
(2)*
}
voidbefore_after(seqlist*L,inti)*根据指定位置寻找其前趋元素和后继元素*
{
if((i<0)||(i>(*L).len-1))
printf("Theplaceisnotcorrect!
\n");
elseif(i==0)
printf("%d\n",(*L).list[i+1]);
elseif(i==(*L).len-1)
printf("%d\n",(*L).list[i-1]);
elseif((i>0)&&(i<(*L).len-1))
printf("%d,%d\n",(*L).list[i-1],(*L).list[i+1]);*运行结果(3)*
}
voidsearch(seqlist*L,intkey)*根据给定元素key查找顺序表*
{
intm;
for(m=0;m<(*L).len;m++)
{
if((*L).list[m]!
=key)
continue;
else
{
printf("Seachingisseccessful!
\n");
printf("Theplaceofthesearchkeyis:
%d\n",m);
break;
}
}
if(m>=(*L).len)
printf("Theseqlist");*运行结果(4)*
}
voiddelete(seqlist*L,inti)*删除顺序表的元素*
{
intm;
printf("Outputtheeveryelementoftheseqlistbeforedeleting:
\n");*删除前输出顺序表中的所有元素*
for(m=0;m<(*L).len;m++)
printf("%5d",(*L).list[m]);*运行结果(5)*
printf("\n");
for(m=i;m<(*L).len;m++)*删除位置之后的所有元素依次左移一位*
(*L).list[m]=(*L).list[m+1];
(*L).len--;
printf("Outputtheeveryelementoftheseqlistafterdeleting:
\n");*删除后输出顺序表中的所有元素*
for(m=0;m<(*L).len;m++)
printf("%5d",(*L).list[m]);*运行结果(6)*
printf("\n");
}
voidinsert(seqlist*L,inti,inte)*在顺序表指定位置i后插入元素e*
{
intm;
printf("Outputtheeveryelementoftheseqlistbeforeinserting:
\n");*插入前输出顺序表中的所有元素*
for(m=0;m<(*L).len;m++)
printf("%5d",(*L).list[m]);*运行结果(7)*
printf("\n");
(*L).len++;
for(m=(*L).len-1;m>=i;m--)*插入位置之后的元素依次右移一位*
(*L).list[m+1]=(*L).list[m];
(*L).list[i]=e;*在指定位置i上插入元素e*
printf("Outputtheeveryelementoftheseqlistafterinserting:
\n");*插入后输出顺序表中的所有元素*
for(m=0;m<(*L).len;m++)
printf("%5d",(*L).list[m]);*运行结果(8)*
printf("\n");
}
voidmain()
{
staticseqlist*L;
inti,e;
printf("\nfunction:
create\n");
create(L);*调用函数create建立一个顺序表*
printf("\nfunction:
access\n");
printf("Pleaseinputtheposition:
");
scanf("%d",&i);*从键盘输入访问位置i*
access(L,i);*调用函数access根据指定位置访问顺序表*
printf("\nfunction:
before_after\n");
printf("Pleaseinputtheposition:
");
scanf("%d",&i);*从键盘输入指定位置i*
before_after(L,i);*调用函数before_after根据位置i确定前趋元素和后继元素*
printf("\nfunction:
search\n");
printf("Pleaseinputthesearchkey:
");
scanf("%d",&e);*从键盘输入查找元素e*
search(L,e);*调用函数search根据关键字e查找顺序表*
printf("\nfunction:
delete\n");
printf("Pleaseinputthedeleteposition:
");
scanf("%d",&i);*从键盘输入删除位置i*
delete(L,i);*调用函数delete删除指定位置i的元素*
printf("\nfunction:
insert\n");
printf("Pleaseinputtheinsertposition:
");
scanf("%d",&i);*从键盘输入插入位置i*
printf("Pleaseinputtheinsertelement:
");
scanf("%d",&e);*从键盘输入插入元素e*
insert(L,i,e);*调用函数insert在插入位置i上插入元素e*
}
(2)上机调试上面的源程序,并根据下列原始数据记录程序的运行结果。
原始数据
顺序表的实际长度
10
顺序表的元素
1,2,3,4,5,6,7,8,9,10
访问顺序表的位置i
5
寻找前趋、后继元素的指定位置i
7
查找关键字e
58
删除位置i
5
插入位置i
7
插入元素e
28
(3)运行结果记录。
①Outputtheeveryelementoftheseqlist:
12345678910
②6
③7,9
Theseqlist;*定义线性表的实际长度*
}seqlist;
voidcreate(seqlist*L)*根据已知条件建立一个有序的顺序表*
{
inti;
printf("\n\nPleaseinputthelengthoftheorderedseqlist:
");
**********blank**********
scanf("%d",&(*L).len);*从键盘输入有序表的实际长度,已知(*L).len=10*
printf("\n\nPleaseinputtheeveryelementoftheorderedseqlist:
");
for(i=0;i<(*L).len;i++)
**********blank**********
scanf("%d",&(*L).list[i]);
*从键盘输入有序表中的每个元素,{9}*
}
voidinsert(seqlist*L,inte)*在顺序表指定位置i后插入元素e*
{
inti,j,m,n;
printf("\n\nOutputtheeveryelementoftheseqlistbeforeinserting:
\n");*插入前输出顺序表中的所有元素*
for(n=0;n<(*L).len;n++)
printf("%5d",(*L).list[n]);
printf("\n");
(*L).len++;
for(i=(*L).len-2;i>=0;i--)
**********blank**********
If(e<(*L).list[i])*确定插入位置*
{
**********blank**********
(*L).list[i+1]=(*L).list[i];*插入位置之后的元素依次右移一位*
j=i;
}
**********blank**********
(*L).list[j]=e;*在确定的插入位置j上插入元素e*
printf("\n\nOutputtheeveryelementoftheseqlistafterinserting:
\n");*插入后输出顺序表中的所有元素*
for(n=0;n<(*L).len;n++)
printf("%5d",(*L).list[n]);
printf("\n\n");
}
voidmain()
{
staticseqlist*L;
inte=25;
**********blank**********
create(L);*调用函数create建立有序表*
**********blank**********
insert(L,e);*调用函数insert在有序表中插入元素e*
}
3.实现单链表的各种基本操作。
(1)源程序代码。
#include"stdio.;
DataTypee;
H=(NODEPTR)malloc(LEN);*为头结点H分配存储空间*
H->next=NULL;*从头结点开始建立单链表*
q=H;
printf("Pleaseinputthelengthofthelinklist:
");*根据实际情况从键盘输入表长*
scanf("%d",&n);
printf("Pleaseinputtheeveryelement:
");*从键盘输入线性表的每个元素*
for(i=1;i<=n;i++)
{
p=(NODEPTR)malloc(LEN);*为当前插入链表的结点分配存储空间*
scanf("%d",&e);
p->data=e;*指向结点的指针变量p指向当前要插入的结点*
q->next=p;*指向结点的指针变量q指向当前插入结点的前趋结点,将p作为其后继赋值,完成当前结点的插入*
q=p;*当前结点完成插入之后,修改指针变量q,为下一次插入做好准备*
}
q->next=NULL;
returnH;
}
voidsearch(NODEPTRH,DataTypee)*在单链表中查找指定数据元素e*
{
NODEPTRp;
inti=1;
p=H->next;*指针变量p赋值为头结点的后继,从该位置开始查找整个单链表*
while(p)*当p不为空时,执行循环*
{
if(p->data!
=e)*指针变量p当前指向结点的数据域不等于查找元素e*
{
p=p->next;*指针后移,继续向下查找*
i++;
continue;
}
else*否则,查找成功,输出该结点的位置*
{
printf("Searchingissuccessful!
\n");
printf("Theplaceofthesearchkeyinthememoryis:
%d\n",p);
*输出该结点在内存中的存储地址,
(2)*
printf("Theplaceofthesearchkeyinthelistis:
%d\n",i);
*输出该结点在线性表中的位置,(3)*
break;
}
}
if(p->next==NULL)
printf("Thelinklist");
}
NODEPTRdelposi(NODEPTRH,inti)*按照指定位置i删除链表中的某个元素*
{
NODEPTRp,pre;
intm;
p=H;
for(m=1;m<=i;m++)*按照指定位置i寻找需要删除的结点*
{
pre=p;
p=p->next;
}
pre->next=p->next;*删除指定位置的结点*
free(p);*结点删除后,释放存储空间*
returnH;
}
NODEPTRinsafter(NODEPTRH,inti,DataTypee)*在指定位置i之后插入元素e*
{
NODEPTRp,s;
intm;
s=(NODEPTR)malloc(LEN);*为插入结点分配存储空间*
s->data=e;*指针变量s指向当前的插入结点*
p=H;
for(m=0;m
p=p->next;
s->next=p->next;
p->next=s;*完成指针的修改,实现结点插入*
returnH;
}
main()
{
NODEPTRH,p;
inti,e;
printf("\nFuntion:
createback\n");
H=createback(H);*调用createback函数从链尾建立单链表*
printf("\nOutputthelinklist:
\n");*单链表建立成功后,输出单链表的所有结点*
p=H->next;
while(p)
{
printf("%d,%d",p->data,p->next);
p=p->next;
}*
(1)*
printf("\n");
printf("\nFuntion:
search\n");
printf("Pleaseinputthesearchkey:
");*从键盘输入查找元素e*
scanf("%d",&e);
search(H,e);*调用search函数在单链表中查找关键字e*
printf("\nFuntion:
delposi\n");
printf("\nOutputthelinklistbeforedeleting:
\n");*删除之前输出单链表的结点*
p=H->next;