c语言实现顺序表的基本操作
2019/7/10 23:17:18
本文主要是介绍c语言实现顺序表的基本操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
数据结构顺序表操作
复制代码 代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LIST_INIT_SIZE 100
#define LISINCREMENT 10
#define ElemType int
#define Status int
typedef struct Sq{
ElemType *elem;
int length;
int listsize;
}SqList;
Status InitList(SqList *L)
{
L->elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->elem)
return 0;
L->length=0;
L->listsize=LIST_INIT_SIZE;
return 1;
}
Status ListInsert(SqList *L,int i,ElemType e)
{
int *q,*p;
if(i<1||i>L->length)
return 0;
if(L->length>L->listsize)
{
ElemType *newbase=(ElemType*)realloc(L->elem,(LIST_INIT_SIZE+LISINCREMENT)*sizeof(ElemType));
if(!newbase)
return 0;
L->elem=newbase;
L->listsize+=(LISINCREMENT);
}
q=&(L->elem[i-1]);
for(p=&(L->elem[L->length-1]);p>=q;--p)
*(p+1)=*p;
*q=e;
++L->length;
return 1;
}
Status ListDelete(SqList *L,int i,ElemType e)
{
int *p,*q;
if(i<1||i>L->length)
return 0;
p=&(L->elem[i-1]);
e=*p;
q=L->elem+L->length-1;
for(++p;p<=q;++p)
*(p-1)=*p;
--L->length;
return 1;
}
int main(void)
{
int i,j,e,lo,temp;
SqList *L=(SqList*)malloc(sizeof(SqList));
InitList(L);
printf("请输顺序表的长度:\n");
scanf("%d",&L->length);
printf("请输入顺序表的各个元素:\n");
for(i=0;i<L->length;++i)
scanf("%d",&L->elem[i]);
printf("输入的顺序表是:\n");
for (i=0;i<L->length;++i)
{
printf("%d ",L->elem[i]);
}
printf("\n");
printf("请输入插入的位置以及节点:\n");
scanf("%d%d",&j,&e);
ListInsert(L,j,e);
printf("插入后的顺序表为:\n");
for (i=0;i<L->length;++i)
{
printf("%d ",L->elem[i]);
}
printf("\n");
printf("请输入要删除的位置:");
scanf("%d",&lo);
ListDelete(L,lo,temp);
for (i=0;i<L->length;++i)
{
printf("%d ",L->elem[i]);
}
printf("\n");
free(L);
return 0;
}
这篇关于c语言实现顺序表的基本操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-06有没有什么开源的py项目可以对图像进行分类-icode9专业技术文章分享
- 2024-07-05feign默认connecttimeout和readtimeout是多少-icode9专业技术文章分享
- 2024-07-05idea控制台,日志太多,导致部分想看得日志被刷走 搜不到-icode9专业技术文章分享
- 2024-07-05The server selected protocol version Tls10 is not accepted by client preferences [TLs12]-icode9专业技术文章分享
- 2024-07-05怎么清理项目缓存-icode9专业技术文章分享
- 2024-07-04安装 Eyoucms详细图文教程-icode9专业技术文章分享
- 2024-07-04ueditor 复制文章时,图片的链接是一个下载图片地址,该如何处理?-icode9专业技术文章分享
- 2024-07-04怎样判断host有没有对wordpress有缓存呢-icode9专业技术文章分享
- 2024-07-04具有编译功能的系统make后,无法ssh连接-icode9专业技术文章分享
- 2024-07-04make后如何升级ssh-icode9专业技术文章分享