在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的更多相关文章

  1. 数据结构顺序表中Sqlist *L,&L,Sqlist *&L

    //定义顺序表L的结构体 typedef struct { Elemtype data[MaxSize]: int length; }SqList; //建立顺序表 void CreateList(S ...

  2. hrbustoj 1545:基础数据结构——顺序表(2)(数据结构,顺序表的实现及基本操作,入门题)

    基础数据结构——顺序表(2) Time Limit: 1000 MS    Memory Limit: 10240 K Total Submit: 355(143 users) Total Accep ...

  3. 线性表之顺序表(C语言实现)

    线性表是从数据元素的逻辑结构上定义的. 这种数据元素的逻辑结构的特征如下: 1.除开第一个和最后一个元素之外.所有元素都有一个前驱元素和后继元素. 2.第一个元素无前驱元素,但有后继元素. 3.最后一 ...

  4. C语言顺序表的实现

    今天本来想写段代码练练手,想法挺好结果,栽了个大跟头,在这个错误上徘徊了4个小时才解决,现在分享出来,给大家提个醒,先贴上代码: /********************************** ...

  5. c语言进阶13-线性表之顺序表

    一. ACM算法:顺序表的查找 顺序表的查找指获取顺序表的第i个元素.对于线性表的顺序存储结构来说,如果我们要实现获取元素的操作(GetElem),即将线性表L中的第i个位置元素值返回.就程序而言,只 ...

  6. 【数据结构 Python & C++】顺序表

    用C++ 和 Python实现顺序表的简单操作 C++代码 // Date:2019.7.31 // Author:Yushow Jue #include<iostream> using ...

  7. 删除顺序表L中下标为p(0<=p<=length-1)的元素,成功返回1,不成功返回0,并将删除元素的值赋给e

    原创:转载请注明出处. [天勤2-2]删除顺序表L中下标为p(0<=p<=length-1)的元素,成功返回1,不成功返回0,并将删除元素的值赋给e 代码: //删除顺序表L中下标为p(0 ...

  8. 顺序表的C语言实现

    在现实应用中,有两种实现线性表数据元素存储功能的方法,分别是顺序存储结构和链式存储结构.顺序表操作是最简单的操作线性表的方法.下面的代码实现了顺序表的几种简单的操作.代码如下 //start from ...

  9. c数据结构 顺序表和链表 相关操作

    编译器:vs2013 内容: #include "stdafx.h"#include<stdio.h>#include<malloc.h>#include& ...

  10. 基于C++的顺序表的实现

    顺序表,是数据结构中按顺序方式存储的线性表,又称向量.具有方便检索的特点.以下,是笔者学习是基于C++实现的顺序表代码,贴上来当网页笔记用. #include <iostream> usi ...

随机推荐

  1. 用Python实现广度优先搜索

    图是一种善于处理关系型数据的数据结构,使用它可以很轻松地表示数据之间是如何关联的 图的实现形式有很多,最简单的方法之一就是用散列表 背景 图有两种经典的遍历方式:广度优先搜索和深度优先搜索.两者是相似 ...

  2. Homework5

    问:什么是分而治之? 答:分而治之就是通过一系列的方法,将复杂的问题逐渐划分成若干份相对简单的问题的方法,对每个相对简单问题进行一一解决,若干个简单问题的解的集合就是一个复杂问题的解.分而治之的应用体 ...

  3. Redis变慢?深入浅出Redis性能诊断系列文章(二)

    (本文首发于"数据库架构师"公号,订阅"数据库架构师"公号,一起学习数据库技术) 本篇为Redis性能问题诊断系列的第二篇,本文主要从应用发起的典型命令使用上进 ...

  4. JDK自带javap命令反编译class文件和Jad反编译class文件(推荐使用jad)

    一.前言 我们在日常学习中,对一个java代码有问题,不知道jvm内部怎么进行解析的时候:有个伟大壮举就是反编译,这样就可以看到jvm内部怎么进行对这个java文件解析的!我们可以使用JDK自带的ja ...

  5. 使用Gitlab CI/CD功能在本地部署 Spring Boot 项目

    前提条件: 1.Docker安装Gitlab,地址:https://www.cnblogs.com/sanduzxcvbnm/p/13814730.html 2.Docker安装Gitlab-runn ...

  6. ProxySQL 全局变量详解

    转载自:https://www.jianshu.com/p/b9d2a09d80e2 全局变量概述 ProxySQL的行为可以通过全局变量来调整.有两种配置方式: 在runtime下,使用admin结 ...

  7. .NET下数据库的负载均衡(有趣实验)(续)

    .NET下数据库的负载均衡(有趣实验)这篇文章发表后,受到了众多读者的关注与好评,其中不乏元老级程序员. 读者来信中询问最多的一个问题是:它是否能支持"异种数据库"的负载均衡?? ...

  8. 在Tomcat中启用虚拟线程特性

    前提 趁着国庆前后阅读了虚拟线程相关的源码,写了一篇<虚拟线程 - VirtualThread源码透视>,里面介绍了虚拟线程的实现原理和使用示例.需要准备做一下前期准备: 安装OpenJD ...

  9. NOIP 2013 提高组 洛谷P1967 货车运输 (Kruskal重构树)

    题目: A 国有 nn 座城市,编号从 11 到 nn,城市之间有 mm 条双向道路.每一条道路对车辆都有重量限制,简称限重. 现在有 qq 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情 ...

  10. 使用 Spring Security 手动验证用户

    1.概述 在这篇快速文章中,我们将重点介绍如何在 Spring Security 和 Spring MVC 中手动验证用户的身份. 2.Spring Security 简单地说,Spring Secu ...