/*线性表的顺序存储:顺序表*/
#include <stdio.h>
#include <stdlib.h> //malloc
#if 0
void *malloc(size_t size);
功能:在堆区开辟空间
参数:size开辟空间的大小
返回:返回堆区的首地址,需要强制类型转换
#endif
#define N 10
//存储的数据类型
typedef int data_t;
//顺序表的类型
typedef struct {
data_t a[N]; //实现顺序存储
int last; //记录最后一个元素的下标
}sqlist_t; //类型名
//开辟空的顺序表
sqlist_t *sqlist_create()
{
sqlist_t *p = NULL; //防止野指针的形成
p = (sqlist_t *)malloc(sizeof(sqlist_t)); //开辟顺序表的空间
if(p == NULL)
return NULL;
p->last = -1; //下标开始为-1
return p; //将顺序表的首地址返回
}
//判断是否为满
//为满返回1,不满返回0
int sqlist_is_full(sqlist_t *p)
{
if(p != NULL)
return p->last == N-1 ? 1 : 0;
#if 0
if(p->last == N-1)
{
return 1;
}
else
{
return 0;
}
#endif
}
//判断是否为空
//为空返回1,非空返回0
int sqlist_is_empty(sqlist_t *p)
{
if(p != NULL)
return p->last == -1 ? 1 : 0;
}
//增:从后面插入数据
int sqlist_insert(sqlist_t *p, data_t value)
{
if(p == NULL)
return -1;
if(sqlist_is_full(p))
{
printf("full\n");
return -1; //异常结束
}
p->last++;
p->a[p->last] = value;
return 0; //正常结束
}
//删:从后面删除数据
int sqlist_delete(sqlist_t *p)
{
if(p == NULL)
return -1;
if(sqlist_is_empty(p))
{
printf("empty\n");
return -1;
}
p->last--;
return 0;
}
//改:将旧数据改成新数据
int sqlist_change(sqlist_t *p, data_t old, data_t new)
{
int i;
if(p == NULL)
return -1;
for(i=0; i<=p->last; i++) //循环整个顺序表
{
if(p->a[i] == old) //查找旧的数据
{
p->a[i] = new; //覆盖为新的数据
}
}
return 0;
}
//增:按位置插入
int sqlist_insert_pos(sqlist_t *p, int pos, data_t value)
{
if(p == NULL)
return -1;
if(sqlist_is_full(p))
{
printf("full\n");
return -1;
}
//判断pos的有效取值区间
if(pos < 0 || p->last + 1 < pos)
{
printf("pos error\n");
return -1;
}
int i;
for(i=p->last; i>= pos; i--) //移动的数据范围last ~ pos
{
p->a[i+1] = p->a[i]; //将数据向后移动
}
p->a[pos] = value; //将数据存储到pos位置
p->last++; //last始终代表最后一个元素的位置
return 0;
}
//删:按位置删除
int sqlist_delete_pos(sqlist_t *p, int pos)
{
if(p == NULL)
return -1;
if(sqlist_is_empty(p))
{
printf("empty\n");
return -1;
}
if(pos < 0 || p->last < pos) //判断pos的有效取值区间
{
printf("pos error\n");
return -1;
}
int i;
for(i=pos+1; i<=p->last; i++) //移动的数据范围pos+1 ~ last
{
p->a[i-1] = p->a[i]; //将数据向前移动
}
p->last--;
return 0;
}
//删除重复
int sqlist_delete_repeat(sqlist_t *p)
{
int i,j;
if(p == NULL)
return -1;
for(i=0; i<p->last; i++) //基准值的取值范围
{
for(j=i+1; j<=p->last; j++) //与基准值比较的数据范围
{
if(p->a[i] == p->a[j]) //判断是否重复
{
sqlist_delete_pos(p,j); //按位置删除后面的数据
j--; //重复判断删除后的新数据
}
}
}
return 0;
}
//遍历顺序表
int sqlist_show(sqlist_t *p)
{
int i;
if(p == NULL)
return -1;
for(i=0; i<=p->last; i++)
{
printf("%d ",p->a[i]); //打印a[0] ~ a[last]
}
putchar('\n'); //输出换行符
}
int main(int argc, const char *argv[])
{
sqlist_t *l = sqlist_create();
if(l == NULL)
return -1;
sqlist_insert(l,10);
sqlist_insert(l,20);
sqlist_insert(l,30);
sqlist_insert(l,40);
sqlist_insert(l,50);
printf("insert data:");
sqlist_show(l);
sqlist_delete(l);
printf("delete data:");
sqlist_show(l);
sqlist_change(l,20,12);
printf("change data:");
sqlist_show(l);
sqlist_insert_pos(l, 3, 15);
printf("insert pos:");
sqlist_show(l);
sqlist_delete_pos(l, 3);
printf("delete pos:");
sqlist_show(l);
free(l);
putchar('\n');
sqlist_t *w = sqlist_create();
sqlist_insert(w,11);
sqlist_insert(w,12);
sqlist_insert(w,13);
sqlist_insert(w,11);
sqlist_insert(w,11);
sqlist_insert(w,15);
sqlist_insert(w,11);
sqlist_insert(w,16);
sqlist_insert(w,17);
printf("insert data:");
sqlist_show(w);
sqlist_delete_repeat(w);
printf("delete repeat:");
sqlist_show(w);
free(w);
return 0;
}
顺序链表的操作
最新推荐文章于 2023-05-22 18:06:41 发布