线性表的顺序存储结构

来源:互联网 发布:网络尖兵 路由器 编辑:程序博客网 时间:2024/04/29 14:34
 

#define  OK   1

#define  ERROR   -1

#define  MAX_SIZE  100

typedef  int  Status ;

typedef  int  ElemType ;

typedef  struct  sqlist

{  

         ElemType  Elem_array[MAX_SIZE] ;

int    length ;

} SqList ;

 

1    顺序线性表初始化

 Status Init_SqList( SqList *L )

L->elem_array=( ElemType * )malloc(MAX_SIZE*sizeof( ElemType ) ) ;

if ( !L -> elem_array ) return  ERROR ;

else

{  

         L->length= 0 ;   

         return OK ; 

}

 

2  顺序线性表的插入

在线性表 L= (a1,…a i-1,ai, ai+1,…,an) 中的第i(1≦i≦n)个位置上插入一个新结点e,使其成为线性表:

      L=(a1,…a i-1,e,ai,ai+1,…,an)

Status Insert_SqList(Sqlist *L,int i ,ElemType e)

 {  

int j ;

if  ( i<0||i>L->length-1)   return  ERROR ;

if  (L->length>=MAX_SIZE)

{   

         printf(“线性表溢出!\n”); 

         return  ERROR ; 

}

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位置插入结点  */

L->length++ ;

return  OK ; 

}

 

3    顺序线性表的删除

在线性表 L=(a1,…a i-1,ai, ai+1,…,an) 中删除结点ai(1≦i≦n),使其成为线性表:L= (a1,…ai-1,ai+1,…,an)

ElemType  Delete_SqList(Sqlist *L,int i)

int  k ;  

ElemType  x ;

if  (L->length==0)

         printf(“线性表L为空!\n”);

         return ERROR; 

}

else if ( i<1||i>L->length )

printf(“要删除的数据元素不存在!\n”) ;

return ERROR ;

}

else 

         x=L->Elem_array[i-1] ;   /*保存结点的值*/

                  for ( k=i ;  k<L->length ; k++)

                      L->Elem_array[k-1]=L->Elem_array[k];

             /*  i位置以后的所有结点前移  */

                  L->length--; 

                  return (x);

         }

}

 

4    顺序线性表的查找定位删除

在线性表 L= (a1,a2,…,an) 中删除值为x的第一个结点。

Status  Locate_Delete_SqList(Sqlist *L,ElemType x)

     /*  删除线性表L中值为x的第一个结点  */

         int  i=0 , k ;

         while  (i<L->length)      /*查找值为x的第一个结点*/

         {  

                   if  (L->Elem_array[i]!=x )  i++ ;

                   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  OK;   

}

原创粉丝点击