C 线性表的链式存储实现及插入、删除等操作示例
一、链式存储的优势
线性表的存储可以通过顺序存储或链式存储实现,其中顺序存储基于数组实现(见本人上一篇博客),在进行插入删除等操作时,需对表内某一部分元素逐个移动,效率较低。而链式结构不依赖于地址连续的存储空间,可以克服数组表现线性表的缺陷。
二、基于链式存储线性表的基本操作
2.1 PtrToLNode Creat(int length):创建一个长度为length的线性表
//创建长度为length的链表
PtrToLNode Creat(int length)
{
PtrToLNode pHead = (PtrToLNode)malloc(sizeof(struct LNode));//链表头
PtrToLNode p0 = pHead;//新创建链表节点地址
PtrToLNode p1 = pHead;//链表尾节点地址
p0->Data = 0;
int i = 1;
for (i = 1; i < length; i++)
{
p0 = (PtrToLNode)malloc(sizeof(struct LNode));
p0->Data = i;
p1->pNext = p0;//新创建节点的首地址是上一节点的pNext
p1 = p0;//更新尾节点
}
p1->pNext = NULL;//最后有一个节点的pNext为NULL
return pHead;
}
2.2 int Length(PtrToLNode ptr):求表头为ptr的链表的长度
//求表长
int Length(PtrToLNode ptr)
{
int len = 0;
while (ptr != NULL)
{
len++;
ptr = ptr->pNext;
}
return len;
}
3.3 int FindKth(PtrToLNode ptr, int n):求表头为ptr的链表第n个结点的Data值
//求第n个结点的Data
int FindKth(PtrToLNode ptr, int n)
{
if (n > Length(ptr))
{
printf("该节点不在链表范围内");
system("pause");
return -1;
}
int i = 1;
for (i = 1; i < n; i++)
{
ptr = ptr->pNext;
}
int result = ptr->Data;
return result;
}
3.4 int Find(PtrToLNode ptr, int num):求表头为ptr的链表中第一个Data值为num的节点的序号
//求链表中第一个Data值为num的节点的序号
int Find(PtrToLNode ptr, int num)
{
int n = 1;//序号从1开式计
while (ptr->Data != num && n <= Length(ptr))
{
n++;
ptr = ptr->pNext;
}
if (n > Length(ptr))
{
printf("该链表内无Data为%d的节点\n", num);
system("pause");
return -1;
}
return n;
}
3.5 PtrToLNode Insert(PtrToLNode ptr, int n, int num):在第n个节点处插入Data值为num的节点
//在第n个节点处插入Data值为num的节点
PtrToLNode Insert(PtrToLNode ptr, int n, int num)
{
PtrToLNode head = ptr;
if (n > Length(ptr) + 1)
{
printf("请重新输入插入位置");
system("pause");
return NULL;
}
if (n == 1)//n=1即插入新的表头
{
PtrToLNode pNewHead = (PtrToLNode)malloc(sizeof(struct LNode));
pNewHead->Data = num;
pNewHead->pNext = ptr;
return pNewHead;
}
int i = 1;
for (i = 1; i < n-1; i++)//找出第n-1个节点的首地址
{
ptr = ptr->pNext;
}
PtrToLNode pTem = ptr->pNext;
PtrToLNode pNew = (PtrToLNode)malloc(sizeof(struct LNode));
pNew->Data = num;
ptr->pNext = pNew;
pNew->pNext = pTem;
return head;
}
3.6 PtrToLNode Delete(PtrToLNode ptr, int n):删除第n个节点
//删除第n个节点
PtrToLNode Delete(PtrToLNode ptr, int n)
{
PtrToLNode head = ptr;
if (n > Length(ptr))
{
printf("请重新输入删除位置");
system("pause");
return NULL;
}
if (n == 1)
{
PtrToLNode pNewHead = ptr->pNext;
free(ptr);//注意将删除的节点free掉
return pNewHead;
}
int i = 1;
for (i = 1; i < n - 1; i++)//找出第n-1个节点的首地址
{
ptr = ptr->pNext;
}
PtrToLNode pTem = (ptr->pNext)->pNext;
free(ptr->pNext);
ptr->pNext = pTem;
return head;
}
注:本程序中节点序号都是从1计起
C 线性表的链式存储实现及插入、删除等操作示例的更多相关文章
- C++线性表的链式存储结构
C++实现线性表的链式存储结构: 为了解决顺序存储不足:用线性表另外一种结构-链式存储.在顺序存储结构(数组描述)中,元素的地址是由数学公式决定的,而在链式储存结构中,元素的地址是随机分布的,每个元素 ...
- C++编程练习(2)----“实现简单的线性表的链式存储结构“
单链表采用链式存储结构,用一组任意的存储单元存放线性表的元素. 对于查找操作,单链表的时间复杂度为O(n). 对于插入和删除操作,单链表在确定位置后,插入和删除时间仅为O(1). 单链表不需要分配存储 ...
- 数据结构-线性表的链式存储相关算法(C语言实现)
链表的简单介绍 为什么需要线性链表 当然是为了克服顺序表的缺点,在顺序表中,做插入和删除操作时,需要大量的移动元素,导致效率下降. 线性链表的分类 按照链接方式: 按照实现角度: 线性链表的创建和简单 ...
- 线性表 顺序存储 链式存储 ---java实现
首先抽象出一个线性表抽象类(包括主要的增删操作) public abstract class MyAbstractList<E> { public abstract void add(E ...
- 线性表的链式存储——C语言实现
SeqList.h #ifndef _WBM_LIST_H_ #define _WBM_LIST_H_ typedef void List; typedef void ListNode; //创建并且 ...
- typedef struct LNode命名结构指针(线性表的链式存储)
一.typedef 关键字 1. 简介: typedef工具是一个高级数据特性,利用typedef可以为某一些类型自定义名称. 2. 工作原理: 例如我们定义链表的存储结构时,需要定义结点的存储数据元 ...
- 线性表的链式存储C语言版
#include <stdio.h> #include <malloc.h> #define N 10 typedef struct Node { int data; stru ...
- 线性表的链式存储结构的实现及其应用(C/C++实现)
存档----------- #include <iostream.h> typedef char ElemType; #include "LinkList.h" voi ...
- javascript实现数据结构:线性表--线性链表(链式存储结构)
上一节中, 线性表的顺序存储结构的特点是逻辑关系上相邻的两个元素在物理位置上也相邻,因此可以随机存取表中任一元素,它的存储位置可用一个简单,直观的公式来表示.然后,另一方面来看,这个特点也造成这种存储 ...
随机推荐
- 安卓学习 intent
其实学习了好几个星期了,是看老罗的视频,但进度太慢 今天 换了一本书 Intent 切换页面 啊啊啊啊 CompentName comp=new CompentName(MainActivity.th ...
- Scania SDP3 2.38.2.37.0 Download, Install, Activate: Confirmed
Download: Scania Diagnos & Programmer SDP3 2.38.2.37.0 free version and tested version SDP3 2.38 ...
- table 的部分使用,固定行,固定列等
主要是用多张table表格实现 <!DOCTYPE html> <html lang="en"> <head> <meta charset ...
- sort 对多列进行排序
sort -t '\t' -k 3,3 -k 2,2 文件名 # 先对第三列进行排序,然后再对第二列进行排序
- Vue 制作简易计算器
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- dt常用类
经常使用的一些datatable的操作,包括一些过滤去重的方法 using System; using System.Collections; using System.Collections.Gen ...
- dom4j移除节点不成功
在使用dom4j的时候想移除xml节点,经常使用remove来移除节点,通过整个文档或者根节点删除某一个子节点,但如果子节点不是儿子节点,在dom4j中就无法删除.在dom4j中移除节点必须使用父节点 ...
- 使用C#重写网上的60行 Javascript 俄罗斯方块源码 (带注释)
在很久很久以前,就已经看过 60行Js的俄罗斯方块源码.无奈当时能力不够看明白,当时觉得就是个神作. 现在总算有空再看了,顺便用c#实现一遍(超过60行),顺道熟悉下Js API. 网上其他博客也有分 ...
- 图解HTTP第四章
:返回结果的 HTTP 状态码 1>状态码告知从服务器端返回的请求结果 状态码如 200 OK,以 3 位数字和原因短语组成 数字中的第一位指定了响应类别,后两位无分类 2XX 成功常用的状态码 ...
- Eclipse neon 4.6 安装tomcat
问题: Eclipse neon 4.6并没有内置Tomcat,所以当我产生想要导入.war,并部署到服务器时,会看到创建服务处是下面的情况: 也就是说,没有tomcat服务可以选择:为此我需配置To ...