将以顺序表A中的元素逆置例如原来顺序表A中的元素是10.docx

上传人:b****3 文档编号:2817747 上传时间:2022-11-15 格式:DOCX 页数:24 大小:21.76KB
下载 相关 举报
将以顺序表A中的元素逆置例如原来顺序表A中的元素是10.docx_第1页
第1页 / 共24页
将以顺序表A中的元素逆置例如原来顺序表A中的元素是10.docx_第2页
第2页 / 共24页
将以顺序表A中的元素逆置例如原来顺序表A中的元素是10.docx_第3页
第3页 / 共24页
将以顺序表A中的元素逆置例如原来顺序表A中的元素是10.docx_第4页
第4页 / 共24页
将以顺序表A中的元素逆置例如原来顺序表A中的元素是10.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

将以顺序表A中的元素逆置例如原来顺序表A中的元素是10.docx

《将以顺序表A中的元素逆置例如原来顺序表A中的元素是10.docx》由会员分享,可在线阅读,更多相关《将以顺序表A中的元素逆置例如原来顺序表A中的元素是10.docx(24页珍藏版)》请在冰豆网上搜索。

将以顺序表A中的元素逆置例如原来顺序表A中的元素是10.docx

将以顺序表A中的元素逆置例如原来顺序表A中的元素是10

第二章线性表

1、将以顺序表A中的元素逆置。

例如原来顺序表A中的元素是100,90,80,70,60,50,40,逆置后为40,50,60,70,80,90,100。

算法所用的辅助空间要尽量可能地少。

用非形式算法描述,并编写C语言程序。

答:

描述:

该顺序表A有N个元素,分别将第1个与第N个对换,第2个与第N-1个对换,依此类推第i个与第N-i个对换。

C语言程序:

#include

#include

intmain(void)

{

charelem[100],t;

inti,n,t;

printf("Pleaseinputnumber(1~100):

");/*输入要输入的元素的个数*/

scanf("%d",&n);

printf("***************************************************************\n");

printf("Pleaseinputelement:

\n");/*输入元素*/

flushall();

for(i=0;i

{

scanf("%c",&elem[i]);

}

for(i=0;i

{

printf("%c",elem[i]);

}

printf("\n");

for(i=0;i

{

t=elem[i];

elem[i]=elem[n-i-1];

elem[n-i-1]=t;

}

for(i=0;i

{

printf("%c",elem[i]);

}

getch();

return0;

}

2、写一算法输出已知顺序表A中元素的最大值和次最大值。

用非形式算法描述,并编写C语言程序。

#include

#include

voidprintFstAndSndValue(SeqListsq)

{

intfirstmax=0;

intsecondmax=0;

inti=0;

if(sq.last==-1)

{

printf(“Listisempty!

”);

return;

}

firstmax=sq.data[0];

secondtmax=0;

for(i=1;i<=sq.last;++i)

{

if(firstmax

{

firstmax=sq.data[i];

}

elseif(secondmax

{

scondmax=sq.data[i];

}

}

printf(“%d%d”,firstmax,secondmax);

}

3、设一顺序表中元素值递增有序。

写一算法,将元素x插到表中适当地位置,并保持顺序表地有序性,且分析算法地时间复杂度。

算法的C语言实现:

int*Insert_SeqList(SeqList*L,datatypex)

{

inti,j,t=1;

for(i=0;ilast;++i)

{

if(L->data[i]

{

continue;

}

if(L->data[i]>=x)

{

for(j=L->last;j>i;--j)

{

L->data[j]=L->data[j-1];

}

L->data[i]=x;

t=0;

break;

}

}

if(t>0)

{

L->data[i+1]=x;

}

}

时间复杂度:

O(n)。

4、设有两个安元素值递增有序的顺序表A和B(单链表A和B),编一程序将A表和B表归并成一个新的递增有序的顺序表C(单链表),值相同的元素均保留在C表中。

C程序:

#include

#include

intmain(void)

{

intA[8]={1,3,4,6,8,12,34,37};

intB[9]={14,16,17,19,26,30,41,88,91};

intC[17];

inti=0;

intj=0;

intk=0;

printf("Aarray:

");

for(i=0;i<8;++i)

{

printf("%d",A[i]);

}

printf("\n");

printf("Barray:

");

for(j=0;j<9;++j)

{

printf("%d",B[j]);

}

printf("\n");

i=0;

j=0;

while((i<8)&&(j<9))

{

if(A[i]

{

C[k++]=A[i++];

}

elseif(B[j]

{

C[k++]=B[j++];

}

else

{

C[k++]=A[i++];

}

}

while(i<8)

{

C[k++]=A[i++];

}

while(j<9)

{

C[k++]=B[j++];

}

printf("Carray:

");

for(k=0;k<17;++k)

{

printf("%d",C[k]);

}

getch();

return0;

}

5、已知L是无表头结点的单链表,且p结点既不是首结点,试从下列提供的语句中选出合适的语句序列。

(1)在p结点后插入s结点:

/4/1。

(2)在p结点前插入s结点:

/8/12/9/4/1。

(3)在表首插入s结点:

/5。

(4)在表尾插入s结点:

/8/10/1/7。

/1p->next=s;

/2p->next=p->next->next;

/3p->next=s->next;

/4s->next=p->next;

/5s->next=L;

/6s->next=p;

/7s->next=NULL;

/8q=p;

/9while(p->next!

=Q){p=p->next;}

/10while(q->next!

=NULL){q=q->next;}

/11p=q;

/12p=L;

/13L=s;

/14L=p;

6、已知p结点是某双向链表的中间结点,试从下列提供的语句中选出合适的语句序列。

(1)在p结点后插入s结点:

/7/12/3/6

(2)在p结点前插入s结点:

/13/8/5/4

(3)删除p结点的直接后继结点:

q=p->next;p->next=q->next;q->next->prior=q->prior;free(q);

(4)删除p结点的直接前趋结点:

q=p->prior;p->prior=q->prior;

q->prior->next=q->next;free(q);

(5)删除p结点:

p->prior->next=p->next;p->next->prior=p->prior;free(p);

7、设有两个线性表A和B皆是单链表存储结构。

同一个表中的元素各不相同,且递增有序。

写一算法,构成一个新的线性表C,使C为A和B的交集,且C中的元素也递增有序。

voidnew(LinkListALinkListB)

{

LinkListC;

LNode*a,*b,*c,*ap,*bp;

a=A->next;

b=B->next;

c=C->next;

while((a->next!

=NULL)&&(b->next!

=NULL))

{

c=(LNode*)malloc(sizeof(LNode));

if(a->data<=b->data)

{

c->next=a;

c=c->next;

ap=a->next;

free(a);

a=ap;

}

else

{

c->next=b;

c=c->next;

bp=b->next;

free(b);

b=bp;

}

}

while(a->next!

=NULL)

{

c=(LNode*)malloc(sizeof(LNode));

c->next=a;

c=c->next;

ap=a->next;

free(a);

a=ap;

}

while(b->next!

=NULL)

{

c=(LNode*)malloc(sizeof(LNode));

c->next=b;

c=c->next;

bp=b->next;

free(b);

b=bp;

}

c->next=NULL;

return0;

}

8、删除线性表a中第i个元素起的k个元素。

StatusDeleteK(SeqList&a,inti,intk)

{

  if((i<1)||(k<0)||(i+k-1>a.length))

{

returnINFEASIBLE;

}

  for(count=1;i+count-1<=a.length-k;++count)/*注意循环结束的条件*/

  {

a.elem[i+count-1]=a.elem[i+count+k-1];

}

  a.length-=k;

  returnOK;

}/*DeleteK*/

9、把x插入递增有序表va中。

StatusInsert_SeqList(SeqList&va,intx)

{

  if(va.length+1>va.listsize)

{

returnERROR;

}

  ++va.length;

  for(i=va.length-1;va.elem[i]>x&&i>=0;--i)

{

    va.elem[i+1]=va.elem[i];

}

  va.elem[i+1]=x;

  returnOK;

}/*Insert_SqList*/

10、比较字符表A和B,并用返回值表示结果,值为正,表示A>B;值为负,表示A

intListComp(SeqListA,SeqListB)

{

  for(i=1;A.elem[i]||B.elem[i];++i)

    {

if(A.elem[i]!

=B.elem[i])

{

returnA.elem[i]-B.elem[i];

}

}

  return0;

}/*ListComp*/

11、在链表上的元素查找指定元素并返回指针

LNode*Locate(LinkListL,intx)

{

LNode*p;

  for(p=l->next;p

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1