线性表——顺序表

简介: 线性表——顺序表


目录

顺序储存定义

结构代码

顺序表初始化

检查线性表容量

线性表的建立

查找数据

数据的插入

数据的删除

修改元素

打印顺序表内容

在main函数中的使用

使用效果



顺序储存定义

用一段地址连续的存储单元依次存储线性表的数据元素。

线性表的顺序存储示意图如下:

结构代码

typedef int SQdataType;
typedef struct SeqList
{
  SQdataType* data;   //后面用动态开辟的方法对其进行开辟
  int size;           //线性表当前长度(已存数据个数)
  int capacity;       //线性表当前的内存容量(最大能存数据的个数)
}SL;

顺序表初始化

void InitList(SL* L)
{
  L->data = NULL;
  L->size = 0;
  L->capacity = 0;
}

检查线性表容量

//检查线性表容量(容量不够开辟空间,容量足够不进行操作)
void Check_capacity(SL* L)
{
  if (L->size == L->capacity)
  {
    int big = L->capacity == 0 ? 4 : L->capacity*4;
    //big为要开辟空间的大小,若容量为0,则开辟空间大小为4个字节,
    //若容量不为0,则在开辟一个当前容量大小的空间
    int* tmp = (int*)realloc(L->data,big);
    if (tmp == NULL)
    {
      printf("空间开辟失败\n");
      exit(-1);
    }
    else
    {
      printf("已成功开辟空间\n");
      L->data = tmp;
      L->capacity += (big/4);
    }
  }
}

线性表的建立

void GreatList(SL*L)
{
  printf("请输入你要导入的数据个数:");
  int n;
  scanf("%d", &n);
  L->data = (int*)malloc(n * sizeof(int));
  for (int i = 0; i < n; i++)
  {
    scanf("%d", &L->data[i]);
  }
  L->capacity = n;
  L->size = n;
}

查找数据

void Getdata(SL*L)
{
  printf("您要查找第几个数据:");
  int n;
  scanf("%d", &n);
  if (n<1 || n>L->size)
  {
    printf("要查找数据不存在!\n");
    return;
  }
  else
    printf("%d\n查找成功!\n", L->data[n - 1]);
}

数据的插入

void ListInsert(SL*L)
{
  Check_capacity(L);
  printf("请输入要插入的数据和插入的位置:");
  int x, y;
  scanf("%d%d", &x, &y);
  if (y<1||y>L->size)
  {
    printf("插入位置错误\n");
    return;
  }
  else
  {
    int end = L->size-1;
    int times = L->size - y + 1;
    while (times--)
    {
      L->data[end+1] = L->data[end];
      --end;
    }
    L->data[y - 1] = x;
    L->size += 1;
    printf("插入成功!\n");
  }
}

数据的删除

void ListDele(SL*L)
{
  printf("您要删除第几个数据:");
  int i;
  scanf("%d", &i);
  if (i<1 || i>L->size)
  {
    printf("要删除的数据不存在\n");
    return;
  }
  else
  {
    int times = L->size - i;
    while (times--)
    {
      L->data[i-1] = L->data[i];
      i++;
    }
    printf("删除成功!\n");
  }
  L->size -= 1;
}

修改元素

void ListModify(SL* L)
{
  printf("你要修改第几个数据:");
  int i;
  scanf("%d", &i);
  if (i<1 || i>L->size)
  {
    printf("要删除的数据不存在\n");
    return;
  }
  else
  {
    printf("你要将它修改为:");
    scanf("%d",& L->data[i - 1]);
    printf("修改成功!\n");
  }
}

打印顺序表内容

void OutPut(SL* L)
{
  int i = L->size;
  for (int j = 0; j < i; j++)
  {
    printf("%d  ", L->data[j]);
  }
  printf("\n");
}

在main函数中的使用

int main()
{
  SL L;
  InitList(&L);
  GreatList(&L);
  printf("1.删除  ");
  printf("2.修改  ");
  printf("3.打印  ");
  printf("4.插入  ");
  printf("5.查找\n\n\n");
  while (1)
  {
    printf("选择你要进行的操作:>");
    int n;
    scanf("%d", &n);
    switch (n)
    {
    case 1:
      ListDele(&L);
      printf("\n");
      break;
    case 2:
      ListModify(&L);
      printf("\n");
      break;
    case 3:
      OutPut(&L); 
      printf("\n");
      break;
    case 4:
      ListInsert(&L);
      printf("\n");
      break;
    case 5:
      Getdata(&L); 
      printf("\n");
      break;
    }
  }
}

使用效果


相关文章
|
4月前
|
存储 测试技术 C语言
顺序表详解(SeqList)
顺序表详解(SeqList)
95 0
|
11月前
|
算法 vr&ar
线性表的详解与深入
线性表的详解与深入
|
3月前
|
存储 算法
顺序表专题
顺序表专题
34 4
|
3月前
|
存储
25.顺序表专题
25.顺序表专题
|
4月前
|
存储
顺序表讲解
顺序表讲解
36 0
|
4月前
顺序表的实现
顺序表的实现
|
10月前
|
测试技术
顺序表(2)
顺序表(2)
543 0
|
10月前
|
存储 C语言
顺序表(1)
顺序表(1)
69 0
|
10月前
|
存储 NoSQL
03 顺序表
03 顺序表
27 0