顺序表的插入和删除
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
typedef struct Vector{
int *data;
int size,length;
}Vec;
Vec *init(int n) //函数指针
{
Vec *v=(Vec *)malloc(sizeof(Vec));
v->data=(int*)malloc(sizeof(sizeof(int)*n));
v->size=n;
v->length=0;
return v; //返回v的首地址
}
void clear(Vec* v)
{
if(v==NULL) return;
free(v->data);
free(v);
}
int insert(Vec* v, int val, int ind)
{
if(v==NULL) return 0;
if(ind<0||ind> v->length) return 0;
if(v->length==v->size) return 0;
for(int i= v->length;i>ind;i--) //将所插入位置后面所有的数向后推移
{
v->data[i]=v->data[i-1];
}
v->data[ind]=val;
v->length++;
return 1;
}
int erase(Vec *v,int ind)
{
if(v==NULL) return 0;
if(ind<0||ind>v->length) return 0;
if(v->size==v->length) return 0;
for(int i=ind+1;i<=v->length;i++) //将所删除的位置后面的数向前推移
{
v->data[i-1]=v->data[i];
}
v->length--;
return 1;
}
void output(Vec *v)
{
if(v==NULL) return;
printf("Vector : [");
for(int i = 0;i<v->length;i++)
{
i && printf(", ");
printf("%d",v->data[i]);
}
printf("]\n");
return;
}
int main()
{
srand(time(0));
#define max_op 20
Vec *v=init(max_op);
for(int i = 0;i < max_op;i++)
{
int val = rand() % 100;
int ind = rand() % (v->length+3)-1; //测试负数 大于这个范围的数
int op =rand()% 4;
switch (op)
{
case 0:
case 1:
case 3:{
printf("insert %d at %d to Vector = %d\n",val,ind,insert(v,val,ind));
}break;
default:{
printf("erase a iterm at %d from Vector = %d\n",ind ,erase(v,ind));
}break;
}
output(v);
printf("\n");
}
clear(v);
#undef max_op
return 0;
}
运行结果:
recalloc() 顺序表扩容
头文件:stdlib.h
声明:void * realloc(void * p,int n);
含义:重新分配堆上的void指针p所指的空间为n个字节,同时会复制原有内容到新分配的堆上存储空间。注意,若原来的void指针p在堆上的空间不大于n个字节,则保持不变。
返回值:同malloc() 函数
注意:如果开辟不了的情况下 会返回NULL
改善顺序表插入删除因输入的size内存不够的情况下
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
typedef struct Vector{
int *data;
int size,length;
}Vec;
Vec *init(int n)
{
Vec *v=(Vec *)malloc(sizeof(Vec));
v->data=(int*)malloc(sizeof(sizeof(int)*n));
v->size=n;
v->length=0;
return v;
}
void clear(Vec* v)
{
if(v==NULL) return;
free(v->data);
free(v);
}
int expend(Vec *v)
{
int *p;
int extr_size =v->size;
while(extr_size) //realloc 开辟不了两倍就开0.5倍 防止他被NUll
{
p=(int *)realloc(v->data,sizeof(int) * (v->size + extr_size));
if(p) break;
extr_size>>=1;
}
if(extr_size==0) return 0;
v->data=p;
v->size+=extr_size;
return 1;
}
int insert(Vec* v, int val, int ind)
{
if(v==NULL) return 0;
if(ind<0||ind> v->length) return 0;
if(v->length==v->size)
{
if(!expend(v)) return 0;
printf("success to expand! the Vector size is %d\n",v->size);
}
for(int i= v->length;i>ind;i--)
{
v->data[i]=v->data[i-1];
}
v->data[ind]=val;
v->length++;
return 1;
}
int erase(Vec *v,int ind)
{
if(v==NULL) return 0;
if(ind<0||ind>v->length) return 0;
if(v->size==v->length) return 0;
for(int i=ind+1;i<=v->length;i++)
{
v->data[i-1]=v->data[i];
}
v->length--;
return 1;
}
void output(Vec *v)
{
if(v==NULL) return;
printf("Vector : [");
for(int i = 0;i<v->length;i++)
{
i && printf(", ");
printf("%d",v->data[i]);
}
printf("]\n");
return;
}
int main()
{
srand(time(0));
#define max_op 20
Vec *v=init(2);
for(int i = 0;i < max_op;i++)
{
int val = rand() % 100;
int ind = rand() % (v->length+3)-1;
int op =rand()% 4;
switch (op)
{
case 0:
case 1:
case 3:{
printf("insert %d at %d to Vector = %d\n",val,ind,insert(v,val,ind));
}break;
default:{
printf("erase a iterm at %d from Vector = %d\n",ind ,erase(v,ind));
}break;
}
output(v);
printf("\n");
}
clear(v);
#undef max_op
return 0;
}
运行结果: