c语言顺序表的基本操作

众所周知,顺序表是数据结构中线性表的知识,线性表有两种存储结构,一种是顺序存储(顺序表),一种是链式存储(链表)。现在给出顺序表中常用基本操作的c语言代码实现。

#include <stdio.h>
#include <stdlib.h>
#define Maxsize 20
typedef int ElemType;
typedef struct
{
	ElemType *data; //定义数组 
	int lenth;        //线性表当前长度 
}SqList;
void InitList(SqList *L)  //线性表的初始化 
{
	L->data=(ElemType *)malloc(Maxsize*sizeof(ElemType));  //为数组分配空间 
	L->lenth=0;	 //初始长度为0 
}
void DestoryList(SqList *L)  //线性表的销毁 
{
	if(L->data!=NULL) 
	free(L->data);
	L->data=NULL;
	L->lenth=0;
 } 
int GetElem(SqList L,int i,ElemType *e)  //获取表L中第i个元素  这里的*e代表指针,传递的时候用地址传递 
{
	if(i<0||L.lenth==0||i>L.lenth)  //如果超出范围则返回0,表示未找到 
	  return 0;
	else
	{*e=L.data[i-1];    //把表中第i个元素赋值给e   这里的*e代表引用 
	  return 1;
    }
}
int ListInsert(SqList *L,int i,ElemType e)  //在表L中第i个元素之前插入e 
{
	int k;
	if(L->lenth==Maxsize) //如果表满,返回0 
	return 0;
	if(i<1||i>L->lenth+1)  //如果要插入的i不在范围时,返回0.注:i>lenth+1是因为可以在表的最后插入 
	return 0;	
	if(i<=L->lenth)
	{
		for(k=L->lenth-1;k>=i-1;k--)  //因为数组下标从0开始,所以都要-1 
		L->data[k+1]=L->data[k];       //将插入的第i位之后的所有元素全部后移一位 
	 } 
	 L->data[i-1]=e;  //将e的值插入
	  L->lenth++;  //表长度+1 
	  return 1; 
 }
 int ListDelete(SqList *L,int i,ElemType *e) //删除表中第i个元素,并用e返回其值
 {	
 	int k;
    if(L->lenth==0)  //如果是空表,返回0 
    return 0;
 	if(i<1||i>L->lenth)  //如果要删除的i不在范围时,返回0. 
	return 0;
	*e=L->data[i-1];   //先把要删除的元素赋值给e 
	if(i<L->lenth)
	{
		for(k=i-1;k<L->lenth-1;k++) 
		L->data[k]=L->data[k+1]; //将删除元素之后的所有元素前移一位 
	 } 
	 L->lenth--;  //表长度-1 
	 return 1;
  } 
void print(SqList L)
 {
 	int i;
 	for(i=0;i<L.lenth;i++)
 	printf("%d ",L.data[i]);
  } 
void menu()
{
	printf("\n-------------------------------------1.向表中插入元素----------------------------------------------------------");
	printf("\n-------------------------------------2.向表中删除元素----------------------------------------------------------");
	printf("\n-------------------------------------3.获取表中元素------------------------------------------------------------");
	printf("\n-------------------------------------4.打印表中元素-------------------------------------------------------------");
   	printf("\n-------------------------------------5.销毁线性表-------------------------------------------------------------");
 } 
 int main()
 {  int choose,location,m,i;
 	SqList L;
	ElemType e;
 	InitList(&L);
 	printf("请输入线性表的长度n:");
 	scanf("%d",&m); 
 	printf("\n请输入n个元素:");
 	for(i=0;i<m;i++)
	 { scanf("%d",&e);
	   ListInsert(&L,i+1,e);
	  } 
 	menu(); 
 	p:
	printf("\n请输入你的选项: ");
	scanf("%d",&choose);
	switch(choose) 
	{ case 1:printf("\n请输入要插入元素的位置:");scanf("%d",&location);printf("\n请输入要插入的元素:");scanf("%d",&e);ListInsert(&L,location,e);goto p; 
	  case 2:printf("\n请输入要删除元素的位置:");scanf("%d",&location);ListDelete(&L,location,&e);printf("\n被删除的数是%d\n",e); goto p;	
	  case 3:printf("\n请输入要获取元素的位置:");scanf("%d",&location);GetElem(L,location,&e);printf("\n获取到的数是%d\n",e); goto p; 
      case 4:print(L); goto p;
      case 5:DestoryList(&L);printf("\n线性表已被销毁"); goto p;
      default:printf("输入错误,请重新输入"); goto p; 
	}
 

 	return 0;
  } 

上述便为常见的顺序表操作代码。
下面给出运行结果图
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
顺序表是一种线性表的数据结构,它的特点是元素之间的顺序是按照元素插入的顺序来确定的。C语言中,顺序表基本操作包括初始化、插入元素、删除元素、按位置查找元素、按值查找元素、求表长和打印等操作。下面是基本操作代码示例: 1. 初始化顺序表: ```c typedef struct SeqList { SLDataType* arr; //顺序表的数据元素存储空间 int size; //顺序表的当前数据个数 int capacity; //顺序表的最大容量 } SL; void SLInit(SL* p) { p->arr = (SLDataType*)malloc(sizeof(SLDataType) * Init_CAPACITY); //动态开辟一块空间给arr if (p == NULL) { perror("malloc fail"); return; } p->size = 0; p->capacity = Init_CAPACITY; } ``` 2. 插入元素: ```c void SLInsert(SL* p, int pos, SLDataType x) { assert(p); assert(pos >= 1 && pos <= p->size + 1); if (p->size == p->capacity) { p->arr = (SLDataType*)realloc(p->arr, sizeof(SLDataType) * (p->capacity * 2)); if (p->arr == NULL) { perror("realloc fail"); return; } p->capacity *= 2; } for (int i = p->size; i >= pos; i--) { p->arr[i] = p->arr[i - 1]; } p->arr[pos - 1] = x; p->size++; } ``` 3. 删除元素: ```c void SLDelete(SL* p, int pos) { assert(p); assert(pos >= 1 && pos <= p->size); for (int i = pos - 1; i < p->size - 1; i++) { p->arr[i] = p->arr[i + 1]; } p->size--; } ``` 4. 按位置查找元素: ```c SLDataType SLFindByPos(SL* p, int pos) { assert(p); assert(pos >= 1 && pos <= p->size); return p->arr[pos - 1]; } ``` 5. 按值查找元素: ```c int SLFindByValue(SL* p, SLDataType x) { assert(p); for (int i = 0; i < p->size; i++) { if (p->arr[i] == x) { return i + 1; } } return -1; } ``` 6. 求表长: ```c int SLLength(SL* p) { assert(p); return p->size; } ``` 7. 打印顺序表: ```c void SLPrint(SL* p) { assert(p); for (int i = 0; i < p->size; i++) { printf("%d ", p->arr[i]); } printf("\n"); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乔梦圆的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值