SQLIST

静态

#include <stdio.h>
#include <malloc.h>

#define N 10

void staticList(){
    int iList[N]={9,8,7,6,5,4,3,2,1,0};
    printf("%d,%d",iList[0],iList[1]);
    
}

void dynamicList(){
    int * pList = (int *)malloc(sizeof(int)*N);
    pList = (int *)malloc(sizeof(int)*2*N);
    printf("%d\n",pList[2]);
    pList[2]=3;
    printf("%d\n",pList[2]);
    int * pNew = (int *)malloc(sizeof(int)*2*N);
    free(pList);
    pList = pNew;
    pList[N+1]= 6;
}


typedef struct Sqlist{
    int iList[N];
    int length;
}Sqlist;

int main(){

    staticList();
    dynamicList();

    return 0;
}

动态存储

#include <stdio.h>
#include <malloc.h>

#define N 10


void dynamicList(){
    int * pList = (int *)malloc(sizeof(int)*N);
    pList = (int *)malloc(sizeof(int)*2*N);
    printf("%d\n",pList[2]);
    pList[2]=3;
    printf("%d\n",pList[2]);
    int * pNew = (int *)malloc(sizeof(int)*2*N);
    free(pList);
    pList = pNew;
    pList[N+1]= 6;
}


typedef struct Sqlist{
    int* pList;
    int length;
    int listsize;
}Sqlist;

int main(){

    dynamicList();
    Sqlist L; 
    for(int i=0;i<L.length-1;i++)
        printf("%d,%d",L.pList[i],L.pList[i+1]);

    return 0;
}

循环的索引问题

void indexTrouble(){
    Sqlist L; 
    for(int i=0;i<L.length;i++){//依次访问每个元素
        printf("%d",L.pList[i]);
    }
    for(int i=0;i<L.length-1;i++){//访问每一组前驱与后继
        printf("%d,%d",L.pList[i],L.pList[i+1]);
    }
}

顺序表

1.顺序表创建

#include <stdio.h>
#include <malloc.h>

#define N 10
typedef int ElemType;

typedef struct Sqlist{
    int* pList;
    int length;
    int listsize;
}Sqlist;

void initial(Sqlist &L){
    L.pList = (ElemType*) malloc(sizeof(ElemType)*N);
    L.listsize = N;
    printf("length:%d\n",L.length);
    L.length = 0;
    printf("length:%d\n",L.length);    
    

}
int main(){
    Sqlist L;
    initial(L);

    return 0;
}

2.第i个位置插入元素

#include <stdio.h>
#include <malloc.h>

#define N 10
typedef int ElemType;

typedef struct Sqlist{
    int* pList;
    int length;
    int listsize;
}Sqlist;

void initial(Sqlist &L){
    L.pList = (ElemType*) malloc(sizeof(ElemType)*N);
    L.listsize = N;
    printf("length:%d\n",L.length);
    L.length = 0;
    printf("length:%d\n",L.length);    
}

int insert(ElemType e,int i,Sqlist &L){
    //step1 判断是否合法
    if(i<1||i>L.length)
        return 0;
    //step2 判断插入空间是否充足,不充足申请新的空间,并将老表元素复制过去
    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;
    }
    //step3 插入
    for(int index=L.length-1;index>=i;index--)
        L.pList[index+1]=L.pList[index];
    L.pList[i-1]=e;
    L.length+=1;
    return 1;
}
void print(Sqlist L){
    for(int i = 0;i<L.length;i++){
        printf("%d ",L.pList[i]);
    }
}
void value(Sqlist &L){
    L.length = N;
    for(int i =0 ;i<L.length;i++){
        if(i==3){
            L.pList[i]=L.length-3;
        }
        else if(i==6){
            L.pList[i]=L.length-2;
        }
        else{
            L.pList[i]=L.length-1;
        }
    }
    print(L);

}

int main(){
    Sqlist L;
    initial(L);
    value(L);
    int ret =insert(100,1,L);
    if(ret == 1)
        print(L);

    return 0;
}

3.删除第i个元素

#include <stdio.h>
#include <malloc.h>

#define N 10
typedef int ElemType;

typedef struct Sqlist{
    int* pList;
    int length;
    int listsize;
}Sqlist;

void initial(Sqlist &L){
    L.pList = (ElemType*) malloc(sizeof(ElemType)*N);
    L.listsize = N;
    printf("length:%d\n",L.length);
    L.length = 0;
    printf("length:%d\n",L.length);    
}

int insert(ElemType e,int i,Sqlist &L){
    //step1 判断是否合法
    if(i<1||i>L.length)
        return 0;
    //step2 判断插入空间是否充足,不充足申请新的空间,并将老表元素复制过去
    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;
    }
    //step3 插入
    for(int index=L.length-1;index>=i;index--)
        L.pList[index+1]=L.pList[index];
    L.pList[i-1]=e;
    L.length+=1;
    return 1;
}
int deleteList(int i,Sqlist &L){
    if(i<1||i>L.length-1){
        return 0;
    }
    for(int index =i;index<L.length;index++){
        L.pList[index-1]=L.pList[index];
    }
    L.length-=1;
    return 1;

}
void print(Sqlist L){
    for(int i = 0;i<L.length;i++){
        printf("%d ",L.pList[i]);
    }
}
void value(Sqlist &L){
    L.length = N;
    for(int i =0 ;i<L.length;i++){
        if(i==3){
            L.pList[i]=L.length-3;
        }
        else if(i==6){
            L.pList[i]=L.length-2;
        }
        else{
            L.pList[i]=L.length-1;
        }
    }
    print(L);

}

int main(){
    Sqlist L;
    initial(L);
    value(L);
    int ret =insert(100,1,L);
    if(ret == 1)
        print(L);
    int ret1=deleteList(1,L);
    if(ret1==1)
        print(L);
    return 0;
}

删除最小元素,空位由最后一个元素填补

​​
最小值的查找问题:
1、数据是给定的,查找范围是有限的,初始化任意元素
2、数据没有给定,未知且随机,查找范围是无限的,初始化为一个不可能出现的无穷大值INFminValue = 100;
{101,102,…}
100压根就不存在于数据集合中,但是你却初始化了最小值为100,以为100为最小值

void deleteMin(Sqlist &L){
    int minIndex=0;
    for(int i=0;i<L.length;i++){
        if(L.pList[minIndex]>L.pList[i])
            minIndex = i;
    }
    L.pList[minIndex]=L.pList[L.length-1];
    --L.length;
}

当前访问子序列长度>=当前结果子序列的长度分析:
1、访问一个,然后通过后面元素往前移动来执行删除,这样时间复杂度太高。
2、开创一个临时空间,将满足要求的元素放入,不满足要求的过滤,这样空间复杂度太高。
3、返现在执行第2种方法时,过滤后的元素集合是读取的元素集合的子集,即过滤后的元素集合不会占用未访问元素的集合,不会发生数据覆盖,从而可以在原有空间上进行过滤操作。
我们称这种在原存储空间上依次访问元素并对访问元素进行过滤来达到对特定一些(–个以上)元素删除的方法成为过滤法。

【过滤算法】

在无序表中删除值在s和t之间的所有元素

#include <stdio.h>
#include <malloc.h>

#define N 10
typedef int ElemType;

typedef struct Sqlist{
    int* pList;
    int length;
    int listsize;
}Sqlist;

void initial(Sqlist &L){
    L.pList = (ElemType*) malloc(sizeof(ElemType)*N);
    L.listsize = N;
    L.length = 0;
 
}

void deleteElem(int s,int t,Sqlist &L){
    int curLength = 0;//当前结果子序列长度
    for(int i=0,j=0;i<L.length;i++){
        if(L.pList[i]<s||L.pList[i]>t){
            L.pList[curLength]=L.pList[i];
            curLength++;
        }
    }
    L.length=curLength;
}

void print(Sqlist L){
    for(int i = 0;i<L.length;i++){
        printf("%d ",L.pList[i]);
    }
}
void value(Sqlist &L){
    L.length = N;
    for(int i =0 ;i<L.length;i++){
        if(i==3){
            L.pList[i]=L.length-3;
        }
        else if(i==6){
            L.pList[i]=L.length-2;
        }
        else{
            L.pList[i]=L.length-i;
        }
    }
    print(L);

}

int main(){
    Sqlist L;
    initial(L);
    value(L);
    deleteElem(3,5,L);
    print(L);

    return 0;
}

【偏移法】

非递减顺序表中删除值在s和t之间的元素

#include <stdio.h>
#include <malloc.h>

#define N 10
typedef int ElemType;

typedef struct Sqlist{
    int* pList;
    int length;
    int listsize;
}Sqlist;

void initial(Sqlist &L){
    L.pList = (ElemType*) malloc(sizeof(ElemType)*N);
    L.listsize = N;
    L.length = 0;
 
}

int deleteElem(int s,int t,Sqlist &L){
    int sIndex = -1;
    for(int i = 0;i<L.length;i++){
        if(L.pList[i]>=s&&L.pList[i]<=t){
            sIndex = i;
            break;
        }
    }
    if(sIndex == -1){
        return 0;
    }

    int tIndex = -1;
    for(int i = L.length-1;i>=0;i--){
        if(L.pList[i]>=s&&L.pList[i]<=t){
            tIndex = i;
            break;
        }
    }
    //偏移量t-s+1
    int delta = tIndex - sIndex + 1;    
    for(int i =tIndex+1;i<L.length;i++){
        L.pList[i-delta]=L.pList[i];
    }
    L.length-=delta;

    return 1;
}

void print(Sqlist L){
    for(int i = 0;i<L.length;i++){
        printf("%d ",L.pList[i]);
    }
}
void value(Sqlist &L){
    L.length = N;
    for(int i= 0;i<L.length;i++){
        L.pList[i]=i+1;
    }
    print(L);

}

int main(){
    Sqlist L;
    initial(L);
    value(L);
    int ret = deleteElem(4,7,L);
    if(ret = 1)
        print(L);
    else 
        printf("no");

    return 0;
}

min

#include <stdio.h>
#include <malloc.h>

#define N 10
typedef int ElemType;

typedef struct Sqlist{
    int* pList;
    int length;
    int listsize;
}Sqlist;

void initial(Sqlist &L){
    L.pList = (ElemType*) malloc(sizeof(ElemType)*N);
    L.listsize = N;
    L.length = 0;
 
}

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; int j = Lb.length -1;
    while(i<La.length&&j>=0){
        if(La.pList[i]<Lb.pList[j]){
            Lc.pList[Lc.length]=La.pList[i];
            Lc.length++;
            i++;
        }
        else{
            Lc.pList[Lc.length]=Lb.pList[j];
            Lc.length++;
            j--;
        }
    }
    while(i<La.length){
        Lc.pList[Lc.length]=La.pList[i];
        Lc.length++;
        i++;
    }
    while(j>=0){
        Lc.pList[Lc.length]=Lb.pList[i];
        Lc.length++;
        j--;
    }
    free(La.pList);    
    free(Lb.pList);    
    La.length=Lb.length =0;
}   

void print(Sqlist L){
    for(int i = 0;i<L.length;i++){
        printf("%d ",L.pList[i]);
    }
    printf("\n");
}
void value(Sqlist &L){
    L.length = N;
    for(int i= 0;i<L.length;i++){
        L.pList[i]=2*i;
    }
    print(L);

}
void value1(Sqlist &L){
    L.length = 5;
    for(int i= 0;i<L.length;i++){
        L.pList[i]=10-i;
    }
    print(L);

}
int main(){
    Sqlist La,Lb,Lc;
    initial(La);
    initial(Lb);
    value(La);
    value1(Lb);
    combine(La,Lb,Lc);

    print(Lc);



    return 0;
}

max

#include <stdio.h>
#include <malloc.h>

#define N 10
typedef int ElemType;

typedef struct Sqlist{
    int* pList;
    int length;
    int listsize;
}Sqlist;

void initial(Sqlist &L){
    L.pList = (ElemType*) malloc(sizeof(ElemType)*N);
    L.listsize = 15;
    L.length = 0;
 
}

void combine(Sqlist &La,Sqlist &Lb){
    int i = La.length-1; 
    int j = 0;
    int curlength = 0;
    while(j<Lb.length&&i>=0){//每次取max
        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(i>=0){//A表剩余不用动
    //     La.pList[La.listsize-curlength-1]=La.pList[i];
    //     Lc.length++;
    //     i--;
    // }    
    while(j<Lb.length){
        La.pList[La.listsize-curlength-1]=Lb.pList[j];
        curlength++;
        j++;
    }
    La.length+=Lb.length;
    Lb.length= 0;

}   

void print(Sqlist L){
    for(int i = 0;i<L.length;i++){
        printf("%d ",L.pList[i]);
    }
    printf("\n");
}
void value(Sqlist &L){
    L.length = N;
    for(int i= 0;i<L.length;i++){
        L.pList[i]=2*i;
    }
    print(L);

}
void value1(Sqlist &L){
    L.length = 5;
    for(int i= 0;i<L.length;i++){
        L.pList[i]=10-i;
    }
    print(L);

}
int main(){
    Sqlist La,Lb;
    initial(La);
    initial(Lb);
    value(La);
    value1(Lb);
    combine(La,Lb);
    print(La);



    return 0;
}

insertsort

#include <iostream>


// 假设 ElemType 是 int 类型
typedef int ElemType;

// 定义 Sqlist 结构
struct Sqlist {
    ElemType *pList;
    int length;
};

// 插入排序算法
void insertsort(int r, Sqlist &L) {
    int insertIndex = r; // 待排序
    while (insertIndex < L.length) {
        int i = insertIndex - 1; // 排好序的最后一个元素
        ElemType e = L.pList[insertIndex];
        while (i >= 0) {
            if (L.pList[i] <= e) { // 找到插入位置
                break;
            }
            L.pList[i + 1] = L.pList[i];
            i--;
        }
        L.pList[i + 1] = e;
        insertIndex += 1;
    }
}

int main() {
    // 创建一个包含递增和递减元素的 Sqlist 结构
    ElemType arr[] = {1, 2, 3, 7, 6, 5, 4};
    int r = 3; // 前3个元素递增有序
    int n = 7; // 总共有7个元素

    Sqlist L;
    L.pList = arr;
    L.length = n;

    // 输出排序前的数组
    std::cout << "排序前的数组:";
    for (int i = 0; i < n; i++) {
        std::cout << L.pList[i] << " ";
    }
    std::cout << std::endl;

    // 调用插入排序算法
    insertsort(r, L);

    // 输出排序后的数组
    std::cout << "排序后的数组:";
    for (int i = 0; i < n; i++) {
        std::cout << L.pList[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

交集算法

#include <iostream>

// 假设 Sqlist 是如下定义的结构
struct Sqlist {
    int *pList; // 顺序表的指针
    int length; // 顺序表的长度
};

// 交集函数的定义
void intersection(Sqlist& La, Sqlist Lb) {
    int curlength = 0;
    int i = 0;
    int j = 0;
    while (i < La.length && j < Lb.length) {
        if (La.pList[i] < Lb.pList[j]) {
            i++;
        } else if (Lb.pList[j] < La.pList[i]) {
            j++;
        } else {
            if (curlength == 0 || La.pList[curlength - 1] != La.pList[i]) {
                La.pList[curlength++] = La.pList[i];
            }
            i++;
            j++;
        }
    }
    La.length = curlength;
}

int main() {
    // 创建两个非递减顺序表
    Sqlist La;
    Sqlist Lb;

    // 假设你已经为 La 和 Lb 分配了内存,并将数据存入顺序表中
    // 以下是示例数据,你可以根据需要修改
    int dataLa[] = {1, 3, 5, 7, 9};
    int dataLb[] = {3, 5, 7, 8, 10};
    
    La.pList = dataLa;
    La.length = sizeof(dataLa) / sizeof(int);
    
    Lb.pList = dataLb;
    Lb.length = sizeof(dataLb) / sizeof(int);

    // 输出原始数据
    std::cout << "顺序表 La: ";
    for (int i = 0; i < La.length; i++) {
        std::cout << La.pList[i] << " ";
    }
    std::cout << std::endl;

    std::cout << "顺序表 Lb: ";
    for (int i = 0; i < Lb.length; i++) {
        std::cout << Lb.pList[i] << " ";
    }
    std::cout << std::endl;

    // 调用交集函数
    intersection(La, Lb);

    // 输出交集结果
    std::cout << "交集为: ";
    for (int i = 0; i < La.length; i++) {
        std::cout << La.pList[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

差集算法

#include <iostream>


struct Sqlist {
    int *pList; // 顺序表的指针
    int length; // 顺序表的长度
};


void exception(Sqlist La, Sqlist Lb, Sqlist& Lc) {
    int i = 0;
    int j = 0;
    while (i < La.length && j < Lb.length) {
        if (La.pList[i] < Lb.pList[j]) {
            Lc.pList[Lc.length] = La.pList[i];
            Lc.length++;
            i++;
        } else if (Lb.pList[j] < La.pList[i]) {
            Lc.pList[Lc.length] = Lb.pList[j];
            Lc.length++;
            j++;
        } else {
            i++;
            j++;
        }
    }
    while (i < La.length) {
        Lc.pList[Lc.length] = La.pList[i];
        Lc.length++;
        i++;
    }
    while (j < Lb.length) {
        Lc.pList[Lc.length] = Lb.pList[j];
        Lc.length++;
        j++;
    }
}

int main() {
    // 创建三个升序顺序表表示集合 La、Lb 和 Lc
    Sqlist La;
    Sqlist Lb;
    Sqlist Lc;

    // 填充集合 La 和 Lb 的数据,假设已经按升序排列
    int dataLa[] = {1, 3, 7, 8};
    int dataLb[] = {3, 6, 8,10};

    La.pList = dataLa;
    La.length = sizeof(dataLa) / sizeof(int);

    Lb.pList = dataLb;
    Lb.length = sizeof(dataLb) / sizeof(int);

    // 为集合 Lc 分配足够的内存空间
    Lc.pList = new int[La.length];
    Lc.length = 0;

    // 调用求差集函数
    exception(La, Lb, Lc);

    // 输出差集结果
    std::cout << "差集为: ";
    for (int i = 0; i < Lc.length; i++) {
        std::cout << Lc.pList[i] << " ";
    }
    std::cout << std::endl;

    // 释放内存
    delete[] Lc.pList;

    return 0;
}

循环左移

逆置顺序表实现

#include <iostream>


struct Sqlist {
    int *pList; // 顺序表的指针
    int length; // 顺序表的长度
};

// 逆置顺序表的函数
void reverse(Sqlist &L, int low, int high) {
    while (low < high) {
        int e = L.pList[low];
        L.pList[low] = L.pList[high];
        L.pList[high] = e;
        low++;
        high--;
    }
}

// 循环左移顺序表的函数
void leftShift(Sqlist &L, int r) {
    int n = L.length;
    if (n == 0 || r <= 0 || r >= n) {
        return; // 无需移动
    }
    
    reverse(L, 0, n - 1);        // 全部逆置
    reverse(L, 0, n - r - 1);   // 逆置前半部分
    reverse(L, n - r, n - 1);   // 逆置后半部分
}

int main() {
    Sqlist L;
    
    // 填充顺序表的数据,假设已经分配内存和初始化
    int data[] = {1, 2, 3, 4, 5};
    L.pList = data;
    L.length = sizeof(data) / sizeof(int);
    
    int r = 2; // 左移的位数
    
    // 调用循环左移函数,使用引用传递
    leftShift(L, r);
    
    // 输出左移后的顺序表
    std::cout << "左移后的顺序表: ";
    for (int i = 0; i < L.length; i++) {
        std::cout << L.pList[i] << " ";
    }
    std::cout << std::endl;
    
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值