顺序表代码总结——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 R6 集群手工配置VIP案例
经常有用户问,V8R6集群搭建时没有配置VIP,搭建完成后,如何添加VIP?以下向大家介绍下手动添加VIP 的过程. 一.操作系统环境 操作系统(UOS): root@uos01:~# cat /et ...
- aardio 编程语言快速入门 —— 语法速览
本文仅供有编程基础的用户快速了解常用语法.如果『没有编程基础』 ,那么您可以通过学习任何一门编程语言去弥补你的编程基础,不同编程语言虽然语法不同 -- 编程基础与经验都是可以互通的.我经常看到一些新手 ...
- Lua CallbackHell优化
概述 在异步操作中,常常要使用回调.但是,回调的嵌套常常会导致逻辑混乱,一步错步步错,难以维护.在Lua中,可以使用协程进行优化. 问题分析 模拟一个回合制游戏攻击过程 local function ...
- DFS文件夹无法访问
最近DFS的文件服务器出现了部分文件和文件夹无法访问的情况.客户端直接访问DFS成员的共享文件夹时有是会出现Element not found的错误.有时打开文件的时候会出现文件不存在,或者你没有权限 ...
- 使用django_registration框架实现用户的注册与激活
1.前言 本节内容是在以下环境中实现的. python version: 3.7 Django version: 3.1.1 Django-registration version: 3.1.1 如版 ...
- portainer 1.24.2 升级到 portainer-ce 最新版
官方升级步骤文档: https://docs.portainer.io/v/ce-2.9/start/upgrade/docker 若是现在的版本是 portainer-ce 2.0.0 ,看Opti ...
- Pod的滚动升级过程
- 内网横向渗透 之 ATT&CK系列一 之 信息收集
前言 靶机下载地址:ATT&CK 拓扑图: 通过模拟真实环境搭建的漏洞靶场,完全模拟ATK&CK攻击链路进行搭建,形成完整个闭环.虚拟机默认密码为hongrisec@2019. 环境搭 ...
- 前端微信登录获取code,userInfo,openid
getUser(e) { wx.getUserProfile({ desc: '用户完善会员资料', success: res => { let userInfo = res.userInfo; ...
- 没有使用IaC的DevOps系统都是耍流氓
作为现代软件工程的基础实践,基础设施即代码(Infrastructure as Code, IaC)是云原生.容器.微服务以及DevOps背后的底层逻辑.应该说,以上所有这些技术或者实践都是以基础设施 ...