Delete the specified element of the 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 ;

// 删除指定元素的值
Status  Locate_Delete_SqList(SqList *L,ElemType x)
     /*  删除线性表L中值为x的第一个结点  */
{
    
      
    int  i=0 , k ; 
    // 从数组第一个元素开始查找值为x的数组元素
    while  (i<L->length)      /*查找值为x的第一个结点*/
    {
    
       
    	// 没找到索引+1
        if  (L->Elem_array[i]!=x )  
            i++ ; 
        // 找到了,标记索引为i的数组元素,将索引为i的替换为索引为i+1的值,以此类推。后面元素往前移。
        else  {
    
      
            for ( k=i+1; k< L->length; k++)
                L->Elem_array[k-1]=L->Elem_array[k]; 
            L->length--;  
            break ; 
        }  
    }
    if  (i>L->length){
    
        
        printf("要删除的数据元素不存在!\n") ; 
        return ERROR ;  
    }
    // 返回删除后的数组
    return  L;    
} 

Guess you like

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