‍/* c1、h (程序名) */ #include

‍/* c1、h (程序名)"/>
收藏 分销(赏)

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

上传人:天**** 文档编号:4373672 上传时间:2024-09-14 格式:DOC 页数:65 大小:214.50KB
下载 相关 举报
《数据结构》的全部代码实现C语言.doc_第1页
第1页 / 共65页
《数据结构》的全部代码实现C语言.doc_第2页
第2页 / 共65页
点击查看更多>>
资源描述
<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>
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 通信科技 > 开发语言

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

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

客服