Insert element of C language sequence table

Insert element of C language sequence table


#include<stdio.h>
#include<math.h>
// #define 定义一个标识符来表示一个常量
#define  OK   1
#define  ERROR   -1
#define  MAX_SIZE  100
//  typedef 关键字来定义自己习惯的数据类型名称
typedef  int  Status ;
typedef  int  ElemType ; 
// struct 结构体
typedef  struct  sqlist{
    
       
	// 定义一个数组
	ElemType  Elem_array[MAX_SIZE] ;
	// 数组的长度
    int length ;
} SqList ;

// 传参:指针,插入位置的序号(这个序号索引记得-1),元素:可以为数组,也可以为一个数字**
Status Insert_SqList(SqList *L,int i,ElemType e)
{
    
       
     int j ;
     // 如果i小于0,或者大于数组长度,则返回-1;
    if  ( i<0||i>L->length-1)   return  ERROR ;
    // 如果你的数组长度大于或者等于你分配空间则插入不了元素
    if  (L->length>=MAX_SIZE){
    
        
        printf("线性表溢出!\n");  return  ERROR ;  
    }
    // i-1位置以后的所有结点后移
    for  ( j=L->length-1; j>=i-1; --j )
        L-> Elem_array[j+1]=L->Elem_array[j];
    /*  i-1位置以后的所有结点后移  */
    L-> Elem_array[i-1]=e;    /*  在i-1位置插入结点  */
    // 数组长度+1;
    L->length++ ;
    // 返回数组
    return  L ;  
}

Guess you like

Origin blog.csdn.net/weixin_46073538/article/details/114034129