【数据结构】—— 顺序表

线性表的顺序存储

顺序表:逻辑和物理上都是连续存储的。
顺序表可以分为
(1)定长顺序表
(2)不定长顺序表
下面就两种顺序表的定义和结构进行介绍和相关操作

一、定长顺序表代码展示如下:

S e q L i s t . h \color{#0000FF}{SeqList.h} SeqList.h

#ifndef _SEQLIST_H//防止头文件重复
#define _SEQLIST_H

#define SIZE  10

typedef struct SeqList
{
    int Elem[SIZE];//存放数据元素的空间
    int length;//当前存储的数据个数
}SeqList,*PSeqList;

void Init_SeqList(PSeqList ps);//初始化顺序表
int Insert_SeqList(PSeqList ps,int pos,int val);//按位置插入元素
int Insert_SeqList_Head(PSeqList ps,int val);//头插
int Insert_SeqList_Tail(PSeqList ps,int val);//尾插
bool IsEmpty(PSeqList ps);//判空
int Delete_SeqList(PSeqList ps,int pos);//按位删除元素
int Delete_SeqList_Head(PSeqList ps);//头删
int Search(PSeqList ps,int key);//返回下标
void Show(PSeqList ps);//显示打印出顺序表
void Destroy(PSeqList ps);//销毁顺序表
void Clear(PSeqList ps);//清空数据
#endif

S e q L i s t . c p p \color{#0000FF}{SeqList.cpp} SeqList.cpp

#include  "SeqList.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

void Init_SeqList(PSeqList ps)
{
    assert(ps!=NULL);
    if(ps==NULL)
    {
        return ;
    }
    ps->length = 0;
    //初始化长度为0
}
static bool IsFull(PSeqList ps)
{
    return ps->length == SIZE;
}
int Insert_SeqList(PSeqList ps,int pos,int val)
{
    if(ps == NULL||pos<0||pos>ps->length)
    {
        return 0;
    }
    for(int i = ps->length-1;i>=pos;i--)
    {
        ps->Elem[i+1] = ps->Elem[i];
    }
    ps->Elem[pos] = val;
    ps->length++;
    //按位置插入后顺序表向后移动一位,长度加一
    return 1;
}
int Insert_SeqList_Head(PSeqList ps,int val)
{
    return Insert_SeqList(ps,0,val);
}
int Insert_SeqList_Tail(PSeqList ps,int val)
{
    if(ps == NULL)
        return 0;
    return Insert_SeqList(ps,ps->length,val) ;
}
bool IsEmpty(PSeqList ps)
{
    return ps->length ==0;
}
int Delete_SeqList(PSeqList ps,int pos)
{
    if (pos<0||pos>=ps->length)
    {
        return 0;
    }
    for(int i =pos;i<ps->length-1;i++)
    {
        ps->Elem[i]=ps->Elem[i+1];
    }
    ps->length--;//按位置删除,长度减一
    return 1;
}
int Delete_SeqList_Head(PSeqList ps)
{
    return Delete_SeqList(ps,0);
}
int Search(PSeqList ps,int key)
{
    for(int i = 0;i<ps->length;i++)
    {
        if(ps->Elem[i]==key)
        {
            return i;
        }
    }
    return -1;
    //查找元素,并返回下标
}
void Show(PSeqList ps)
{
    for(int i =0;i<ps->length;i++)
    {
        printf("%d   ",ps->Elem[i]);
    }
    printf("\n");
}
//销毁,回收内存
void Destroy(PSeqList ps)
{
    Clear(ps);
}
//清空数据
void Clear(PSeqList ps)
{
    ps->length = 0;
}

m a i n . c p p \color{#0000FF}{main.cpp} main.cpp

#include  "SeqList.h"
#include <stdio.h>
int main()
{
    SeqList s;
    Init_SeqList(&s);
    for(int i = 0;i<10;i++)
    {
        Insert_SeqList(&s,i,i+1);
    }
    Delete_SeqList(&s,0);//删除0号下标的元素
    Show(&s);
    printf("%d\n",Search(&s,8));
    return 0;
}

通过主函数相关功能的调用,最终运行结果为
在这里插入图片描述
二、不定长顺序表
D S e q L i s t . h \color{#0000FF}{DSeqList.h} DSeqList.h

#ifndef _DSEQLIST_H
#define _DSEQLIST_H

#define INIT_SIZE  10//初始化单元数量
/*不定长顺序表和定长顺序表的区别是,定长规定长度;
而不定长顺序表是长度满的时候可以对线性表进行扩容*/
typedef struct DSeqList
{
    int *elem;//指向存储数据的内存
    int length;//有效数据个数
    int listsize;//总格子数
    //12个字节
}DSeqList,*PDSeqList;

void Init_DSeqList(PDSeqList ps);
bool Insert_DSeqList(PDSeqList ps,int pos,int val);
bool IsEmpty(PDSeqList ps);
bool Delete_DSeqList(PDSeqList ps,int val);
int Search(PDSeqList ps,int key);//返回下标
void Show(PDSeqList ps);
void Destroy(PDSeqList ps);
void Clear(PDSeqList ps);
#endif

D S e q L i s t . c p p \color{#0000FF}{DSeqList.cpp} DSeqList.cpp

#include  "DSeqlist.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void Init_DSeqList(PDSeqList ps)
{
    assert(ps!=NULL);
    if(ps==NULL)
    {
        return ;
    }
    ps->elem = (int *)malloc(INIT_SIZE*sizeof(int));
    ps->length = 0;
    ps->listsize = INIT_SIZE;
}
static bool IsFull(PDSeqList ps)
{
    return ps->length == ps->listsize;
}
static void Inc(PDSeqList ps)//扩容
{
    ps->elem = (int *) realloc(ps->elem,ps->listsize*2*sizeof(int));
    //realloc两个参数,原来的大小和新申请的大小
    //ps->length;不需要处理
    ps->listsize *=2;//2只是经验值
}
bool Insert_DSeqList(PDSeqList ps,int pos,int val)
{
    if(pos<0||pos>ps->length)
    {
        return false;
    }
    if(IsFull(ps))
    {
        Inc(ps);
    }//容量一定足够
    for(int i = ps->length-1;i>=pos;i--)
    {
        ps->elem[i+1] = ps->elem[i];

    }
    ps->elem[pos] = val;
    ps->length++;
    return true;
}
bool IsEmpty(PDSeqList ps)
{
    return ps->length ==0;
}
bool Delete_DSeqList(PDSeqList ps,int pos)
{
    if (pos<0||pos>=ps->length)
    {
        return false;
    }
    for(int i =pos;i<ps->length-1;i++)
    {
        ps->elem[i]=ps->elem[i+1];
    }
    ps->length--;
    return true;
}
int Search(PDSeqList ps,int key)
{
    for(int i = 0;i<ps->length;i++)
    {
        if(ps->elem[i]==key)
        {
            return i;
        }
    }
    return -1;
}
void Show(PDSeqList ps)
{
    for(int i =0;i<ps->length;i++)
    {
        printf("%d   ",ps->elem[i]);
    }
    printf("\n");
}
//销毁,回收内存
void Destroy(PDSeqList ps)
{
    assert(ps!=NULL);
    free(ps->elem);
    ps->elem = NULL;
    ps->length = 0;
    ps->listsize = 0;
}
//清空数据
void Clear(PDSeqList ps)
{
    ps->length = 0;
}

m a i n . c p p \color{#0000FF}{main.cpp} main.cpp

#include  "DSeqlist.h"
#include <stdio.h>
int main()
{
    DSeqList s;
    Init_DSeqList(&s);
    for(int i = 0;i<20;i++)
    {
        Insert_DSeqList(&s,i,i);
    }
    Delete_DSeqList(&s,0);
    return 0;
}

运行结果如图:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值