单链线性表的实现

//函数结果状态代码#define    TRUE    1
#define    FALSE    0
#define    OK        1
#define    ERROR    0
#define    INFEASIBLE    -1
#define    OVERFLOW    -2
//Status是函数的类型,其值是函数结果状态代码
typedef int Status;
typedef int ElemType;typedef struct LNode{ElemType        data;struct LNode    *next;
}LNode,*LinkList;Status GetElem_L(LinkList L,int i,ElemType &e){//L为带头结点的单链表的头指针。//当第i个元素存在时,其值赋给e并返回OK,否则返回ERRORLNode *p;int j;p=L->next;j=1;        //初始化,p指向第一个结点,j为计数器while(p && j<i){        //顺指针向后查找,直到p指向第i个元素或p为空p=p->next;++j;}if(!p || j>i)    return ERROR;    //第i个元素不存在e=p->data;            //取第i个元素return OK;
}//GetElem_L

Status ListInsert_L(LinkList &L,int i,ElemType e){//在带头结点的单链线性表L中第i个位置之前插入元素e
    LinkList p,s;int j;p=L; j=0;while(p && j<i-1){    //寻找第i-1个结点p=p->next;++j;}if(!p || j>i-1)    return ERROR;    //i小于1或者大于表长加1s=(LinkList)malloc(sizeof(LNode));    //生成新结点s->data=e;s->next=p->next;        //插入L中p->next=s;return OK;
}//ListInsert_L

Status ListDelete_L(LinkList &L,int i,ElemType e){//在带头结点的单链线性表L中,删除第i个元素,并由e返回其值
    LinkList p,q;int j;p=L; j=0;while(p->next && j<i-1){    //寻找第i个结点,并令p指向其前趋p=p->next;++j;}if(!(p->next) || j>i-1)    return ERROR;    //删除位置不合理q=p->next;p->next=q->next;                //删除并释放结点e=q->data;free(q);return OK;
}//ListInsert_Lvoid CreateList_L(LinkList &L,int n){//逆位序输入n个元素的值,建立带表头结点的单链线性表L。int i;LinkList p;L=(LinkList)malloc(sizeof(LNode));L->next=NULL;        //先建立一个带头结点的单链表for(i=n;i>0;--i){p=(LinkList)malloc(sizeof(LNode));        //生成新结点scanf("%d",&p->data);                        //输入元素值p->next=L->next;L->next=p;                //插入到表头
    }}//CreateList_Lvoid MergeList_L(LinkList &La,LinkList &Lb,LinkList &Lc){//已至单链线性表La和Lb的元素按值非递减排序//归并La和Lb得到新的单链线性表Lc,Lc的元素也按值非递减排序。
    LinkList pa,pb,pc;pa=La->next;pb=Lb->next;Lc=pc=La;            //用La的头结点作为Lc的头结点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的头结点
}//MergeList_L

 

 

#include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"
#include "malloc.h"
#include "LinkList.h"int main(int argc, char* argv[])
{LinkList L;ElemType e;ElemType in;int n;n=5;CreateList_L(L,5);GetElem_L(L,n,e);printf("GetElem %d: %d ",n,e);in=6;ListInsert_L(L,5,in);GetElem_L(L,n,e);printf("GetElem %d: %d ",n,e);return 0;
}

 

转载于:https://www.cnblogs.com/13yan/p/3417150.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377591.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

时间模块,带Python示例

Python时间模块 (Python time Module) The time module is a built-in module in Python and it has various functions that require to perform more operations on time. This is one of the best modules in Python that used to solve various real-life time-related pro…

五、torchvision

一、下载CIFAR-10数据集 CIFAR-10数据集官网 通过阅读官网给的解释可以大概了解到&#xff0c;一共6w张图片&#xff0c;每张图片大小为3232&#xff0c;5w张训练图像&#xff0c;1w张测试图像&#xff0c;一共由十大类图像。 CIFAR10官网使用文档 torchvision.datasets.CIF…

leetcode 69. x 的平方根 思考分析

题目 实现 int sqrt(int x) 函数。 计算并返回 x 的平方根&#xff0c;其中 x 是非负整数。 由于返回类型是整数&#xff0c;结果只保留整数的部分&#xff0c;小数部分将被舍去。 示例 1: 输入: 4 输出: 2 示例 2: 输入: 8 输出: 2 说明: 8 的平方根是 2.82842…, 由于返回…

背包问题 小灰_小背包问题

背包问题 小灰Prerequisites: Algorithm for fractional knapsack problem 先决条件&#xff1a; 分数背包问题算法 Here, we are discussing the practical implementation of the fractional knapsack problem. It can be solved using the greedy approach and in fraction…

360浏览器兼容问题

360浏览器兼容问题 360浏览器又是一大奇葩&#xff0c;市场份额大&#xff0c;让我们不得不也对他做些兼容性处理。 360浏览器提供了两种浏览模式&#xff0c;极速模式和兼容模式&#xff0c;极速模式下是webkit内核的处理模式&#xff0c;兼容模式下是与IE内核相同的处理模式。…

转 设计师也需要了解的一些前端知识

一、常见视觉效果是如何实现的 一些事 关于文字效果 互联网的一些事 文字自身属性相关的效果css中都是有相对应的样式的&#xff0c;如字号、行高、加粗、倾斜、下划线等&#xff0c;但是一些特殊的效果&#xff0c;主要表现为ps中图层样式中的效果&#xff0c;css是无能为力的…

六、DataLoader

一、DataLoader参数解析 DataLoader官网使用手册 参数描述dataset说明数据集所在的位置、数据总数等batch_size每次取多少张图片shuffleTrue乱序、False顺序(默认)samplerbatch_samplernum_workers多进程&#xff0c;默认为0采用主进程加载数据collate_fnpin_memorydrop_las…

单调栈 leetcode整理(一)

目录单调栈知识402. 移掉K位数字1673. 找出最具竞争力的子序列316. 去除重复字母&#xff08;1081. 不同字符的最小子序列&#xff09;321. 拼接最大数单调栈知识 单调栈就是一个内部元素有序的栈&#xff08;大->小 or 小->大&#xff09;&#xff0c;但是只用到它的一…

数字签名 那些密码技术_密码学中的数字签名

数字签名 那些密码技术A signature is usually used to bind signatory to the message. The digital signature is thus a technique that binds a person or the entity to the digital data. This binding ensures that the person sending the data is solely responsible …

七、torch.nn

一、神经网络模块 进入到PyTorch的torch.nnAPI学习页面 PyTorch提供了很多的神经网络方面的模块&#xff0c;NN就是Neural Networks的简称 二、Containers torch.nn下的Containers 一共有六个模块&#xff0c;最常用的就是Module模块&#xff0c;看解释可以知道&#xff0c…

Java多线程初学者指南(8):从线程返回数据的两种方法

本文介绍学习Java多线程中需要学习的从线程返回数据的两种方法。从线程中返回数据和向线程传递数据类似。也可以通过类成员以及回调函数来返回数据。原文链接 从线程中返回数据和向线程传递数据类似。也可以通过类成员以及回调函数来返回数据。但类成员在返回数据和传递数据时有…

【C++进阶】 遵循TDD原则,实现平面向量类(Vec2D)

目录1、明确要实现的类的方法以及成员函数2、假设已经编写Vec2D&#xff0c;根据要求&#xff0c;写出测试代码3、编写平面向量类Vec2D,并进行测试4、完整代码5、最终结果1、明确要实现的类的方法以及成员函数 考虑到效率问题&#xff0c;我们一般将函数的参数设置为引用类型。…

Keilc的中断号计算方法

中断号码 (中断向量-3)/8转载于:https://www.cnblogs.com/yuqilihualuo/p/3423634.html

md5模式 签名_MD的完整形式是什么?

md5模式 签名医师&#xff1a;医学博士/常务董事 (MD: Doctor of Medicine / Managing Director) 1)医学博士&#xff1a;医学博士 (1) MD: Doctor of Medicine) MD is an abbreviation of a Doctor of Medicine degree. In the field of Medicine, it is the main academic de…

八、卷积层

一、Conv2d torch.nn.Conv2d官网文档 torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride1, padding0, dilation1, groups1, biasTrue, padding_modezeros, deviceNone, dtypeNone) 参数解释官网详情说明in_channels输入的通道数&#xff0c;如果是彩色照片通道…

HTMl5结构元素:header

页眉header 页眉将是页面加载的第一个元素&#xff0c;包含了站点的标题、logo、网站导航等。<header> <div class"container_16"> <div class"logo"> <h1><a href"index.html"><strong>Real</st…

【C++grammar】左值、右值和将亡值

目录C03的左值和右值C11的左值和右值将亡值在C03中就有相关的概念 C03的左值和右值 通俗的理解&#xff1a; (1) 能放在等号左边的是lvalue (2) 只能放在等号右边的是rvalue (3) lvalue可以作为rvalue使用 对于第三点可以举个例子&#xff1a; int x ; x 6; //x是左值&#…

scala字符串的拉链操作_在Scala中对字符串进行操作

scala字符串的拉链操作Scala字符串操作 (Scala strings operation) A string is a very important datatype in Scala. This is why there are a lot of operations that can be done on the string object. Since the regular operations like addition, subtraction is not v…

九、池化层

一、Pooling layers Pooling layers官网文档 MaxPool最大池化层下采样 MaxUnpool最大池化层上采样 AvgPool最大池化层平均采样 例如&#xff1a;池化核为(3,3)&#xff0c;输入图像为(5,5)&#xff0c;步长为1&#xff0c;不加边 最大池化就是选出在池化核为单位图像中的最大…

[分享]SharePoint移动设备解决方案

老外写的一个PPT&#xff0c;讲SharePoint在移动领域的应用&#xff0c;2012年最新的&#xff0c;有iPad哟。/Files/zhaojunqi/SharePoint2010andMobileDevices.pdf 转载于:https://www.cnblogs.com/zhaojunqi/archive/2012/04/12/2444712.html