分享
分销 收藏 举报 申诉 / 65
播放页_导航下方通栏广告

类型《数据结构》的全部代码实现C语言.doc

  • 上传人:天****
  • 文档编号:4373672
  • 上传时间:2024-09-14
  • 格式:DOC
  • 页数:65
  • 大小:214.50KB
  • 下载积分:14 金币
  • 播放页_非在线预览资源立即下载上方广告
    配套讲稿:

    如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

    特殊限制:

    部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

    关 键  词:
    数据结构 华严 全部 代码 实现 语言
    资源描述:
    <p><span id="_baidu_bookmark_start_0" style="display: none; line-height: 0px;">‍</span>/* c1、h (程序名) */ #include</p><string、h>#include<ctype、h>#include<malloc、h>/* malloc()等 */ #include<limits、h>/* INT_MAX等 */ #include<stdio、h>/* EOF(=^Z或F6),NULL */ #include<stdlib、h>/* atoi() */ #include<io、h>/* eof() */ #include<math、h>/* floor(),ceil(),abs() */ #include<process、h>/* exit() */ /* 函数结果状态代码 */ #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 /* #define OVERFLOW -2 因为在math、h中已定义OVERFLOW得值为3,故去掉此行 */ typedef int Status; /* Status就是函数得类型,其值就是函数结果状态代码,如OK等 */ typedef int Boolean; /* Boolean就是布尔类型,其值就是TRUE或FALSE */ /* algo2-1、c 实现算法2、1得程序 */ #include&quot;c1、h&quot; typedef int ElemType; #include&quot;c2-1、h&quot; /*c2-1、h 线性表得动态分配顺序存储结构 */ #define LIST_INIT_SIZE 10 /* 线性表存储空间得初始分配量 */ #define LISTINCREMENT 2 /* 线性表存储空间得分配增量 */ typedef struct { &nbsp; ElemType *elem; /* 存储空间基址 */ &nbsp; int length; /* 当前长度 */ &nbsp; int listsize; /* 当前分配得存储容量(以sizeof(ElemType)为单位) */ }SqList; #include&quot;bo2-1、c&quot; /* bo2-1、c 顺序表示得线性表(存储结构由c2-1、h定义)得基本操作(12个) */ Status InitList(SqList *L) /* 算法2、3 */ { /* 操作结果:构造一个空得顺序线性表 */ &nbsp; (*L)、elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType)); &nbsp; if(!(*L)、elem) &nbsp; &nbsp; exit(OVERFLOW); /* 存储分配失败 */ &nbsp; (*L)、length=0; /* 空表长度为0 */ &nbsp; (*L)、listsize=LIST_INIT_SIZE; /* 初始存储容量 */ &nbsp; return OK; } Status DestroyList(SqList *L) { /* 初始条件:顺序线性表L已存在。操作结果:销毁顺序线性表L */ &nbsp; free((*L)、elem); &nbsp; (*L)、elem=NULL; &nbsp; (*L)、length=0; &nbsp; (*L)、listsize=0; &nbsp; return OK; } Status ClearList(SqList *L) { /* 初始条件:顺序线性表L已存在。操作结果:将L重置为空表 */ &nbsp; (*L)、length=0; &nbsp; return OK; } Status ListEmpty(SqList L) { /* 初始条件:顺序线性表L已存在。操作结果:若L为空表,则返回TRUE,否则返回FALSE */ &nbsp; if(L、length==0) &nbsp; &nbsp; return TRUE; &nbsp; else &nbsp; &nbsp; return FALSE; } int ListLength(SqList L) { /* 初始条件:顺序线性表L已存在。操作结果:返回L中数据元素个数 */ &nbsp; return L、length; } Status GetElem(SqList L,int i,ElemType *e) { /* 初始条件:顺序线性表L已存在,1≤i≤ListLength(L) */ &nbsp; /* 操作结果:用e返回L中第i个数据元素得值 */ &nbsp; if(i&lt;1||i&gt;L、length) &nbsp; &nbsp; exit(ERROR); &nbsp; *e=*(L、elem+i-1); &nbsp; return OK; } int LocateElem(SqList L,ElemType e,Status(*compare)(ElemType,ElemType)) { /* 初始条件:顺序线性表L已存在,compare()就是数据元素判定函数(满足为1,否则为0) */ &nbsp; /* 操作结果:返回L中第1个与e满足关系compare()得数据元素得位序。 */ &nbsp; /* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 若这样得数据元素不存在,则返回值为0。算法2、6 */ &nbsp; ElemType *p; &nbsp; int i=1; /* i得初值为第1个元素得位序 */ &nbsp; p=L、elem; /* p得初值为第1个元素得存储位置 */ &nbsp; while(i&lt;=L、length&amp;&amp;!compare(*p++,e)) &nbsp; &nbsp; ++i; &nbsp; if(i&lt;=L、length) &nbsp; &nbsp; return i; &nbsp; else &nbsp; &nbsp; return 0; } Status PriorElem(SqList L,ElemType cur_e,ElemType *pre_e) { /* 初始条件:顺序线性表L已存在 */ &nbsp; /* 操作结果:若cur_e就是L得数据元素,且不就是第一个,则用pre_e返回它得前驱, */ &nbsp; /* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 否则操作失败,pre_e无定义 */ &nbsp; int i=2; &nbsp; ElemType *p=L、elem+1; &nbsp; while(i&lt;=l、length&amp;&amp;*p!=cur_e) i=&quot;&quot;&gt;L、length) &nbsp; &nbsp; return INFEASIBLE; &nbsp; else &nbsp; { &nbsp; &nbsp; *pre_e=*--p; &nbsp; &nbsp; return OK; &nbsp; } } Status NextElem(SqList L,ElemType cur_e,ElemType *next_e) { /* 初始条件:顺序线性表L已存在 */ &nbsp; /* 操作结果:若cur_e就是L得数据元素,且不就是最后一个,则用next_e返回它得后继, */ &nbsp; /* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 否则操作失败,next_e无定义 */ &nbsp; int i=1; &nbsp; ElemType *p=L、elem; &nbsp; while(i&lt;L、length&amp;&amp;*p!=cur_e) &nbsp; { &nbsp; &nbsp; i++; &nbsp; &nbsp; p++; &nbsp; } &nbsp; if(i==L、length) &nbsp; &nbsp; return INFEASIBLE; &nbsp; else &nbsp; { &nbsp; &nbsp; *next_e=*++p; &nbsp; &nbsp; return OK; &nbsp; } } Status ListInsert(SqList *L,int i,ElemType e) /* 算法2、4 */ { /* 初始条件:顺序线性表L已存在,1≤i≤ListLength(L)+1 */ &nbsp; /* 操作结果:在L中第i个位置之前插入新得数据元素e,L得长度加1 */ &nbsp; ElemType *newbase,*q,*p; &nbsp; if(i&lt;1||i&gt;(*L)、length+1) /* i值不合法 */ &nbsp; &nbsp; return ERROR; &nbsp; if((*L)、length&gt;=(*L)、listsize) /* 当前存储空间已满,增加分配 */ &nbsp; { &nbsp; &nbsp; newbase=(ElemType *)realloc((*L)、elem,((*L)、listsize+LISTINCREMENT)*sizeof(ElemType)); &nbsp; &nbsp; if(!newbase) &nbsp; &nbsp; &nbsp; exit(OVERFLOW); /* 存储分配失败 */ &nbsp; &nbsp; (*L)、elem=newbase; /* 新基址 */ &nbsp; &nbsp; (*L)、listsize+=LISTINCREMENT; /* 增加存储容量 */ &nbsp; } &nbsp; q=(*L)、elem+i-1; /* q为插入位置 */ &nbsp; for(p=(*L)、elem+(*L)、length-1;p&gt;=q;--p) /* 插入位置及之后得元素右移 */ &nbsp; &nbsp; *(p+1)=*p; &nbsp; *q=e; /* 插入e */ &nbsp; ++(*L)、length; /* 表长增1 */ &nbsp; return OK; } Status ListDelete(SqList *L,int i,ElemType *e) /* 算法2、5 */ { /* 初始条件:顺序线性表L已存在,1≤i≤ListLength(L) */ &nbsp; /* 操作结果:删除L得第i个数据元素,并用e返回其值,L得长度减1 */ &nbsp; ElemType *p,*q; &nbsp; if(i&lt;1||i&gt;(*L)、length) /* i值不合法 */ &nbsp; &nbsp; return ERROR; &nbsp; p=(*L)、elem+i-1; /* p为被删除元素得位置 */ &nbsp; *e=*p; /* 被删除元素得值赋给e */ &nbsp; q=(*L)、elem+(*L)、length-1; /* 表尾元素得位置 */ &nbsp; for(++p;p&lt;=q;++p) /* 被删除元素之后得元素左移 */ &nbsp; &nbsp; *(p-1)=*p; &nbsp; (*L)、length--; /* 表长减1 */ &nbsp; return OK; } Status ListTraverse(SqList L,void(*vi)(ElemType*)) { /* 初始条件:顺序线性表L已存在 */ &nbsp; /* 操作结果:依次对L得每个数据元素调用函数vi()。一旦vi()失败,则操作失败 */ &nbsp; /* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vi()得形参加&#39;&amp;&#39;,表明可通过调用vi()改变元素得值 */ &nbsp; ElemType *p; &nbsp; int i; &nbsp; p=L、elem; &nbsp; for(i=1;i&lt;=L、length;i++) &nbsp; &nbsp; vi(p++); &nbsp; printf(&quot;\n&quot;); &nbsp; return OK; } Status equal(ElemType c1,ElemType c2) { /* 判断就是否相等得函数,Union()用到 */ &nbsp; if(c1==c2) &nbsp; &nbsp; return TRUE; &nbsp; else &nbsp; &nbsp; return FALSE; } void Union(SqList *La,SqList Lb) /* 算法2、1 */ { /* 将所有在线性表Lb中但不在La中得数据元素插入到La中 */ &nbsp; ElemType e; &nbsp; int La_len,Lb_len; &nbsp; int i; &nbsp; La_len=ListLength(*La); /* 求线性表得长度 */ &nbsp; Lb_len=ListLength(Lb); &nbsp; for(i=1;i&lt;=Lb_len;i++) &nbsp; { &nbsp; &nbsp; GetElem(Lb,i,&amp;e); /* 取Lb中第i个数据元素赋给e */ &nbsp; &nbsp; if(!LocateElem(*La,e,equal)) /* La中不存在与e相同得元素,则插入之 */ &nbsp; &nbsp; &nbsp; ListInsert(La,++La_len,e); &nbsp; } } void print(ElemType *c) { &nbsp; printf(&quot;%d &quot;,*c); } void main() { &nbsp; SqList La,Lb; &nbsp; Status i; &nbsp; int j; &nbsp; i=InitList(&amp;La); &nbsp; if(i==1) /* 创建空表La成功 */ &nbsp; &nbsp; for(j=1;j&lt;=5;j++) /* 在表La中插入5个元素 */ &nbsp; &nbsp; &nbsp; i=ListInsert(&amp;La,j,j); &nbsp; printf(&quot;La= &quot;); /* 输出表La得内容 */ &nbsp; ListTraverse(La,print); &nbsp; InitList(&amp;Lb); /* 也可不判断就是否创建成功 */ &nbsp; for(j=1;j&lt;=5;j++) /* 在表Lb中插入5个元素 */ &nbsp; &nbsp; i=ListInsert(&amp;Lb,j,2*j); &nbsp; printf(&quot;Lb= &quot;); /* 输出表Lb得内容 */ &nbsp; ListTraverse(Lb,print); &nbsp; Union(&amp;La,Lb); &nbsp; printf(&quot;new La= &quot;); /* 输出新表La得内容 */ &nbsp; ListTraverse(La,print);} /* algo2-2、c 实现算法2、2得程序 */ #include&quot;c1、h&quot; typedef int ElemType; #include&quot;c2-1、h&quot; #include&quot;bo2-1、c&quot; void MergeList(SqList La,SqList Lb,SqList *Lc) /* 算法2、2 */ { /* 已知线性表La与Lb中得数据元素按值非递减排列。 */ &nbsp; /* 归并La与Lb得到新得线性表Lc,Lc得数据元素也按值非递减排列 */ &nbsp; int i=1,j=1,k=0; &nbsp; int La_len,Lb_len; &nbsp; ElemType ai,bj; &nbsp; InitList(Lc); /* 创建空表Lc */ &nbsp; La_len=ListLength(La); &nbsp; Lb_len=ListLength(Lb); &nbsp; while(i&lt;=La_len&amp;&amp;j&lt;=Lb_len) /* 表La与表Lb均非空 */ &nbsp; { &nbsp; &nbsp; GetElem(La,i,&amp;ai); &nbsp; &nbsp; GetElem(Lb,j,&amp;bj); &nbsp; &nbsp; if(ai&lt;=bj) &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; ListInsert(Lc,++k,ai); &nbsp; &nbsp; &nbsp; ++i; &nbsp; &nbsp; } &nbsp; &nbsp; else &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; ListInsert(Lc,++k,bj); &nbsp; &nbsp; &nbsp; ++j; &nbsp; &nbsp; } &nbsp; } &nbsp; while(i&lt;=La_len) /* 表La非空且表Lb空 */ &nbsp; { &nbsp; &nbsp; GetElem(La,i++,&amp;ai); &nbsp; &nbsp; ListInsert(Lc,++k,ai); &nbsp; } &nbsp; while(j&lt;=Lb_len) /* 表Lb非空且表La空 */ &nbsp; { &nbsp; &nbsp; GetElem(Lb,j++,&amp;bj); &nbsp; &nbsp; ListInsert(Lc,++k,bj); &nbsp; } } void print(ElemType *c) { &nbsp; printf(&quot;%d &quot;,*c); } void main() { &nbsp; SqList La,Lb,Lc; &nbsp; int j,a[4]={3,5,8,11},b[7]={2,6,8,9,11,15,20}; &nbsp; InitList(&amp;La); /* 创建空表La */ &nbsp; for(j=1;j&lt;=4;j++) /* 在表La中插入4个元素 */ &nbsp; &nbsp; ListInsert(&amp;La,j,a[j-1]); &nbsp; printf(&quot;La= &quot;); /* 输出表La得内容 */ &nbsp; ListTraverse(La,print); &nbsp; InitList(&amp;Lb); /* 创建空表Lb */ &nbsp; for(j=1;j&lt;=7;j++) /* 在表Lb中插入7个元素 */ &nbsp; &nbsp; ListInsert(&amp;Lb,j,b[j-1]); &nbsp; printf(&quot;Lb= &quot;); /* 输出表Lb得内容 */ &nbsp; ListTraverse(Lb,print); &nbsp; MergeList(La,Lb,&amp;Lc); &nbsp; printf(&quot;Lc= &quot;); /* 输出表Lc得内容 */ &nbsp; ListTraverse(Lc,print); } /* algo2-3、c 实现算法2、7得程序 */ #include&quot;c1、h&quot; typedef int ElemType; #include&quot;c2-1、h&quot; #include&quot;bo2-1、c&quot; void MergeList(SqList La,SqList Lb,SqList *Lc) /* 算法2、7 */ { /* 已知顺序线性表La与Lb得元素按值非递减排列。 */ &nbsp; /* 归并La与Lb得到新得顺序线性表Lc,Lc得元素也按值非递减排列 */ &nbsp; ElemType *pa,*pa_last,*pb,*pb_last,*pc; &nbsp; pa=La、elem; &nbsp; pb=Lb、elem; &nbsp; (*Lc)、listsize=(*Lc)、length=La、length+Lb、length;/*不用InitList()创建空表Lc */ &nbsp; pc=(*Lc)、elem=(ElemType *)malloc((*Lc)、listsize*sizeof(ElemType)); &nbsp; if(!(*Lc)、elem) /* 存储分配失败 */ &nbsp; &nbsp; exit(OVERFLOW); &nbsp; pa_last=La、elem+La、length-1; &nbsp; pb_last=Lb、elem+Lb、length-1; &nbsp; while(pa&lt;=pa_last&amp;&amp;pb&lt;=pb_last) /* 表La与表Lb均非空 */ &nbsp; { /* 归并 */ &nbsp; &nbsp; if(*pa&lt;=*pb) &nbsp; &nbsp; &nbsp; *pc++=*pa++; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; *pc++=*pb++; &nbsp; } &nbsp; while(pa&lt;=pa_last) /* 表La非空且表Lb空 */ &nbsp; &nbsp; *pc++=*pa++; /* 插入La得剩余元素 */ &nbsp; while(pb&lt;=pb_last) /* 表Lb非空且表La空 */ &nbsp; &nbsp; *pc++=*pb++; /* 插入Lb得剩余元素 */ } void print(ElemType *c) { &nbsp; printf(&quot;%d &quot;,*c); } void main() { &nbsp; SqList La,Lb,Lc; &nbsp; int j; &nbsp; InitList(&amp;La); /* 创建空表La */ &nbsp; for(j=1;j&lt;=5;j++) /* 在表La中插入5个元素 */ &nbsp; &nbsp; ListInsert(&amp;La,j,j); &nbsp; printf(&quot;La= &quot;); /* 输出表La得内容 */ &nbsp; ListTraverse(La,print); &nbsp; InitList(&amp;Lb); /* 创建空表Lb */ &nbsp; for(j=1;j&lt;=5;j++) /* 在表Lb中插入5个元素 */ &nbsp; &nbsp; ListInsert(&amp;Lb,j,2*j); &nbsp; printf(&quot;Lb= &quot;); /* 输出表Lb得内容 */ &nbsp; ListTraverse(Lb,print); &nbsp; MergeList(La,Lb,&amp;Lc); &nbsp; printf(&quot;Lc= &quot;); /* 输出表Lc得内容 */ &nbsp; ListTraverse(Lc,print); } /* algo2-4、c 修改算法2、7得第一个循环语句中得条件语句为开关语句,且当 */ /* *pa=*pb时,只将两者中之一插入Lc。此操作得结果与算法2、1相同 */ #include&quot;c1、h&quot; typedef int ElemType; #include&quot;c2-1、h&quot; #include&quot;bo2-1、c&quot; int comp(ElemType c1,ElemType c2) { &nbsp; int i; &nbsp; if(c1&lt;c2) &nbsp; &nbsp; i=1; &nbsp; else if(c1==c2) &nbsp; &nbsp; i=0; &nbsp; else &nbsp; &nbsp; i=-1; &nbsp; return i; } void MergeList(SqList La,SqList Lb,SqList *Lc) { /* 另一种合并线性表得方法(根据算法2、7下得要求修改算法2、7) */ &nbsp; ElemType &nbsp;*pa,*pa_last,*pb,*pb_last,*pc; &nbsp; pa=La、elem; &nbsp; pb=Lb、elem; &nbsp; (*Lc)、listsize=La、length+Lb、length; /* 此句与算法2、7不同 */ &nbsp; pc=(*Lc)、elem=(ElemType *)malloc((*Lc)、listsize*sizeof(ElemType)); &nbsp; if(!(*Lc)、elem) &nbsp; &nbsp; exit(OVERFLOW); &nbsp; pa_last=La、elem+La、length-1; &nbsp; pb_last=Lb、elem+Lb、length-1; &nbsp; while(pa&lt;=pa_last&amp;&amp;pb&lt;=pb_last) /* 表La与表Lb均非空 */ &nbsp; &nbsp; switch(comp(*pa,*pb)) /* 此句与算法2、7不同 */ &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; case &nbsp;0: pb++; &nbsp; &nbsp; &nbsp; case &nbsp;1: *pc++=*pa++; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break; &nbsp; &nbsp; &nbsp; case -1: *pc++=*pb++; &nbsp; &nbsp; } &nbsp; while(pa&lt;=pa_last) /* 表La非空且表Lb空 */ &nbsp; &nbsp; *pc++=*pa++; &nbsp; while(pb&lt;=pb_last) /* 表Lb非空且表La空 */ &nbsp; &nbsp; *pc++=*pb++; &nbsp; (*Lc)、length=pc-(*Lc)、elem; /* 加此句 */ } void print(ElemType *c) { &nbsp; printf(&quot;%d &quot;,*c); } void main() { &nbsp; SqList La,Lb,Lc; &nbsp; int j; &nbsp; InitList(&amp;La); /* 创建空表La */ &nbsp; for(j=1;j&lt;=5;j++) /* 在表La中插入5个元素 */ &nbsp; &nbsp; ListInsert(&amp;La,j,j); &nbsp; printf(&quot;La= &quot;); /* 输出表La得内容 */ &nbsp; ListTraverse(La,print); &nbsp; InitList(&amp;Lb); /* 创建空表Lb */ &nbsp; for(j=1;j&lt;=5;j++) lb=&quot;); /* 输出表Lb得内容 */ &nbsp; ListTraverse(Lb,print); &nbsp; MergeList(La,Lb,&amp;Lc); &nbsp; printf(&quot; lc=&quot;); /* 输出表Lc得内容 */ &nbsp; ListTraverse(Lc,print); } /* algo2-5、c 实现算法2、11、2、12得程序 */ #include&quot; typedef=&quot;&quot; int=&quot;&quot; h=&quot;&quot; struct=&quot;&quot; lnode=&quot;&quot; elemtype=&quot;&quot; c=&quot;&quot; status=&quot;&quot; linklist=&quot;&quot; l=&quot;(LinkList)malloc(sizeof(struct&quot; -=&quot;&quot;&gt;next=NULL; /* 指针域为空 */ &nbsp; return OK; } Status DestroyList(LinkList *L) { /* 初始条件:线性表L已存在。操作结果:销毁线性表L */ &nbsp; LinkList q; &nbsp; while(*L) &nbsp; { &nbsp; &nbsp; q=(*L)-&gt;next; &nbsp; &nbsp; free(*L); &nbsp; &nbsp; *L=q; &nbsp; } &nbsp; return OK; } Status ClearList(LinkList L) /* 不改变L */ { /* 初始条件:线性表L已存在。操作结果:将L重置为空表 */ &nbsp; LinkList p,q; &nbsp; p=L-&gt;next; /* p指向第一个结点 */ &nbsp; while(p) /* 没到表尾 */ &nbsp; { &nbsp; &nbsp; q=p-&gt;next; &nbsp; &nbsp; free(p); &nbsp; &nbsp; p=q; &nbsp; } &nbsp; L-&gt;next=NULL; /* 头结点指针域为空 */ &nbsp; return OK; } Status ListEmpty(LinkList L) { /* 初始条件:线性表L已存在。操作结果:若L为空表,则返回TRUE,否则返回FALSE */ &nbsp; if(L-&gt;next) /* 非空 */ &nbsp; &nbsp; return FALSE; &nbsp; else &nbsp; &nbsp; return TRUE; } int ListLength(LinkList L) { /* 初始条件:线性表L已存在。操作结果:返回L中数据元素个数 */ &nbsp; int i=0; &nbsp; LinkList p=L-&gt;next; /* p指向第一个结点 */ &nbsp; while(p) /* 没到表尾 */ &nbsp; { &nbsp; &nbsp; i++; &nbsp; &nbsp; p=p-&gt;next; &nbsp; } &nbsp; return i; } Status GetElem(LinkList L,int i,ElemType *e) /* 算法2、8 */ { /* L为带头结点得单链表得头指针。当第i个元素存在时,其值赋给e并返回OK,否则返回ERROR */ &nbsp; int j=1; /* j为计数器 */ &nbsp; LinkList p=L-&gt;next; /* p指向第一个结点 */ &nbsp; while(p&amp;&amp;j<i) p="p-">next; &nbsp; &nbsp; j++; &nbsp; } &nbsp; if(!p||j&gt;i) /* 第i个元素不存在 */ &nbsp; &nbsp; return ERROR; &nbsp; *e=p-&gt;data; /* 取第i个元素 */ &nbsp; return OK; } int LocateElem(LinkList L,ElemType e,Status(*compare)(ElemType,ElemType)) { /* 初始条件: 线性表L已存在,compare()就是数据元素判定函数(满足为1,否则为0) */ &nbsp; /* 操作结果: 返回L中第1个与e满足关系compare()得数据元素得位序。 */ &nbsp; /* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 若这样得数据元素不存在,则返回值为0 */ &nbsp; int i=0; &nbsp; LinkList p=L-&gt;next; &nbsp; while(p) &nbsp; { &nbsp; &nbsp; i++; &nbsp; &nbsp; if(compare(p-&gt;data,e)) /* 找到这样得数据元素 */ &nbsp; &nbsp; &nbsp; return i; &nbsp; &nbsp; p=p-&gt;next; &nbsp; } &nbsp; return 0; } Status PriorElem(LinkList L,ElemType cur_e,ElemType *pre_e) { /* 初始条件: 线性表L已存在 */ &nbsp; /* 操作结果: 若cur_e就是L得数据元素,且不就是第一个,则用pre_e返回它得前驱, */ &nbsp; /* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 返回OK;否则操作失败,pre_e无定义,返回INFEASIBLE */ &nbsp; LinkList q,p=L-&gt;next; /* p指向第一个结点 */ &nbsp; while(p-&gt;next) /* p所指结点有后继 */ &nbsp; { &nbsp; &nbsp; q=p-&gt;next; /* q为p得后继 */ &nbsp; &nbsp; if(q-&gt;data==cur_e) &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; *pre_e=p-&gt;data; &nbsp; &nbsp; &nbsp; return OK; &nbsp; &nbsp; } &nbsp; &nbsp; p=q; /* p向后移 */ &nbsp; } &nbsp; return INFEASIBLE; } Status NextElem(LinkList L,ElemType cur_e,ElemType *next_e) { /* 初始条件:线性表L已存在 */ &nbsp; /* 操作结果:若cur_e就是L得数据元素,且不就是最后一个,则用next_e返回它得后继, */ &nbsp; /* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 返回OK;否则操作失败,next_e无定义,返回INFEASIBLE */ &nbsp; LinkList p=L-&gt;next; /* p指向第一个结点 */ &nbsp; while(p-&gt;next) /* p所指结点有后继 */ &nbsp; { &nbsp; &nbsp; if(p-&gt;data==cur_e) &nbsp; &nbsp; { &nbsp; &nbsp; &nbsp; *next_e=p-&gt;next-&gt;data; &nbsp; &nbsp; &nbsp; return OK; &nbsp; &nbsp; } &nbsp; &nbsp; p=p-&gt;next; &nbsp; } &nbsp; return INFEASIBLE; } Status ListInsert(LinkList L,int i,ElemType e) /* 算法2、9。不改变L */ { /* 在带头结点得单链线性表L中第i个位置之前插入元素e */ &nbsp; int j=0; &nbsp; LinkList p=L,s; &nbsp; while(p&amp;&amp;j<i-1) p="p-">next; &nbsp; &nbsp; j++; &nbsp; } &nbsp; if(!p||j&gt;i-1) /* i小于1或者大于表长 */ &nbsp; &nbsp; return ERROR; &nbsp; s=(LinkList)malloc(sizeof(struct LNode)); /* 生成新结点 */ &nbsp; s-&gt;data=e; /* 插入L中 */ &nbsp; s-&gt;next=p-&gt;next; &nbsp; p-&gt;next=s; &nbsp; return OK; } Status ListDelete(LinkList L,int i,ElemType *e) /* 算法2、10。不改变L */ { /* 在带头结点得单链线性表L中,删除第i个元素,并由e返回其值 */ &nbsp; int j=0; &nbsp; LinkList p=L,q; &nbsp; while(p-&gt;next&amp;&amp;j<i-1) p="p-">next; &nbsp; &nbsp; j++; &nbsp; } &nbsp; if(!p-&gt;next||j&gt;i-1) /* 删除位置不合理 */ &nbsp; &nbsp; return ERROR; &nbsp; q=p-&gt;next; /* 删除并释放结点 */ &nbsp; p-&gt;next=q-&gt;next; &nbsp; *e=q-&gt;data; &nbsp; free(q); &nbsp; return OK; } Status ListTraverse(LinkList L,void(*vi)(ElemType)) /* vi得形参类型为ElemType,与bo2-1、c中相应函数得形参类型ElemType&amp;不同 */ { /* 初始条件:线性表L已存在 */ &nbsp; /* 操作结果:依次对L得每个数据元素调用函数vi()。一旦vi()失败,则操作失败 */ &nbsp; LinkList p=L-&gt;next; &nbsp; while(p) &nbsp; { &nbsp; &nbsp; vi(p-&gt;data); &nbsp; &nbsp; p=p-&gt;next; &nbsp; } &nbsp; printf(&quot;\n&quot;); &nbsp; return OK; } void CreateList(LinkList *L,int n) /* 算法2、11 */ { /* 逆位序(插在表头)输入n个元素得值,建立带表头结构得单链线性表L */ &nbsp; int i; &nbsp; LinkList p; &nbsp; *L=(LinkList)malloc(sizeof(struct LNode)); &nbsp; (*L)-&gt;next=NULL; /* 先建立一个带头结点得单链表 */ &nbsp; printf(&quot;请输入%d个数据\n&quot;,n); &nbsp; for(i=n;i&gt;0;--i) &nbsp; { &nbsp; &nbsp; p=(LinkList)malloc(sizeof(struct LNode)); /* 生成新结点 */ &nbsp; &nbsp; scanf(&quot;%d&quot;,&amp;p-&gt;data); /* 输入元素值 */ &nbsp; &nbsp; p-&gt;next=(*L)-&gt;next; /* 插入到表头 */ &nbsp; &nbsp; (*L)-&gt;next=p; &nbsp; } } void CreateList2(LinkList *L,int n) { /* 正位序(插在表尾)输入n个元素得值,建立带表头结构得单链线性表 */ &nbsp; int i; &nbsp; LinkList p,q; &nbsp; *L=(LinkList)malloc(sizeof(struct LNode)); /* 生成头结点 */ &nbsp; (*L)-&gt;next=NULL; &nbsp; q=*L; &nbsp; printf(&quot;请输入%d个数据\n&quot;,n); &nbsp; for(i=1;i&lt;</i-1)></i-1)></i)><!--=5;j++)--><!--1||i--><!--1||i--><!--=l、length&&*p!=cur_e)--><!--1||i--></process、h></math、h></io、h></stdlib、h></stdio、h></limits、h></malloc、h></ctype、h></string、h>
    展开阅读全文
    提示  咨信网温馨提示:
    1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
    2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
    3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
    4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
    5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
    6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

    开通VIP折扣优惠下载文档

    自信AI创作助手
    关于本文
    本文标题:《数据结构》的全部代码实现C语言.doc
    链接地址:https://www.zixin.com.cn/doc/4373672.html
    页脚通栏广告

    Copyright ©2010-2026   All Rights Reserved  宁波自信网络信息技术有限公司 版权所有   |  客服电话:0574-28810668    微信客服:咨信网客服    投诉电话:18658249818   

    违法和不良信息举报邮箱:help@zixin.com.cn    文档合作和网站合作邮箱:fuwu@zixin.com.cn    意见反馈和侵权处理邮箱:1219186828@qq.com   | 证照中心

    12321jubao.png12321网络举报中心 电话:010-12321  jubao.png中国互联网举报中心 电话:12377   gongan.png浙公网安备33021202000488号  icp.png浙ICP备2021020529号-1 浙B2-20240490   


    关注我们 :微信公众号  抖音  微博  LOFTER               

    自信网络  |  ZixinNetwork