顺序表代码总结——SqList
在C++编译器下可直接运行
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
//顺序表存储结构
//动态存储结构
typedef int ElemType;
typedef struct SqList
{
ElemType *pList;
int length;
int listSize;
}SqList;
#define INIT_SIZE 10
#define INCRE_SIZE 10
#define N 10
//创建一个顺序表
void initial(SqList &L)
{
L.pList = (ElemType *) malloc(INIT_SIZE * sizeof(ElemType));
L.length = 0;
L.listSize = INIT_SIZE;
}
//从顺序表中删除第i个元素
int deleteIthElem(SqList &L,int ith,int &e)
{
if(ith <= 0 || ith > L.length)
return 0;
if(ith < L.length)
{
e = L.pList[ith - 1];
for(int curIndex = ith;curIndex < L.length;curIndex++)
{
L.pList[curIndex - 1] = L.pList[curIndex];
}
}
--L.length;
return 1;
}
//在i个位置插入e
int insert1(ElemType e,SqList &L,int ith)
{
//判断i是否合法
if(ith < 1 || ith > L.length)
return 0;
//判断插入空间是否充足,不充足,申请新的空间,并将老表中中的元素复制进去
if(L.length >= L.listSize)
{
ElemType *pNew = (ElemType *) malloc(sizeof(ElemType) * (2 * L.listSize));
for(int index = 0;index < L.length;index++)
{
pNew[index] = L.pList[index];
}
L.pList = pNew;
L.listSize = 2 * L.listSize;
}
//插入i
for(int curIndex = L.length - 1;curIndex >= ith - 1;--curIndex)
{
L.pList[curIndex + 1] = L.pList[curIndex];
}
L.pList[ith - 1] = e;
++ L.length;
return 1;
}
//从顺序表中删除最小元素,空出位置由最后一个元素填补
void delMinElem(SqList &L)
{
int minIndex = 0;
for(int index = 1;index < L.length; ++index)
{
if(L.pList[index] < L.pList[minIndex])
{
minIndex = index;
}
}
L.pList[minIndex] = L.pList[L.length - 1];
--L.length;
}
void printFunc(SqList L)
{
for(int i = 0; i < L.length;i++)
{
printf("%d ", L.pList[i]);
}
printf("\n");
printf("Length: %d size: %d", L.length,L.listSize);
printf("\n");
}
void values(SqList &L)
{
L.length = N;
for(int i = 0;i < L.length ;i++)
{
if(i < 3)
L.pList[i] = i;
else if(i < 7)
L.pList[i] = 4;
else
L.pList[i] = i ;
}
//printFunc(L);
}
//在五序表中删除值在S到T之间对所有元素,包含st;
//在原有存储空间上一次访问元素并对访问元素进行过滤操作,达delete到对特定一些元素删除对方法成为 过滤法
//在一个表的存储空间中进行操作
//当前访问子序列长度 >= 当前结果子序列长度
int deleteElem(ElemType s,ElemType t,SqList &L)
{
int curLength = 0;//当前子序列的长度
if(s > t)
return 0;
if(s <= t)
{
for(int i = 0;i < L.length;i++)
{
if(L.pList[i] < s || L.pList[i] > t)
{
L.pList[curLength] = L.pList[i];
curLength++;
}
}
L.length = curLength;
}
return 1;
}
//在非递减顺序表中删除值在[s,t]的所有元素
//非递减:前驱永远小于等于后继
//非递增:前驱永远大于等于后继////不单调相区别!!!!!!!
//在一个表中不知道哪几个位置要删除,不能明确一次性删除哪几个位置,则用过滤法;
//如果能明确删除位置,一次性确定删除哪些位置,则用 偏移法(两边夹逼)
int deleteElem1(ElemType s,ElemType t,SqList &L)
{
//从前往后找第一个大于等于s且小于等于t的元素位置
int sIndex = -1;
for(int i = 0;i < L.length;i++)
{
if(L.pList[i] >= s)
{
sIndex = i;
break;
}
}
//从后往前找第一个小于等于t且大于s的元素的位置
if(sIndex == -1)
return 0;
int tIndex = L.length - 1;
for(int i = L.length - 1;i >= 0;i-- )
{
if(L.pList[i] >= s && L.pList[i] <= t)
{
tIndex = i + 1;
break;
}
}
int dela = tIndex - sIndex ;
for(int i = tIndex;i < L.length;i++)
{
L.pList[i - dela] = L.pList[i];
}
L.length -= dela;
return 1;
}
//删除非递减顺序表L 中的重复元素
//当前访问序列 》= 删除后对结果子序列 过滤法
//当前访问元素跟结果子序列末尾元素相同,则为重复元素,将其过滤;不相同则追加至结果子序列
void deleteRepeatElem(SqList &L)
{
int curLength = 0;
for(int i = 0;i < L.length;i++)
{
if(curLength == 0 || L.pList[i] != L.pList[curLength - 1])//不等于其前驱×××××××××××××!!!!!!!!!!
{
L.pList[curLength] = L.pList[i];
curLength++;
}
}
L.length = curLength;
}
//最小值/最大值法
//依次取两个表中最小者插入到新表,直到其中一个表的元素都被插入到新表中为止,最后将剩余表的元素从小到大顺序依次追加到新表末尾
void combine(SqList &La,SqList &Lb,SqList &Lc)
{
Lc.pList = (ElemType *) malloc(sizeof(ElemType) * (La.length + Lb.length));
Lc.listSize = La.length + Lb.length;
Lc.length = 0;
int i = 0;//A 表从第一个位置开始取
int j = Lb.length - 1;//B 表从末尾开始取
while( i < La.length && j >= 0)
{
if(La.pList[i] < Lb.pList[j])
{
Lc.pList[Lc.length++] = La.pList[i++];
}
else
{
Lc.pList[Lc.length++] = Lb.pList[j--];
}
}
while(i < La.length)
{
Lc.pList[Lc.length++] = La.pList[i++];
}
while(j >= 0)
{
Lc.pList[Lc.length++] = Lb.pList[j--];
}
free(La.pList);
free(Lb.pList);
La.length = 0;
Lb.length = 0;
La.listSize = 0;
Lb.listSize = 0;
}
//A 有m+n个存储空间,A递增表,B递减表,无相同元素,合并两表
//最大值法 最大值存在最后位置
//有序表 多表归并 :最小值/最大值法×××××××××××××××××××××××××××
void combineplus(SqList &La,SqList &Lb)
{
int i = La.length - 1;//指向A表最后一个位置
int j = 0;//指向B表第一个位置
int curLength = 0;//当前结果序列长度
while(i >= 0 && j < Lb.length)
{
if(La.pList[i] > Lb.pList[j])
{
La.pList[La.listSize - curLength++ - 1] = La.pList[i--];
// curLength++;
// i--;
}
else
{
La.pList[La.listSize - curLength++ - 1] = Lb.pList[j++];
// curLength++;
//j++;
}
}
while(j < Lb.length )
{
La.pList[La.listSize - curLength++ - 1] = Lb.pList[j++];
//curLength++;
// j++;
}
La.length += Lb.length;
Lb.length = 0;
}
//表A 前r个元素递增有序,后 n-r个元素递减有序,将表A进行升序排序
//插入排序
//取到的待排序列与当前元素比较,
//如果当前元素小于等于待排元素,打么就将其插入到当前元素后面
void insertSort(int r,SqList &La)
{
int insertIndex = r;
while(insertIndex < La.length)//待排序元素索引范围
{
int i = insertIndex - 1;//排序好序列最后一个位置
ElemType e = La.pList[insertIndex];
while(i >= 0)//依次访问排序好序列中对元素
{
if(La.pList[i] <= e)//找到插入位置
{
break;
}
La.pList[i+1] = La.pList[i];//往后挪动一个位置
i--;//访问前一个元素
}
La.pList[i + 1] = e;
insertIndex++;//取下一个待排元素
}
}
//给定两个非空集合A和B,分别用升序表La和Lb存储,
//设计算法求解A交B(共同元素只能在求解过程中出现一次)
//最小值法
//算法思想:指针indexLa、indexLb分别指向La和Lb的第一个元素。如果indexLa
//指向的元素小于indexLb指向的元素,移动indexLa访问下一个元素;反之,则移动indexLb
//访问下一个元素;如果两个指针所指元素相同,则发现共有元素,将其保存后再同时移动两个指针
//重复上述步骤,直到一个表的元素访问完为止。
void intersect(SqList &La,SqList Lb)//存储到A表
{
int curLength = 0;
int i = 0;
int j = 0;
while(i < La.length && j < Lb.length)//过滤法
{
if(La.pList[i] < Lb.pList[j])
{
i++;
}
if(La.pList[i] > Lb.pList[j])
{
j++;
}
// if(La.pList[i] == Lb.pList[j])//均为单调递增
// {
// La.pList[curLength] = La.pList[i];
// curLength++;
// ++i;
// ++j;
// }
if(La.pList[i] == Lb.pList[j])//均为非递减
{
if(curLength == 0 || La.pList[curLength - 1] != La.pList[i])//排除相同
{
La.pList[curLength] = La.pList[i];
curLength++;
}
i++;
j++;
}
}
La.length = curLength;
}
//给定两个非空集合A和B,分别用升序表La和Lb存储,
//设计算法求解A-B(共同元素只能在求解过程中出现一次)
//最小值法
void except(SqList La,SqList Lb,SqList &Lc)//当非递减时有bug!!!
{
int indexLa = 0;
int indexLb = 0;
int curLength = 0;
while(indexLa < La.length && indexLb < Lb.length)
{
if(La.pList[indexLa] < Lb.pList[indexLb] )
{
if(La.pList[indexLa] == La.pList[indexLa - 1])
{
indexLa++;
}
else if(La.pList[indexLa] < Lb.pList[indexLb])
{
Lc.pList[curLength] = La.pList[indexLa];
curLength++;
indexLa++;
}
}
else if(La.pList[indexLa] > Lb.pList[indexLb] )
{
if(Lb.pList[indexLb] == Lb.pList[indexLb - 1])
{
indexLb++;
}
else if(La.pList[indexLa] > Lb.pList[indexLb])
{
Lc.pList[curLength] = Lb.pList[indexLb];
curLength++;
indexLb++;
}
}
else{
indexLa++;
indexLb++;
}
}
while(indexLa < La.length)
{
Lc.pList[curLength] = La.pList[indexLa];
curLength++;
indexLa++;
}
while(indexLb < Lb.length)
{
if(Lb.pList[indexLb] == Lb.pList[indexLb - 1])
{
indexLb++;
if(indexLb >= Lb.length)
{
Lb.length++;
}
}
Lc.pList[curLength] = Lb.pList[indexLb];
curLength++;
indexLb++;
}
Lc.length = curLength;//Lc.length超限?
}
//设计算法逆置顺序表L
int reversel(SqList L,int low,int high)//指针传递地址值不需要引用
{
//low = 0;
//high = L.length - 1;
if(high <=0)
{
return 0;
}
while(low < high)
{
ElemType temp = L.pList[low];
L.pList[low] = L.pList[high];
L.pList[high] = temp;
low++;
high--;
}
return 1;
}
//循环左移
void rol(int r,SqList L)
{
reversel(L,0,L.length - 1);
reversel(L,0,L.length - 1 - r);
reversel(L,L.length - r,L.length - 1);
}
int main()
{
// SqList L;
// initial(L);
// insert(1,L,0);
// insert(2,L,1);
// insert(3,L,2);
// values(L);
// printFunc(L);
// int ret = insert(100,L,1);
// if(ret == 1)
// {
// //printf("success\n");
// printFunc(L);
// }
//
// delMinElem(L);
// printFunc(L);
// int e;
// int ret1 = deleteIthElem(L,1,e);
// if(ret1 == 1)
// {
// printf("delete: %d\n",e);
// printFunc(L);
// }
// int ret2 = deleteElem(4,6,L);
// if(ret2 == 1)
// printFunc(L);
//
// int ret3 = deleteElem1(3,5,L);
// if(ret3 == 1)
// {
// printFunc(L);
// }
// deleteRepeatElem(L);
// printFunc(L);
SqList La;
SqList Lb;
SqList Lc;
La.pList = (ElemType *) malloc(sizeof(ElemType) * 10);
Lb.pList = (ElemType *) malloc(sizeof(ElemType) * 10);
Lc.pList = (ElemType *) malloc(sizeof(ElemType) * 20);
La.listSize = 10;
Lb.listSize = 10;
Lc.listSize = 20;
La.length = 10;
Lb.length = 10;
Lc.length = 0;
for(int i = 0;i < 10;i++)
{
La.pList[i] = i;
}
// for(int i = 0;i < 10;i++)
// {
// Lb.pList[i] = i * 2;
// }
printFunc(La);
//printFunc(Lb);
//SqList Lc;
//combineplus(La,Lb);
//insertSort(10,La);
//intersect(La,Lb);
//except(La,Lb,Lc);
rol(4,La);
printFunc(La);
//printFunc(Lb);
// printFunc(Lc);
}
顺序表代码总结——SqList的更多相关文章
- 数据结构顺序表中Sqlist *L,&L,Sqlist *&L
//定义顺序表L的结构体 typedef struct { Elemtype data[MaxSize]: int length; }SqList; //建立顺序表 void CreateList(S ...
- hrbustoj 1545:基础数据结构——顺序表(2)(数据结构,顺序表的实现及基本操作,入门题)
基础数据结构——顺序表(2) Time Limit: 1000 MS Memory Limit: 10240 K Total Submit: 355(143 users) Total Accep ...
- 线性表之顺序表(C语言实现)
线性表是从数据元素的逻辑结构上定义的. 这种数据元素的逻辑结构的特征如下: 1.除开第一个和最后一个元素之外.所有元素都有一个前驱元素和后继元素. 2.第一个元素无前驱元素,但有后继元素. 3.最后一 ...
- C语言顺序表的实现
今天本来想写段代码练练手,想法挺好结果,栽了个大跟头,在这个错误上徘徊了4个小时才解决,现在分享出来,给大家提个醒,先贴上代码: /********************************** ...
- c语言进阶13-线性表之顺序表
一. ACM算法:顺序表的查找 顺序表的查找指获取顺序表的第i个元素.对于线性表的顺序存储结构来说,如果我们要实现获取元素的操作(GetElem),即将线性表L中的第i个位置元素值返回.就程序而言,只 ...
- 【数据结构 Python & C++】顺序表
用C++ 和 Python实现顺序表的简单操作 C++代码 // Date:2019.7.31 // Author:Yushow Jue #include<iostream> using ...
- 删除顺序表L中下标为p(0<=p<=length-1)的元素,成功返回1,不成功返回0,并将删除元素的值赋给e
原创:转载请注明出处. [天勤2-2]删除顺序表L中下标为p(0<=p<=length-1)的元素,成功返回1,不成功返回0,并将删除元素的值赋给e 代码: //删除顺序表L中下标为p(0 ...
- 顺序表的C语言实现
在现实应用中,有两种实现线性表数据元素存储功能的方法,分别是顺序存储结构和链式存储结构.顺序表操作是最简单的操作线性表的方法.下面的代码实现了顺序表的几种简单的操作.代码如下 //start from ...
- c数据结构 顺序表和链表 相关操作
编译器:vs2013 内容: #include "stdafx.h"#include<stdio.h>#include<malloc.h>#include& ...
- 基于C++的顺序表的实现
顺序表,是数据结构中按顺序方式存储的线性表,又称向量.具有方便检索的特点.以下,是笔者学习是基于C++实现的顺序表代码,贴上来当网页笔记用. #include <iostream> usi ...
随机推荐
- kingbaseES R3 集群修改data路径测试案例
案例说明: 默认KingbaseES R3集群部署后,数据存储目录(data)在/home/kingbase下,部署时不能更改:本案例是在部署完成后,迁移data目录到其他指定的存储位置. 数据库版本 ...
- 全网最简单的大文件上传与下载代码实现(React+Go)
前言 前段时间我需要实现大文件上传的需求,在网上查找了很多资料,并且也发现已经有很多优秀的博客讲了大文件上传下载这个功能. 我的项目是个比较简单的项目,并没有采用特别复杂的实现方式,所以我这篇文章的目 ...
- day37-IO流04
JavaIO流04 4.常用的类03 4.4节点流和处理流02 4.4.5对象处理流-ObjectInputStream和ObjectOutputStream 1.序列化和反序列化 例子1: 看一个需 ...
- 系统无法启动inaccessible boot device
近日有一台Windows 2016遇到了系统无法启动的问题,出现错误inaccessible boot device.发现系统可以进入故障恢复模式,看来问题还不大.因为进入故障恢复模式的时候自动识别的 ...
- Java 快速开发几 MB 独立 EXE,写图形界面很方便
Java 写的桌面软件带上运行时只有 6 MB,而且还是独立 EXE 文 件,是不是难以置信? 想一想 Electron 没写多少功能就可能超过百 MB 的体积,Java 写的桌面软件算不算得上小.轻 ...
- day03-2无异常退出
多用户即时通讯系统03 4.编码实现02 4.3功能实现-无异常退出系统 4.3.1思路分析 上述代码运行时,在客户端选择退出系统的时候,可以发现程序并没有停止运行,原因是: 退出时,程序将循环标志l ...
- MySQL 安装(二进制版)
MySQL 的安装方式一般分为三种,二进制版本.编译版本.RPM 包.比较常见的是二进制版本安装,方便简单,相对于编译安装,如果不是追求极致性能,使用起来差别不大.本次教程以二进制版本为例,系统为 c ...
- Loki 简明教程
文章转载参考自:https://jishuin.proginn.com/p/763bfbd2ac34 Loki 是 Grafana Labs 团队最新的开源项目,是一个水平可扩展,高可用性,多租户的日 ...
- SpringBoot 项目部署(初级)
之前的项目一直在本地电脑上写,最近需要将项目部署到服务器上进行联调测速度.于是,在网上搜集资料后简单的进行一下总结. 由于本次打包部署是为了测试,于是很多内容做的还不算详尽,只是将项目简单的打包为ja ...
- 关于Redhat-7.x-下docker的安装记录
今天因公司项目,需要部署docker环境,能根据指定的镜像创建容器 于是首先就得先部署docker环境,过程记录如下: 在Redhat 7.x - (aws上的Redhat) 环境下部署过程 1.安装 ...