在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. KingbaseES V8R6 维护管理案例之---Kstudio在CentOS 7启动故障

    ​ 案例说明: 在CentOS 7上安装KingbaseES V8R6C006数据库后,启动Kstudio图形界面启动失败,gtk动态库加载失败,安装gtk相关动态库后,问题解决. 适用版本: Kin ...

  2. Gitea 1.17.1 正式发布 | 08 累积更新

    Gitea 1.17.1 已正式发布.在这个小的版本更新中我们合并了 35 个 PR,没有包含功能性的更改,但我们强烈建议用户升级到此版本以获得重要的修复补丁. 致谢:感谢报告问题的安全研究人员,同时 ...

  3. ASP.NET MVC 对于视图引擎的优化

    我经常使用asp.net MVC框架来做网站.总的来说,MVC框架是一个非常优秀的框架.相对于曾经的web form模式,我个人感觉分工更加合理思路也更加清晰,但是交给开发人员的工作也相对变多了. 当 ...

  4. 华南理工大学 Python第3章课后小测-1

    1.(单选)给出如下代码 s = 'Hello scut' print(s[::-1]) 上述代码的输出结果是(本题分数:4)A) HelloB) Hello scutC) olleH tucsD)  ...

  5. 华南理工大学 Python第1章课后小测

    1.(单选)计算机有两个基本特性:功能性和()性.(本题分数:5)A) 可存储B) 可计算C) 可通信D) 可编程您的答案:D  正确率:100%2.(单选)计算机硬件可以直接识别和执行的程序设计语言 ...

  6. OKR之剑(理念篇)01—— OKR带给我们的改变

    作者:vivo互联网平台产品研发团队 一.前言 OKR即目标与关键成果法,起源于英特尔,在谷歌发扬光大.近几年在国内比较火,很多企业都相继引入了OKR的管理方式,小到2-3人的小微初创公司,大到十几万 ...

  7. LVGL 虚拟键盘使用

    一.使用例程 二.使用方式 函数的详细说明请看 lv_keyboard.h 文件 创建对象 lv_obj_t * lv_keyboard_create(lv_obj_t * parent); lv_o ...

  8. 分步骤讲解Deployment故障排除

    背景假设 当你希望在Kubernetes中部署应用程序时,你通常会定义三个组件: 一个Deployment - 这是一份用于创建你的应用程序的Pod副本的"食谱": 一个Servi ...

  9. 从nuxt开始的SEO之路

    故事从一个"美好"的早上开始...... 大清早的来到公司,打开电脑,emm, 还是熟悉的味道,鱼儿被我摸熟了的味道......就在开始准备一天的摸鱼之旅的时候,一种不详的预感涌上 ...

  10. pgsql 的问题

    pgsql 怎么插入inet类型的数据?insert into table (remote_addr) values ( ?::INET); pgsql如何截取时间的精度 select  create ...