第二章线性表.docx

上传人:b****1 文档编号:14715242 上传时间:2023-06-26 格式:DOCX 页数:22 大小:19.54KB
下载 相关 举报
第二章线性表.docx_第1页
第1页 / 共22页
第二章线性表.docx_第2页
第2页 / 共22页
第二章线性表.docx_第3页
第3页 / 共22页
第二章线性表.docx_第4页
第4页 / 共22页
第二章线性表.docx_第5页
第5页 / 共22页
第二章线性表.docx_第6页
第6页 / 共22页
第二章线性表.docx_第7页
第7页 / 共22页
第二章线性表.docx_第8页
第8页 / 共22页
第二章线性表.docx_第9页
第9页 / 共22页
第二章线性表.docx_第10页
第10页 / 共22页
第二章线性表.docx_第11页
第11页 / 共22页
第二章线性表.docx_第12页
第12页 / 共22页
第二章线性表.docx_第13页
第13页 / 共22页
第二章线性表.docx_第14页
第14页 / 共22页
第二章线性表.docx_第15页
第15页 / 共22页
第二章线性表.docx_第16页
第16页 / 共22页
第二章线性表.docx_第17页
第17页 / 共22页
第二章线性表.docx_第18页
第18页 / 共22页
第二章线性表.docx_第19页
第19页 / 共22页
第二章线性表.docx_第20页
第20页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

第二章线性表.docx

《第二章线性表.docx》由会员分享,可在线阅读,更多相关《第二章线性表.docx(22页珍藏版)》请在冰点文库上搜索。

第二章线性表.docx

第二章线性表

  

第二章线性表

2.10

StatusDeleteK(SqList&a,inti,intk)//删除线性表a中第i个元素起的k个元素

{

  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

2.11

StatusInsert_SqList(SqList&va,intx)//把x插入递增有序表va中

{

  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

2.12

intListComp(SqListA,SqListB)//比较字符表A和B,并用返回值表示结果,值为1,表示A>B;值为-1,表示A

{

  for(i=1;i<=A.length&&i<=B.length;i++)

    if(A.elem[i]!

=B.elem[i])

      returnA.elem[i]>B.elem[i]?

1:

-1;

  if(A.length==B.length)return0;

  returnA.length>B.length?

1:

-1;    //当两个字符表可以互相比较的部分完全相同时,哪个较长,哪个就较大

}//ListComp

2.13

LNode*Locate(LinkListL,intx)//链表上的元素查找,返回指针

{

  for(p=l->next;p&&p->data!

=x;p=p->next);

  returnp;

}//Locate

2.14

intLength(LinkListL)//求链表的长度

{

  for(k=0,p=L;p->next;p=p->next,k++);

  returnk;

}//Length

2.15

voidListConcat(LinkListha,LinkListhb,LinkList&hc)//把链表hb接在ha后面形成链表hc

{

  hc=ha;p=ha;

  while(p->next)p=p->next;

  p->next=hb;

}//ListConcat

2.16

见书后答案.

2.17

StatusInsert(LinkList&L,inti,intb)//在无头结点链表L的第i个元素之前插入元素b

{

  p=L;q=(LinkList*)malloc(sizeof(LNode));

  q.data=b;

  if(i==1)

  {

    q.next=p;L=q;//插入在链表头部

  }

  else

  {

    while(--i>1)p=p->next;

    q->next=p->next;p->next=q;//插入在第i个元素的位置

  }

}//Insert

2.18

StatusDelete(LinkList&L,inti)//在无头结点链表L中删除第i个元素

{

  if(i==1)L=L->next;//删除第一个元素

  else

  {

    p=L;

    while(--i>1)p=p->next;

    p->next=p->next->next;//删除第i个元素

  }

}//Delete

2.19

StatusDelete_Between(Linklist&L,intmink,intmaxk)//删除元素递增排列的链表L中值大于mink且小于maxk的所有元素

{

  p=L;

  while(p->next->data<=mink)p=p->next;//p是最后一个不大于mink的元素

  if(p->next)    //如果还有比mink更大的元素

  {

    q=p->next;

    while(q->datanext;//q是第一个不小于maxk的元素

    p->next=q;

  }

}//Delete_Between

2.20

StatusDelete_Equal(Linklist&L)//删除元素递增排列的链表L中所有值相同的元素

{

  p=L->next;q=p->next;//p,q指向相邻两元素

  while(p->next)

  {

    if(p->data!

=q->data)

    {

      p=p->next;q=p->next;//当相邻两元素不相等时,p,q都向后推一步

    }

    else

    {

      while(q->data==p->data)

  {

    free(q);

    q=q->next;

  }

      p->next=q;p=q;q=p->next;//当相邻元素相等时删除多余元素

    }//else

  }//while

}//Delete_Equal

2.21

voidreverse(SqList&A)//顺序表的就地逆置

{

  for(i=1,j=A.length;i

    A.elem[i]<->A.elem[j];

}//reverse

2.22

voidLinkList_reverse(Linklist&L)//链表的就地逆置;为简化算法,假设表长大于2

{

  p=L->next;q=p->next;s=q->next;p->next=NULL;

  while(s->next)

  {

    q->next=p;p=q;

    q=s;s=s->next;//把L的元素逐个插入新表表头

  }

  q->next=p;s->next=q;L->next=s;

}//LinkList_reverse

分析:

本算法的思想是,逐个地把L的当前元素q插入新的链表头部,p为新表表头.

2.23

voidmerge1(LinkList&A,LinkList&B,LinkList&C)//把链表A和B合并为C,A和B的元素间隔排列,且使用原存储空间

{

  p=A->next;q=B->next;C=A;

  while(p&&q)

  {

    s=p->next;p->next=q;//将B的元素插入

    if(s)

    {

      t=q->next;q->next=s;//如A非空,将A的元素插入

    }

    p=s;q=t;

  }//while

}//merge1

2.24

voidreverse_merge(LinkList&A,LinkList&B,LinkList&C)//把元素递增排列的链表A和B合并为C,且C中元素递减排列,使用原空间

{

  pa=A->next;pb=B->next;pre=NULL;//pa和pb分别指向A,B的当前元素

  while(pa||pb)

  {

    if(pa->datadata||!

pb)

    {

      pc=pa;q=pa->next;pa->next=pre;pa=q;//将A的元素插入新表

    }

    else

    {

      pc=pb;q=pb->next;pb->next=pre;pb=q;//将B的元素插入新表

    }

    pre=pc;

  }

  C=A;A->next=pc;//构造新表头

}//reverse_merge

分析:

本算法的思想是,按从小到大的顺序依次把A和B的元素插入新表的头部pc处,最后处理A或B的剩余元素.

2.25

voidSqList_Intersect(SqListA,SqListB,SqList&C)//求元素递增排列的线性表A和B的元素的交集并存入C中

{

  i=1;j=1;k=0;

  while(A.elem[i]&&B.elem[j])

  {

    if(A.elem[i]

    if(A.elem[i]>B.elem[j])j++;

    if(A.elem[i]==B.elem[j])

    {

      C.elem[++k]=A.elem[i];//当发现了一个在A,B中都存在的元素,

      i++;j++;//就添加到C中

    }

  }//while

}//SqList_Intersect

2.26

voidLinkList_Intersect(LinkListA,LinkListB,LinkList&C)//在链表结构上重做上题

{

  p=A->next;q=B->next;

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

 C=pc;

  while(p&&q)

  {

    if(p->datadata)p=p->next;

    elseif(p->data>q->data)q=q->next;

    else

    {

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

      s->data=p->data;

      pc->next=s;pc=s;

      p=p->next;q=q->next;

    }

  }//while

}//LinkList_Intersect

2.27

voidSqList_Intersect_True(SqList&A,SqListB)//求元素递增排列的线性表A和B的元素的交集并存回A中

{

  i=1;j=1;k=0;

  while(A.elem[i]&&B.elem[j])

  {

    if(A.elem[i]

    elseif(A.elem[i]>B.elem[j])j++;

    elseif(A.elem[i]!

=A.elem[k])

    {

      A.elem[++k]=A.elem[i];//当发现了一个在A,B中都存在的元素

      i++;j++;//且C中没有,就添加到C中

    }

    else{i++;j++;}

  }//while

  while(A.elem[k])A.elem[k++]=0;

}//SqList_Intersect_True

2.28

voidLinkList_Intersect_True(LinkList&A,LinkListB)//在链表结构上重做上题

{

  p=A->next;q=B->next;pc=A;

  while(p&&q)

  {

    if(p->datadata)p=p->next;

    elseif(p->data>q->data)q=q->next;

    elseif(p->data!

=pc->data)

    {

      pc=pc->next;

      pc->data=p->data;

      p=p->next;q=q->next;

    }

  }//while

}//LinkList_Intersect_True

2.29

voidSqList_Intersect_Delete(SqList&A,SqListB,SqListC)

{

 i=0;j=0;k=0;m=0;   //i指示A中元素原来的位置,m为移动后的位置

 while(i

 {

   if(B.elem[j]

   elseif(B.elem[j]>C.elem[k])k++;

   else

   {

     same=B.elem[j];                  //找到了相同元素same

     while(B.elem[j]==same)j++;

     while(C.elem[k]==same)k++;    //j,k后移到新的元素

     while(i

       A.elem[m++]=A.elem[i++];           //需保留的元素移动到新位置

     while(i

   }

 }//while

 while(i

   A.elem[m++]=A.elem[i++];     //A的剩余元素重新存储。

 A.length=m;

}//SqList_Intersect_Delete

分析:

先从B和C中找出共有元素,记为same,再在A中从当前位置开始,凡小于same的

元素均保留(存到新的位置),等于same的就跳过,到大于same时就再找下一个same.

2.30

voidLinkList_Intersect_Delete(LinkList&A,LinkListB,LinkListC)//在链表结构上重做上题

{

  p=B->next;q=C->next;r=A-next;

  while(p&&q&&r)

  {

    if(p->datadata)p=p->next;

    elseif(p->data>q->data)q=q->next;

    else

    {

      u=p->data;//确定待删除元素u

      while(r->next->datanext;//确定最后一个小于u的元素指针r

      if(r->next->data==u)

      {

        s=r->next;

        while(s->data==u)

        {

          t=s;s=s->next;free(t);//确定第一个大于u的元素指针s

        }//while

        r->next=s;//删除r和s之间的元素

      }//if

      while(p->data=u)p=p->next;

      while(q->data=u)q=q->next;

    }//else

  }//while

}//LinkList_Intersect_Delete

2.31

StatusDelete_Pre(CiLNode*s)//删除单循环链表中结点s的直接前驱

{

  p=s;

  while(p->next->next!

=s)p=p->next;//找到s的前驱的前驱p

  p->next=s;

  returnOK;

}//Delete_Pre

2.32

StatusDuLNode_Pre(DuLinkList&L)//完成双向循环链表结点的pre域

{

  for(p=L;!

p->next->pre;p=p->next)p->next->pre=p;

  returnOK;

}//DuLNode_Pre

2.33

StatusLinkList_Divide(LinkList&L,CiList&A,CiList&B,CiList&C)//把单链表L的元素按类型分为三个循环链表.CiList为带头结点的单循环链表类型.

{

  s=L->next;

  A=(CiList*)malloc(sizeof(CiLNode));p=A;

  B=(CiList*)malloc(sizeof(CiLNode));q=B;

  C=(CiList*)malloc(sizeof(CiLNode));r=C;//建立头结点

  while(s)

  {

    if(isalphabet(s->data))

    {

      p->next=s;p=s;

    }

    elseif(isdigit(s->data))

    {

      q->next=s;q=s;

    }

    else

    {

      r->next=s;r=s;

    }

  }//while

  p->next=A;q->next=B;r->next=C;//完成循环链表

}//LinkList_Divide

2.34

voidPrint_XorLinkedList(XorLinkedListL)//从左向右输出异或链表的元素值

{

  p=L.left;pre=NULL;

  while(p)

  {

    printf("%d",p->data);

    q=XorP(p->LRPtr,pre);

    pre=p;p=q;//任何一个结点的LRPtr域值与其左结点指针进行异或运算即得到其右结点指针

  }

}//Print_XorLinkedList

2.35

StatusInsert_XorLinkedList(XorLinkedList&L,intx,inti)//在异或链表L的第i个元素前插入元素x

{

  p=L.left;pre=NULL;

  r=(XorNode*)malloc(sizeof(XorNode));

  r->data=x;

  if(i==1)//当插入点在最左边的情况

  {

    p->LRPtr=XorP(p.LRPtr,r);

    r->LRPtr=p;

    L.left=r;

    returnOK;

  }

  j=1;q=p->LRPtr;//当插入点在中间的情况

  while(++j

  {

    q=XorP(p->LRPtr,pre);

    pre=p;p=q;

  }//while//在p,q两结点之间插入

  if(!

q)returnINFEASIBLE;//i不可以超过表长

  p->LRPtr=XorP(XorP(p->LRPtr,q),r);

  q->LRPtr=XorP(XorP(q->LRPtr,p),r);

  r->LRPtr=XorP(p,q);//修改指针

  returnOK;

}//Insert_XorLinkedList

2.36

StatusDelete_XorLinkedList(XorlinkedList&L,inti)//删除异或链表L的第i个元素

{

  p=L.left;pre=NULL;

  if(i==1)//删除最左结点的情况

  {

    q=p->LRPtr;

    q->LRPtr=XorP(q->LRPtr,p);

    L.left=q;free(p);

    returnOK;

  }

  j=1;q=p->LRPtr;

  while(++j

  {

    q=XorP(p->LRPtr,pre);

    pre=p;p=q;

  }//while//找到待删结点q

  if(!

q)returnINFEASIBLE;//i不可以超过表长

  if(L.right==q)//q为最右结点的情况

  {

    p->LRPtr=XorP(p->LRPtr,q);

    L.right=p;free(q);

    returnOK;

  }

  r=XorP(q->LRPtr,p);//q为中间结点的情况,此时p,r分别为其左右结点

  p->LRPtr=XorP(XorP(p->LRPtr,q),r);

  r->LRPtr=XorP(XorP(r->LRPtr,q),p);//修改指针

  free(q);

  returnOK;

}//Delete_XorLinkedList

2.37

voidOEReform(DuLinkedList&L)//按1,3,5,...4,2的顺序重排双向循环链表L中的所有结点

{

  p=L.next;

  while(p->next!

=L&&p->next->next!

=L)

  {

    p->next=p->next->next;

    p=p->next;

  }//此时p指向最后一个奇数结点

  if(p->next==L)p->next=L->pre->pre;

  elsep->next=l->pre;

  p=p->next;//此时p指向最后一个偶数结点

  while(p->pre->pre!

=L)

  {

    p->next=p->pre->pre;

    p=p->next;

  }

  p->next=L;//按题目要求调整了next链的结构,此时pre链仍为原状

  for(p=L;p->next!

=L;p=p->next)p->next->pre=p;

  L->pre=p;//调整pre链的结构,同2.32方法

}//OEReform

分析:

next链和pre链的调整只能分开进行.如同时进行调整的话,必须使用堆栈保存偶数结点的指针,否则将会破坏链表结构,造成结点丢失.

2.38

DuLNode*Locate_DuList(DuLinkedList&L,intx)//带freq域的双向循环链表上的查找

{

  p=L.next;

  while(p.data!

=x&&p!

=L)p=p->next;

  if(p==L)returnNULL;//没找到

  p->freq++;q=p->pre;

  while(q->freq<=p->freq&&p!

=L)q=q->pre;//查找插入位置

  if(q!

=p->pre)

  {

    p->pre->next=p->next;p->next->pre=p->pre;

    q->next->pre=p;p->next=q->next;

    q->next=p;p->pre=q;//调整位置

  }

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

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2