顺序表简介及特点
顺序表是在计算机内存中以数组的形式保存的线性表,采用顺序存储结构。
特点:
顺序并且连续存储
大小固定,满不能存,空不能取
查找方便,增删改麻烦
定义顺序表
重定义一种数据类型为data_type(以int为例),定义一个顺序表,表中可以存N个数据,count用来记录存入的数据个数。
# define N 10
typedef int data_type;
typedef struct list
{
data_type arr[ N] ;
int count;
} List;
子函数
创建顺序表
通过malloc进行顺序表的空间开辟,并用memset进行清空。
List * CreatList ( void )
{
List * pList = ( List * ) malloc ( sizeof ( List) ) ;
if ( NULL == pList)
{
return NULL ;
}
memset ( pList, 0 , sizeof ( List) ) ;
pList-> count = 0 ;
return pList;
}
插入、显示、修改、销毁
插入:
参数:List *pList,接收顺序表
int pos,接收插入的位置
data_type item,接收插入的数据
返回值:int,使用enum定义了OK,成功返回OK,也可以直接返回0,表示插入成功.
先进行参数判断,然后进行插入,插入必须满足表不为满,然后从最后一个数据开始,依次往后移动一个位置,最后在pos的
位置插入数据,并将pList->count++(数据个数加1)
注意:数据存是从pList->arr[0]开始存,一般将这个元素称为第一个元素,移动的时候需要注意自己的逻辑。
int InsertList ( List * pList, int pos, data_type item)
{
if ( NULL == pList)
{
return LISTNULL;
}
if ( pos < 1 || pos > N)
{
return POSERROR;
}
if ( N == pList-> count)
{
return LISTFULL;
}
int i = 0 ;
for ( i = pList-> count; i >= pos; i-- )
{
pList-> arr[ i] = pList-> arr[ i- 1 ] ;
}
pList-> arr[ pos- 1 ] = item;
pList-> count++ ;
return OK;
}
显示:
参数:
List *pList,接收顺序表
按顺序进行输出,注意i的取值
int ShowList ( List * pList)
{
if ( NULL == pList || pList-> count == 0 )
{
return LISTEMPTY;
}
int i = 0 ;
for ( i = 0 ; i < pList-> count; i++ )
{
printf ( "%d " , pList-> arr[ i] ) ;
}
puts ( "" ) ;
return OK;
}
删除
参数:List *pList,接收顺序表
int pos,接收删除的位置
data_type *item,用来存放删除的数据,返回给main函数,通过指针进行操作
返回值:int,使用enum定义了OK,成功返回OK,也可以直接返回0,表示删除成功.
先进行参数判断,然后进行删除,删除必须满足表不为空,然后将要删除的数据保存到*item中,有效数字减1,将后面的数据依次前移。
int DelList ( List * pList, int pos, data_type * item)
{
if ( NULL == pList)
{
return LISTNULL;
}
if ( 0 == pList-> count)
{
return LISTEMPTY;
}
int i = 0 ;
* item = pList-> arr[ pos- 1 ] ;
for ( i = pos + 1 ; i <= pList-> count; i++ )
{
pList-> arr[ i- 2 ] = pList-> arr[ i- 1 ] ;
}
pList-> count-- ;
return OK;
}
销毁:
通过二级指针接收链表的地址进行销毁
int DestoryList ( List * * ppList)
{
if ( NULL == ppList)
{
return LISTNULL;
}
free ( * ppList) ;
* ppList = NULL ;
return OK;
}
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# define N 10
typedef int data_type;
typedef struct list
{
data_type arr[ N] ;
int count;
} List;
enum ret
{
POSERROR = - 5 ,
MALLOCERROR = - 4 ,
LISTNULL,
LISTFULL,
LISTEMPTY,
OK,
} ;
List * CreatList ( void ) ;
int InsertList ( List * pList, int pos, data_type item) ;
int DelList ( List * pList, int pos, data_type * pData) ;
int ShowList ( List * pList) ;
int DestoryList ( List * * ppList) ;
int main ( int argc, const char * argv[ ] )
{
int op, pos;
data_type item;
List * pList = NULL ;
pList = CreatList ( ) ;
while ( 1 )
{
printf ( "********1.Insert********\n" ) ;
printf ( "********2.Show**********\n" ) ;
printf ( "********3.DelList*******\n" ) ;
printf ( "********-1.EXIT*********\n" ) ;
printf ( "\n" ) ;
printf ( "input your op:" ) ;
scanf ( "%d" , & op) ;
if ( - 1 == op)
break ;
switch ( op)
{
case 1 :
{
printf ( "input your pos and item: " ) ;
scanf ( "%d%d" , & pos, & item) ;
InsertList ( pList, pos, item) ;
break ;
}
case 2 :
{
ShowList ( pList) ;
break ;
}
case 3 :
{
printf ( "input your pos:" ) ;
scanf ( "%d" , & pos) ;
DelList ( pList, pos, & item) ;
printf ( "Del is %d\n" , item) ;
break ;
}
}
}
printf ( "%p\n" , pList) ;
DestoryList ( & pList) ;
printf ( "%p\n" , pList) ;
return 0 ;
}
List * CreatList ( void )
{
List * pList = ( List * ) malloc ( sizeof ( List) ) ;
if ( NULL == pList)
{
return NULL ;
}
memset ( pList, 0 , sizeof ( List) ) ;
pList-> count = 0 ;
return pList;
}
int InsertList ( List * pList, int pos, data_type item)
{
if ( NULL == pList)
{
return LISTNULL;
}
if ( pos < 1 || pos > N)
{
return POSERROR;
}
if ( N == pList-> count)
{
return LISTFULL;
}
int i = 0 ;
for ( i = pList-> count; i >= pos; i-- )
{
pList-> arr[ i] = pList-> arr[ i- 1 ] ;
}
pList-> arr[ pos- 1 ] = item;
pList-> count++ ;
return OK;
}
int ShowList ( List * pList)
{
if ( NULL == pList || pList-> count == 0 )
{
return LISTEMPTY;
}
int i = 0 ;
for ( i = 0 ; i < pList-> count; i++ )
{
printf ( "%d " , pList-> arr[ i] ) ;
}
puts ( "" ) ;
return OK;
}
int DelList ( List * pList, int pos, data_type * item)
{
if ( NULL == pList)
{
return LISTNULL;
}
if ( 0 == pList-> count)
{
return LISTEMPTY;
}
int i = 0 ;
* item = pList-> arr[ pos- 1 ] ;
for ( i = pos + 1 ; i <= pList-> count; i++ )
{
pList-> arr[ i- 2 ] = pList-> arr[ i- 1 ] ;
}
pList-> count-- ;
return OK;
}
int DestoryList ( List * * ppList)
{
if ( NULL == ppList)
{
return LISTNULL;
}
free ( * ppList) ;
* ppList = NULL ;
return OK;
}