顺序表的增删改查


//功能:顺序表的实现
//思路:初始化一个顺序表,如果n > INIT_LIST_SIZE 则重新分配内存空间
//反之,将元素存入顺序表
#include <stdio.h>
#include <malloc.h>

#define INIT_LIST_SIZE 2
#define ERROR 0
#define OK 1
#define LISTINCREMENT 10
#define SIZE sizeof(int)
typedef int ElemType;
typedef int Status;

//顺序表定义
typedef struct{
int length;
int listsize;
ElemType *elem;
}sqList;

//初始化顺序表
sqList initSqList()
{
sqList L;
L.elem = (ElemType *)malloc(sizeof(ElemType) * INIT_LIST_SIZE);
if(!L.elem)
{
printf("顺序表初始化失败!");
}
L.length = 0;
L.listsize = INIT_LIST_SIZE;
return L;
}
//创建顺序表
sqList createSqList(sqList L)
{
int i =0, e;
int *newBase;
printf("请输入顺序表元素(以Ctrl + Z结束):");
while(scanf("%d", &e) != EOF)
{
if(i >= L.listsize)
{
newBase = (ElemType *)realloc(L.elem, SIZE * (L.listsize + LISTINCREMENT));
L.elem = newBase;
L.listsize += LISTINCREMENT;
}
L.elem[i++] = e;
}
L.length = i;
printf("输入的顺序表元素为:");
for(i = 0; i < L.length; i++)
{
printf("%d ", L.elem[i]);
}
printf("\n");
return L;
}
//插入顺序表的值:
sqList InsertValue(sqList L, int location, ElemType e)
{
ElemType *newBase;
//当顺序表已存满时
if(L.length >= L.listsize)
{
newBase = (ElemType *)realloc(L.elem, SIZE * (L.listsize + LISTINCREMENT));
L.elem = newBase;
L.listsize += LISTINCREMENT;
}
ElemType * q = &L.elem[location - 1];
ElemType *p = &L.elem[L.length];
for(; p > q; p--)
{
*p = *(p - 1);
}
*q = e;
++L.length;
return L;
}
//按位置查找值
ElemType GetElem(sqList L, int location)
{
ElemType *e;
e = L.elem + location - 1;
return *e;
}
//定位元素,返回第一个匹配的位置
int LocateElem(sqList L, ElemType e)
{
for(int i = 0; i < L.length; i++)
{
if(L.elem[i] == e)
return i+1;
}
return 0;
}
//删除顺序表的元素,按位置删除
sqList deleteElemLocation(sqList L, int location)
{
ElemType *p = &L.elem[location - 1];
for(; p < &L.elem[L.length - 1]; p++)
{
*p = *(p + 1);
}
L.length--;
return L;
}
int main()
{
int len;
sqList L = initSqList();
L = createSqList(L);
//插入值
printf("请输入插入值的位置:");
int location;
int element;
scanf("%d", &location);
while(location < 1 || location > L.length)
{
printf("输入位置有误,请重新输入!\n");
scanf("%d", &location);
}
printf("请输入插入的元素:");
scanf("%d", &element);
L = InsertValue(L, location, element);
printf("插入元素后的顺序表为:\n");
for(int i = 0; i < L.length; i++)
{
printf("%d ", L.elem[i]);
}
printf("\n");

//查找元素
printf("请输入查找的位置:");
scanf("%d", &location);
while(location < 1 || location > L.length)
{
printf("输入位置有误,请重新输入!\n");
scanf("%d", &location);
}
printf("第%d个元素为%d\n", location, GetElem(L, location));
printf("请输入查找的元素值:");
scanf("%d", &element);
if(!LocateElem(L, element))
{
printf("不存在此元素!");
}else
{
printf("元素%d在顺序表第%d个位置!\n", element, LocateElem(L, element));
}
//删除元素
printf("请输入删除元素的位置:");
scanf("%d", &location);
while(location < 1 || location > L.length)
{
printf("输入位置有误,请重新输入!\n");
scanf("%d", &location);
}
L = deleteElemLocation(L, location);
printf("删除后的顺序表元素为:\n");
for(i = 0; i < L.length; i++)
{
printf("%d ", L.elem[i]);
}
printf("\n一共有%d个元素\n", L.length);
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值