第四周 建立单链表算法库
来源:互联网 发布:美股行情软件下载 编辑:程序博客网 时间:2024/06/16 16:56
- 烟台大学计算机学院
- 文件名称:xm.cpp
- 作者:李浩南
- 完成日期:2017年9月26日
- 输入描述:无
- 输出描述:进行了各个的链表的值
- */
list.cpp
- #include <stdio.h>
- #include <malloc.h>
- #include "list.h"
- void initList(Linklist *&L)//初始化链表
- {
- L=(Linklist *)malloc(sizeof(Linklist));//动态开辟内存
- L->next=NULL;//链表为空
- }
- bool ListInsert(Linklist *&L,int i,ElemType e)//链表插入
- {
- int j=0;
- Linklist *p=L,*s;
- if(i<=0)//输入的i比0小不合法
- {
- return false;
- }
- while(j<i-1 && p!=NULL)//遍历
- {
- j++;
- p=p->next;
- }
- if(p==NULL)//未找到i-1位置结点
- {
- return false;
- }
- else//找到
- {
- s=(Linklist *)malloc(sizeof(Linklist));
- s->data=e;
- s->next=p->next;
- p->next=s;//插入操作
- return true;
- }
- }
- void DispList(Linklist *L)//输出链表的元素值
- {
- Linklist*p=L->next;
- while(p!=NULL)//输出+遍历
- {
- printf("%d ",p->data);
- p=p->next;
- }
- printf("\n");
- }
- void DestroyList(Linklist *&L)//销毁链表
- {
- Linklist *pre=L,*p=L->next;
- while(p!=NULL)
- {
- free(pre);
- pre=p;
- p=pre->next;
- }
- free(pre);
- }
- void CreateListF(Linklist *&L,ElemType a[],int n)
- {
- Linklist *s;
- L=(Linklist *)malloc(sizeof(Linklist));//动态开辟内存空间
- L->next=NULL;
- for(int i=0;i<n;i++)//头插法
- {
- s=(Linklist *)malloc(sizeof(Linklist));
- s->data=a[i];
- s->next=L->next;
- L->next=s;
- }
- }
- void CreateListR(Linklist *&L,ElemType a[],int n)//尾插法
- {
- Linklist *s,*r;
- L=(Linklist *)malloc(sizeof(Linklist));
- r=L;
- for(int i=0;i<n;i++)//尾插法
- {
- s=(Linklist *)malloc(sizeof(Linklist));
- s->data=a[i];
- r->next=s;
- r=s;
- }
- r->next=NULL;
- }
- bool ListDelete(Linklist *&L,int i,ElemType &e)//删除链表元素
- {
- int j=0;
- Linklist *p=L,*q;
- if(i<=0)return false;
- while(j<i-1 && p!=NULL)//找到i的前一节点i-1
- {
- j++;
- p=p->next;
- }
- if(p==NULL)//p为空,未找到元素
- {
- return false;
- }
- else
- {
- q=p->next;
- if(q==NULL)//未找到元素
- {
- return false;
- }
- e=q->data;//删除的元素保留到e
- p->next=q->next;
- free(q);
- return true;
- }
- }
- #include <stdio.h>
- #include <malloc.h>
- #include "list.h"
- int main()
- {
- Linklist *L1,*L2;
- ElemType a[8]={7,9,8,2,0,4,6,3};
- printf("头插法建表结果:");
- CreateListF(L1,a,8);
- DispList(L1);
- printf("尾插法建表结果:");
- CreateListR(L2,a,8);
- DispList(L2);
- DestoryList(L1);
- DestoryList(L2);
- int b;
- Linklist *L3;
- CreateListR(L3,a,8);
- ListDelete(L3,4,b);
- printf("删除a数组中的元素:");
- printf("%d\n",b);
- DispList(L3);
- printf("插入验证:");
- Linklist*L;
- initList(L);
- ListInsert(L,1,15);
- ListInsert(L,1,10);
- ListInsert(L,1,5);
- ListInsert(L,1,20);
- DispList(L);
- DestroyList(L);
- return 0;
- }
- list.h :
- <pre class="cpp" name="code">#include <stdio.h>
- #include <malloc.h>
- #include <stdio.h>
- #include <malloc.h>
- typedef int ElemType;
- typedef struct LNode
- {
- ElemType data;
- struct LNode *next;
- }Linklist;
- void initList(Linklist *&L);//初始化链表
- bool ListInsert(Linklist *&L,int i,ElemType e);//链表插入
- void DispList(Linklist *L);//输出
- void DestroyList(Linklist *&L);//销毁
- void CreateListF(Linklist *&L,ElemType a[],int n);//头插法
- void CreateListR(Linklist *&L,ElemType a[],int n);//尾插法
- bool ListDelete(Linklist *&L,int i,ElemType &e);//元素删除 </pre><br>
- <p></p>
- <pre></pre>
- <pre class="cpp" name="code"> 运行结果:</pre><pre class="cpp" name="code"> </pre><pre class="cpp" name="code"> <img alt="" src="http://img.blog.csdn.net/20170926205758728?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTGZlbEw=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast"></pre><pre class="cpp" name="code"></pre><pre class="cpp" name="code"></pre><pre class="cpp" name="code"><pre class="cpp" name="code">学习心得:</pre><br>
- <pre></pre>
- <pre class="cpp" name="code"> 通过这次的训练,<span style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">学会了建立链表算法库,感觉好开心</span></pre><pre class="cpp" name="code"> </pre><pre class="cpp" name="code">
- </pre><br>
- <p></p>
- </pre>
阅读全文
0 1
- 第四周 建立单链表算法库
- 第四周 建立单链表算法库
- 第四周 建立单链表算法库
- 第四周—建立单链表算法库
- 第四周 项目2-建立“单链表”算法库
- 第四周项目2—建立“单链表”算法库
- 第四周--项目二--建立“单链表”算法库
- 第四周项目2 建立单链表算法库
- 第四周项目2--建立”单链表“算法库
- 第四周实践项目~建立“单链表”算法库
- 第四周实践项目2-建立单链表算法库
- 第四周 项目2 建立“单链表”的算法库
- 第四周项目2 建立单链表算法库
- 第四周 项目二-建立单链表算法库
- 第四周项目2--建立“单链表”算法库
- 第四周项目2建立“单链表”算法库
- 第四周项目2 建立单链表算法库
- 第四周项目2-建立"单链表"算法库
- SpringMVC 常用注解(个人笔记)
- 批量xml格式文件按字段顺序转json文件
- 第三周 【项目 4
- 菜鸟先飞之jAVA_异常
- Quartus_II_13.1c (64-bit)与仿真器的安装与破解
- 第四周 建立单链表算法库
- AVA技术路线图
- 修改后的日期类
- (欧) 第6章 预处理、const与sizeof
- 虚函数
- hadoop之yarn的资源分配详解
- [NLP]1.StanfordNLP的安装和初探
- 设计模式9-外观模式
- 第四周 建立双链表算法库