目录
一.顺序表的概念及结构
1.概念
顺序表:是用一段连续的物理空间依次存储数据元素的线性结构,常用数组存储。
2.顺序表的分类
①.静态顺序表:用定长数组存储数据,该存储方式定开辟的空间不够灵活,容易造成浪费和空间不够的尴尬情况。
②.动态顺序表::使用动态开辟的数组存储数据,该方式使得顺序表的空间更加灵活,提高了空间利用率。
3.顺序表的结构
①.静态顺序表的结构
②.动态顺序表的结构
二.顺序表的基本操作(动态为例)
1. 顺序表的初始化
void initseq(sl* L)
{
assert(L);
L->arr = (datatype*)malloc(sizeof(datatype) * INIT);
L->capacity = INIT;
L->size = 0;
}
我们给顺序表初始化一个INIT个大小的空间
2.顺序表的尾插和扩容函数
void pushback(sl* L,datatype x)
{
assert(L);
check(L);
L->arr[L->size] = x;
(L->size)++;
}
需要注意的是,插入数据的时候,如果空间满了,需要增容。
void check(sl* L)
{
assert(L);
if (L->capacity == L->size)
{
sl* new = realloc(L->arr, sizeof(datatype) * 2 * L->capacity);
if (new)
{
L->arr = new;
L->capacity*=2;
printf("Add Finish\n");
}
else
return;
}
}
3.顺序表的打印
void print(sl* L)
{
assert(L);
int i = 0;
for (i = 0; i < L->size; i++)
{
printf("%d ", L->arr[i]);
}
printf("\n");
}
4.顺序表的尾删
void popback(sl* L)
{
assert(L);
assert(L->size>0);
if (L->size == 0)
return;
L->size--;
}
需要注意的是:如果表内没有元素,是删不了的!
5.顺序表的头插
void pushfront(sl* L,datatype x)
{
assert(L);
check(L);
int end = L->size - 1;
while (end>=0)
{
L->arr[end + 1] = L->arr[end];
end--;
}
L->arr[0] = x;
L->size++;
}
6.顺序表的头删
void popfront(sl* L)
{
assert(L->size>0);
assert(L);
int begin = 0;
while (begin<L->size)
{
L->arr[begin] = L->arr[begin+1];
begin++;
}
L->size--;
}
7.顺序表的查找
int slfind(sl* L,datatype x)
{
assert(L);
int i = 0;
for (i = 0; i < L->size; i++)
{
if (L->arr[i] == x)
return 1;
}
return -1;
}
8.顺序表的插入
void insert(sl* L, int pos, datatype x)
{
assert(L);
assert(pos >= 0&&pos<=L->size);
check(L);
int end = L->size - 1;
while (end >= pos)
{
L->arr[end+1] = L->arr[end];
end--;
}
L->arr[pos] = x;
L->size++;
}
9.顺序表的删除
void pop(sl* L, int pos)
{
assert(L);
assert(pos >= 0 && pos <= L->size);
assert(L->size > 0);
int begin = pos;
while (begin < L->size)
{
L->arr[begin] = L->arr[begin + 1];
begin++;
}
L->size--;
}
10.顺序表的销毁
void slDestroy(sl* L)
{
assert(L);
free(L->arr);
L->arr = NULL;
L->capacity = L->size = 0;
}
三.顺序表的全部代码
1.test.c
#include"seqlist.h"
int main()
{
sl L;
initseq(&L);
pushback(&L,1);
pushback(&L,2);
print(&L);
pushback(&L,3);
pushback(&L,4);
print(&L);
pushback(&L,5);
pushback(&L,6);
print(&L);
pushback(&L,7);
pushback(&L,8);
print(&L);
pushback(&L,9);
print(&L);
//popback(&L);
//print(&L);
pushfront(&L,-1);
pushfront(&L,-2);
pushfront(&L,-3);
pushfront(&L,-4);
pushfront(&L,-5);
pushfront(&L,-6);
pushfront(&L,-7);
pushfront(&L,-8);
print(&L);
popfront(&L);
print(&L);
int ret=slfind(&L,20);
insert(&L, 3, 99);
print(&L);
pop(&L, 0);
print(&L);
slDestroy(&L);
}
2.Seqlist.h
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int datatype;
#define INIT 2
typedef struct seqlist
{
datatype* arr;
int capacity;
int size;
}sl;
void initseq(sl* L);
void pushback(sl* L,datatype x);
void print(sl* L);
void popback(sl* L);
void pushfront(sl* L,datatype x);
void popfront(sl* L);
void
int slfind(sl* L,datatype x);
void insert(sl* L, int pos, datatype x);
void pop(sl* L, int pos);
void slDestroy(sl*L);
3.Seqlist.c
#include"seqlist.h"
void initseq(sl* L)
{
assert(L);
L->arr = (datatype*)malloc(sizeof(datatype) * INIT);
L->capacity = INIT;
L->size = 0;
}
void check(sl* L)
{
assert(L);
if (L->capacity == L->size)
{
sl* new = realloc(L->arr, sizeof(datatype) * 2 * L->capacity);
if (new)
{
L->arr = new;
L->capacity*=2;
printf("Add Finish\n");
}
else
return;
}
}
void pushback(sl* L,datatype x)
{
assert(L);
check(L);
L->arr[L->size] = x;
(L->size)++;
}
void print(sl* L)
{
assert(L);
int i = 0;
for (i = 0; i < L->size; i++)
{
printf("%d ", L->arr[i]);
}
printf("\n");
}
void popback(sl* L)
{
assert(L);
assert(L->size>0);
if (L->size == 0)
return;
L->size--;
}
void pushfront(sl* L,datatype x)
{
assert(L);
check(L);
int end = L->size - 1;
while (end>=0)
{
L->arr[end + 1] = L->arr[end];
end--;
}
L->arr[0] = x;
L->size++;
}
void popfront(sl* L)
{
assert(L->size>0);
assert(L);
int begin = 0;
while (begin<L->size)
{
L->arr[begin] = L->arr[begin+1];
begin++;
}
L->size--;
}
int slfind(sl* L,datatype x)
{
assert(L);
int i = 0;
for (i = 0; i < L->size; i++)
{
if (L->arr[i] == x)
return 1;
}
return -1;
}
void insert(sl* L, int pos, datatype x)
{
assert(L);
assert(pos >= 0&&pos<=L->size);
check(L);
int end = L->size - 1;
while (end >= pos)
{
L->arr[end+1] = L->arr[end];
end--;
}
L->arr[pos] = x;
L->size++;
}
void pop(sl* L, int pos)
{
assert(L);
assert(pos >= 0 && pos <= L->size);
assert(L->size > 0);
int begin = pos;
while (begin < L->size)
{
L->arr[begin] = L->arr[begin + 1];
begin++;
}
L->size--;
}
void slDestroy(sl* L)
{
assert(L)
L->size=0;
L->capacity=0;
free(L->arr);
L->arr=NULL;
}