顺序表操作(初始化、取值、查找、删除、插入)17计科班

博客围绕顺序表运算展开,重点提及顺序表的初始化、插入和删除操作,这些均为信息技术中数据结构相关的重要内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
using namespace std;
#define MAXSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int ElemType;
typedef int Status;
typedef struct{
  ElemType *elem;
  int length;
}SqList;
Status InitList_Sq(SqList &L)                   //构造一 个空的顺序表L
{
    L.elem=new ElemType[MAXSIZE];   //为顺序表分配空间
    if(!L.elem) exit(OVERFLOW);           //存储分配失败
    L.length=0;				     //空表长度为0
    return OK;
}
int GetElem(SqList L,int i,ElemType &e)
{
  if (i<1||i>L.length) return ERROR;   
   //判断i值是否合理,若不合理,返回ERROR
  e=L.elem[i-1];   //第i-1的单元存储着第i个数据
  return OK;
}
int LocateELem(SqList L,ElemType e)
{
  	for (int i=0;i< L.length;i++)
      	if (L.elem[i]==e) return i+1;                
 	 return 0;
}
Status ListInsert_Sq(SqList &L,int i ,ElemType e)
{
   if(i<1 || i>L.length+1) return ERROR;	         //i值不合法
   if(L.length==MAXSIZE) return ERROR;    //当前存储空间已满     
   for(int j=L.length-1;j>=i-1;j--) 
       L.elem[j+1]=L.elem[j];    //插入位置及之后的元素后移
    L.elem[i-1]=e;                     //将新元素e放入第i个位置
    ++L.length;		     	//表长增1
    return OK;
}
/*删除运算*/
Status ListDelete_Sq(SqList &L,int i)
{
   if((i<1)||(i>L.length)) return ERROR;	 //i值不合法
   for (int j=i;j<=L.length-1;j++)                   
   L.elem[j-1]=L.elem[j];       //被删除元素之后的元素前移  
   --L.length;               	                 //表长减1
  return OK;
}
 

int main()
{
	/*第一步 创建结构体变量,进行初始化*/
	SqList L;
	int Init_flag=InitList_Sq(L);
	cout<<"第一步 创建结构体变量,进行初始化"<<endl;
	if(Init_flag==1)
		cout<<"Init OK!"<<endl;
	else
		cout<<"Init ERROE!"<<endl;
    /*第二步 给顺序表赋值*/
    cout<<"第二步 给顺序表赋值"<<endl;
	for(int i=0;i<20;i++)
		L.elem[i]=i+100;
	 L.length=20;
	cout<<"The SqList value is:";
	for(int j=0;j<20;j++)
		cout<<L.elem[j]<<" ";
	cout<<endl;
	/*第三步 按照位置k取值 */
	cout<<"第三步 按照位置k取值"<<endl;
	int k,value,getElem_flag;
	cout<<"please input SqList getElem_k:";
	cin>>k;
    getElem_flag=GetElem(L,k,value);
	if( getElem_flag==1)
		cout<<"获取到的数据为:"<<value<<endl;
	else
		cout<<"输入的位置越界!"<<endl;

	/*第四步 按值e查找,返回位置*/
	cout<<"第四步 按值e查找,返回位置"<<endl;
	int e,local_flag;
	cout<<"please input local value e:";
	cin>>e;
	local_flag=LocateELem(L,e);
	if(local_flag)
		cout<<"该元素的位置是:"<<local_flag<<endl;
	else
		cout<<"没有找到该元素!"<<endl;

	/*第五步 插入运算*/
	cout<<"第五步 在位置i处插入元素e"<<endl;
	int Insert_i,Insert_value,Insert_flag;
	cout<<"please input Insert_i:";
	cin>>Insert_i;
	cout<<"please input Insert_value:";
	cin>>Insert_value;
	Insert_flag=ListInsert_Sq(L,Insert_i,Insert_value);
    if(Insert_flag)
	{  //插入成果循环输出L中元素
		cout<<"Insert OK!"<<endl;
		cout<<"SqList L Length is:"<<L.length<<endl;
		for(int h=0;h<L.length;h++)
		cout<<L.elem[h]<<" ";
	    cout<<endl;
	}
	else
		cout<<"Insert ERROR!"<<endl;
    /*第六步 删除运算,将表中的第i个元素删除*/
    cout<<"第六步 删除运算,将表中的第i个元素删除"<<endl;
    int delete_i,delete_flag;
	cout<<"please input the delete_i:";
	cin>>delete_i;
	delete_flag=ListDelete_Sq(L,delete_i);
	if(delete_flag)
	{
		//删除成功
		cout<<"delete Ok!"<<endl;
		cout<<"SqList L Length is:"<<L.length<<endl;
		for(int h=0;h<L.length;h++)
		cout<<L.elem[h]<<" ";
		cout<<endl;
	}
	 
	return OK;
}

在 C 语言中,我们可以使用数组来实现顺序表的数据结构。以下是关于顺序表的基本操作初始化取值查找插入删除: 1. **初始化** (创建并初始化顺序表): ```c #include <stdio.h> #define MAX_SIZE 100 // 定义数组大小 typedef struct { int data[MAX_SIZE]; // 数据元素数组 int size; // 当前元素数量 } SeqList; // 初始化函数 void init(SeqList* list) { list->size = 0; } ``` 2. **取值** (访问指定位置的元素): ```c int get(int index, SeqList* list) { if (index >= 0 && index < list->size) { return list->data[index]; } else { printf("Index out of range.\n"); return -1; // 或者抛出错误 } } ``` 3. **查找** (搜索特定值): ```c int search(int value, SeqList* list) { for (int i = 0; i < list->size; i++) { if (list->data[i] == value) return i; // 返回找到的位置 } return -1; // 如果没找到,返回-1 } ``` 4. **插入** (在指定位置添加新元素): ```c void insert(int value, int position, SeqList* list) { if (position <= list->size) { memmove(&list->data[position + 1], &list->data[position], sizeof(int) * (list->size - position)); list->data[position] = value; list->size++; } else { printf("Invalid insertion position.\n"); } } ``` 5. **删除** (移除指定位置的元素): ```c void delete(int position, SeqList* list) { if (position >= 0 && position < list->size - 1) { memmove(&list->data[position], &list->data[position + 1], sizeof(int) * (list->size - position - 1)); list->size--; } else { printf("Invalid deletion position.\n"); } } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值