美文网首页
顺序表-动态顺序表

顺序表-动态顺序表

作者: 又是一只小白鼠 | 来源:发表于2020-03-26 00:32 被阅读0次

顺序表是逻辑上相邻的元素物理也相邻的。

静态内存是指在程序开始运行时由编译器分配的内存,它的分配是在程序开始编译时完成的,基本类型,数组。
动态内存用户无法确定空间大小,或者空间太大,栈上无法分配时,会采用动态内存分配。

顺序表的初始化

#define MaxSize 50
typedef int ElemType;

typedef struct seqList {
    int *head; //声明了一个名为head的长度不确定的数组,也叫“动态数组”
    int length; //记录当前顺序表的长度
    int size; //记录顺序表分配的存储容量
}Seqlist, *pSeqlist;


//初始化
void InitSeqlist(pSeqlist plist) {
    assert(NULL != plist);
    plist->head = (ElemType *)malloc(sizeof(ElemType)*MaxSize);
    memset(plist->head, 0, sizeof(ElemType)*MaxSize);
    plist->length = 0;  //空表的长度初始化为0
    plist->size = MaxSize; //空表的初始化储存空间为Maxsize
}

顺序表的插入

//尾插
void InsertBack(pSeqlist plist, ElemType e) {
    assert(NULL != plist);
    CheckCapacity(plist);
    if (plist->length == plist->size) {
        printf("Seqlist is Full...\n");
        return;
    }
    plist->head[plist->length++] = e;
}


//首插
void InsertFirst(pSeqlist plist, ElemType e) {
    assert(NULL != plist);
    CheckCapacity(plist);
    if (plist->length == plist->size) {
        printf("Seqlist is Full...\n");
        return;
    }
    int i = plist->size;
    for (; i>0; i--) {
        plist->head[i] = plist->head[i-1];
    }
    plist->head[i] = e;
    plist->length ++;
}


//在指定位置i插入e
void InsertPos(pSeqlist plist, ElemType e, int pos) {
    assert(NULL != plist);
    CheckCapacity(plist);
    if (pos >= plist->size  && pos <0) {
        printf("Error...\n");
        return;
    }
    for (int i=plist->length; i>pos; i--) {
        plist->head[i] = plist->head[i-1];
    }
    plist->head[pos] = e;
    plist->length ++;
}

顺序表的删除

//尾删
void RemoveBack(pSeqlist plist) {
    assert(NULL != plist);
    if (plist->length == 0) {
        printf("Seqlist is Empty...\n");
        return;
    }
    plist->length --;
}


//首删
void RemooveFrist(pSeqlist plist) {
    assert(NULL != plist);
    if (plist->length == 0) {
        printf("Seqlist is Empty...\n");
        return;
    }
    for (int i=0; i<plist->length; i++) {
        plist->head[i] = plist->head[i+1];
    }
    plist->length --;
}

//在指定位置i删除
void RemovePos(pSeqlist plist, int pos) {
    assert(NULL != plist);
    if (plist ->length == 0) {
        printf("Seqlist is Empty...\n");
        return;
    }
    for (int i=pos; i<plist->length; i++) {
        plist->head[i] = plist->head[i+1];
    }
    plist->length --;
}

//查找元素
int FindNum(Seqlist splist, ElemType e) {
    if (splist.length == 0) {
        printf("Seqlist is Empty...\n");
        return -1;
    }
    int i=0;
    for (; i<splist.length; i++) {
        if (splist.head[i] == e) {
            return i;
        }
    }
    return -1;
}


//删除指定元素(唯一)
void RemoveNum(pSeqlist pliist, ElemType e) {
    assert(NULL != pliist);
    if (pliist->length == 0) {
        printf("Seqlist is Empty...\n");
        return;
    }
    int index = FindNum(*pliist, e);
    RemovePos(pliist, index);
}


//删除所有指定元素(有重复的值)
void RemoveNumAll(pSeqlist plist, ElemType e) {
    assert(NULL != plist);
    if (plist->length == 0) {
        printf("Seqlist is Empty...\n");
        return;
    }
    int isFind = 0;
    while ((isFind=FindNum(*plist, e)) != -1 ) {
        RemovePos(plist, isFind);
    }
}

顺序表逆置

//逆置
void ReverseSeqlist(pSeqlist plist) {
    assert(NULL != plist);
    if (plist->length == 0) {
        printf("Seqlist is Empty...\n");
        return;
    }
    int i=0;
    int j=plist->length-1;
    for (i, j; i<j; i++, j--) {
        int temp = plist->head[i];
        plist->head[i] = plist->head[j];
        plist->head[j] = temp;
    }
}

顺序表冒泡排序

//冒泡排序
void BubbleSSort(pSeqlist plist) {
    assert(NULL != plist);
    if (plist->length == 0) {
        printf("Seqlist is Empty...\n");
        return;
    }
    for (int i=0; i<plist->length; i++) {
        for (int j=i; j<plist->length; j++) {
            if (plist->head[j] < plist->head[i]) {
                int temp = plist->head[j];
                plist->head[j] = plist->head[i];
                plist->head[i] = temp;
            }
        }
    }
}

有序表拼接

//拼接,有序表1+有序表2=有序表3
void Join(pSeqlist plist1, pSeqlist plist2, pSeqlist plist) {
    assert(NULL != plist1);
    assert(NULL != plist2);
    if (plist2->length ==0 && plist1->length == 0) {
        printf("Seqlist is Empty...\n");
        return;
    }
    int i=0, j=0, k=0;
    while (i<plist1->length && j<plist2->length) {
        if (plist1->head[i] <= plist2->head[j]) {
            plist->head[k++] = plist1->head[i++];
        }
        else
            plist->head[k++] = plist2->head[j++];
    }
    //将有序表1插入到新表剩余的元素,直接尾插到新表
    if (i<plist1->length) {
        plist->head[k++] = plist1->head[i++];
    }
    //将有序表2插入到新表剩余的元素,直接尾插到新表
    if (j<plist2->length) {
        plist->head[k++] = plist2->head[j++];
    }
    plist->length = k;
}

扩容

//检查顺序表的容量, 若不够则扩容
void CheckCapacity(pSeqlist plist) {
    assert(NULL != plist);
    if(plist->size == plist->length) {
        ElemType *temp = (ElemType *)realloc(plist->head, sizeof(ElemType)*(plist->size + MaxSize));
        plist->head = temp;
        plist->size += MaxSize;
    }
}

相关文章

  • 顺序表-动态顺序表

    顺序表是逻辑上相邻的元素物理也相邻的。 静态内存是指在程序开始运行时由编译器分配的内存,它的分配是在程序开始编译时...

  • 记录十一 线性表的链式存储结构

    前言 在前面记录八 线性表的顺序存储结构和记录九 线性表的顺序存储结构扩展(动态顺序表)中我们了解到线性表的顺序存...

  • 顺序头文件

    顺序头文件 //c2-1.h线性表的动态分配顺序存储结构 顺序表基本方法 //bo2-1.cpp顺序表示的线性表的...

  • 考研数据结构笔记——2.顺序表

    顺序表 假定线性表的元素类型为ElemType,线性表的存储类型描述为 顺序表的动态分配 C++的动态分配语句为L...

  • 数据结构与算法(二,线性表的顺序存储结构,穷举法(冒泡和选择排序

    线性表 顺序表 顺序表的特性 顺序表的元素有前驱和后继 顺序表有size 顺序表的增删改查 顺序表的优缺点优点:尾...

  • 顺序表-静态顺序表

    线性表,全名为线性存储结构。将具有“一对一”关系的数据“线性”地存储到物理空间中,这种存储结构就称为线性存储结构(...

  • 线性表之顺序存储-顺序表

    顺序表的操作 [x] 向有序顺序表插入一个元素 [x] 顺序表的冒泡排序 [x] 顺序表的删除操作 [x] 顺序表...

  • 数据结构之线性表

    1、线性表-顺序表线性表-顺序表

  • 线性表-顺序表与单链表

    顺序表 线性表的顺序存储,是逻辑相邻,物理存储地址也相邻。 结构定义 顺序表的初始化 顺序表的插入 顺序表的取值 ...

  • 顺序表和链表的区别

    参考:线性表和链表的区别 注:参考文中的‘线性表’准确的说应该是’顺序表‘,链表与顺序表都是线性表。 顺序表:顺序...

网友评论

      本文标题:顺序表-动态顺序表

      本文链接:https://www.haomeiwen.com/subject/avofuhtx.html