最近在学数据结构,学到链表这节作业有链表,毕竟菜鸟代码基本照看书上算法写的,再加上自己的小修改,这里先记录下来,万一哪天想看了,来看看。

  里面有用到二级指针,还是不太理解,还有就是注释不多,后续有了更好的理解,再来添加

 #define TRUE                    1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define INFEASIBLE -1
#define Status int
#define ElemType int typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode; typedef LNode LinkList;
Status ListInsert_L(LinkList L, int i, ElemType e);//插入函数
Status ListDelete_L(LinkList L, int i, ElemType *e);//删除某个元素
void CreateList_L(LinkList L, int n);//创建一个带头结点的空链表
void MergeList_L(LinkList *La, LinkList *Lb, LinkList **Lc); //合并两个有序链表

LinkList.h

 #include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "LinkList.h" //新建带头结点的空链表
void CreateList_L(LinkList **L, int n)
{
int i; LinkList *p = NULL;
(*L) = (LinkList*) malloc (sizeof (LNode));
(*L) ->next = NULL;
printf("请输入链表元素 :\n");
for(i = n; i > ; --i)
{
p = (LinkList*) malloc (sizeof (LNode));//生成新结点
scanf("%d", &p -> data);//输入元素值
p -> next = (*L) -> next;
(*L) -> next = p;//插入到表头
}
} //插入元素
Status ListInsert_L(LinkList *L, int i, ElemType e)
{
LinkList *p = L;
LinkList *s = NULL;
int j = ;
while (p->next && j < i - )
{
p = p -> next;
++ j;
}
if(!p || j != i - )
return ERROR;
s = (LinkList*) malloc (sizeof(LNode));
s -> data = e;
s -> next = p ->next;
p -> next = s;
return OK;
} //删除元素
Status ListDelete_L(LinkList *L, int i, ElemType *e)
{
LinkList *p = L;
LinkList *q;
int j = ;
while(p -> next && j < i - )
{
p = p -> next;
++ j;
}
if(!(p -> next) || j > i - )
return ERROR;
q = p -> next;
p -> next = q -> next;
*e= q -> data;
free(q);
return OK;
} void MergeList_L(LinkList *La, LinkList *Lb, LinkList **Lc)
{
LinkList *pa = La -> next;
LinkList *pb = Lb -> next;
LinkList *pc = NULL;
(*Lc) = pc = La;
while(pa && pb)
{
if(pa -> data <= pb -> data)
{
pc -> next = pa;
pc = pa;
pa = pa ->next;
}
else
{
pc -> next = pb;
pc = pb;
pb = pb -> next;
}
}
pc -> next = pa ? pa : pb;//插入剩余段
free(Lb);//释放Lb头结点
Lb = NULL; } void Free(LinkList *L)
{
if(L && L -> next)
Free( L -> next);
free(L);
return;
} int main()
{
int selectn,length0,length1,location,e,counti;
LNode *L1 = NULL,*L2 = NULL, *L3 = NULL, *la = NULL, *lc = NULL;
printf("请输入新链表长度 :\n");
scanf("%d", &length0);
CreateList_L(&L1, length0);
printf("请选择 : \n");
printf("1 : 插入元素\n");
printf("2 : 删除元素\n");
printf("3 : 合并链表\n");
scanf("%d", &selectn);
switch (selectn)
{
case :
printf("请输入插入位置 :\n");
scanf("%d", &location);
printf("请输入要插入元素 :\n");
scanf("%d", &e);
if(ListInsert_L(L1, location, e) == OK )
{ //逆序输出
counti = ;
la = L1;
printf("新链表的顺序为 :\n");
while(la -> next)
{
la = la->next;
if(counti++ % )
printf("%-5d",la -> data);
else
printf("%-5d\n",la -> data);
}
}
else
printf("插入异常\n");
printf("\n");
break;
case :
printf("请输入要删除的位置 :\n");
scanf("%d",&location);
if(ListDelete_L(L1, location, &e) == OK)
{
printf("删除成功\n被删除元素为 : %-5d\n", e);
}
else
printf("删除异常\n");
break;
case :
printf("请输入链表2 :\n");
printf("请输入链表长度 :\n");
scanf("%d", &length1);
CreateList_L(&L2, length1);
MergeList_L(L1, L2, &L3);
L2 = NULL;
lc = L3 -> next;
printf("新链表顺序为 :\n");
counti = ;
while(lc)
{ if(counti++ % )
printf("%-5d", lc -> data);
else
printf("%-5d", lc -> data);
lc = lc -> next;
}
printf("\n");
break;
default :
printf("ERROR\n");
break;
}
Free(L1);
L1 = NULL;
L3 = NULL;
system("pause");
return ;
}

Data_Struct(LinkList)的更多相关文章

  1. java学习第15天(Linklist Vector)

    根据集合的分类(上一天有说),首先接触的是ArrayList但是和Collection一样,他没有什么特殊的功能,直接跳过,然后是Vector. 一   Vector A:有特有功能 a:添加 pub ...

  2. Java自己实现双向链表LinkList

    /** * <p> * Node 双向链表实体类 * <p> * * @author <a href="mailto:yangkj@corp.21cn.com& ...

  3. ArrayList和LinkList区别

    ArrayList和LinkList区别 前者是数组的数据结构,后者是链表的数据结构 前者应用于排序和查找,后者应用于插入删除

  4. Java:List,ArrayList和LinkList的区别

    1.大学数据结构中ArrayList是实现了基于动态数组的数据结构,LinkList基于链表的数据结构 2.对于随机访问get和set,ArrayList优于LinkList,因为LinkedList ...

  5. [置顶] Array ArrayList LinkList的区别剖析

    这是一个面试中我们经常被问到的问题 Array.ArrayList.LinkList之间的区别:Array.ArrayList.LinkList均属于泛型的范畴,都用来存放元素,主要区别是Array是 ...

  6. ArrayList 、Vector、 LinkList

    public class TestList {     public static void init(List list)     {         if(list!=null)          ...

  7. LinkList Operation

    链表典型数据结构: #define ElemType int typedef struct LinkNode{ ElemType value; struct LinkNode* next; }; 相比 ...

  8. 链表中LinkList L与LinkList *L 借鉴

    链表中LinkList L与LinkList *L的区别以及(*L).elem,L.elem L->next,(*L)->next的区别typedef struct Node{int el ...

  9. Java集合(2)一 ArrayList 与 LinkList

    目录 Java集合(1)一 集合框架 Java集合(2)一 ArrayList 与 LinkList Java集合(3)一 红黑树.TreeMap与TreeSet(上) Java集合(4)一 红黑树. ...

随机推荐

  1. windows转储文件(dmp)

    1. 何为转储文件      转储文件也就是我们常说的dump文件.可以把转储文件看成软件的某个时刻的一个快照.转储文件一般都是在软件出现问题时手动生成或者程序自动生成.下面我们介绍几种生成转储文件的 ...

  2. .NET Core 2.0 应用程序大小减少50%

    .NET Core 2.0减小体积瘦身官方工具 IL Linker. IL Linker 来源于mono的linker  https://github.com/mono/linker,目前还是预览版本 ...

  3. MySQL常见的三种存储引擎(InnoDB、MyISAM、MEMORY)

    简单来说,存储引擎就是指表的类型以及表在计算机上的存储方式. 存储引擎的概念是MySQL的特点,Oracle中没有专门的存储引擎的概念,Oracle有OLTP和OLAP模式的区分.不同的存储引擎决定了 ...

  4. border-radius值的解析

    border-radius: none | length{1,4} / length{1,4} 其中每一个值可以为 数值或百分比的形式. length/length 第一个lenght表示水平方向的半 ...

  5. 花生壳DDNS为何不支持LetsEncrypt申请

    Inspired by Let's Encrypt Community , thx to sahsanu, jsha, and orzorc. 开端 Lets Encrypt 是一款免费的网站 SSL ...

  6. 基于AXI4总线卷积FPGA加速IP核的尝试

    本文先总结不同AXI IP核的实现的方法,性能的对比,性能差异的分析,可能改进的方面.使用的硬件平台是Zedboard. 不同的AXI总线卷积加速模块的概况 这次实现并逐渐优化了三个版本的卷积加速模块 ...

  7. 【★】KMP算法完整教程

    KMP算法完整教程 全称:                               Knuth_Morris_Pratt Algorithm(KMP算法) 类型:                 ...

  8. 封装bootstrap-treegrid组件

    封装bootstrap-treegrid组件   阅读目录 一.开源的treegrid 1.组件效果预览 2.组件开源地址 二.封装treegrid 1.组件封装的必要性 2.组件封装代码示例 3.封 ...

  9. Java8-初识Lambda

    廉颇老矣,尚能饭否 Java,这位已经20多岁的编程语言,称得上是编程语言界的老大哥了.他曾经攻城略地,碾压各路编程语言小弟,风光无限,不可一世.现在,也是家大业大,江湖地位,很难撼动. 但是,这依然 ...

  10. 团队作业8——第二次项目冲刺(Beta阶段)--第七天

    会议照片: 燃尽图: 项目进展: 所有项目都已完成 进行app测试即使用情况评估 团队贡献比: 队员 角色 团队贡献比 陈麟凤 PM 17% 张志杰 DEV 18% 黄海鸿 TEST 16% 康建灿 ...