顺序表的建立、插入等;
#include<stdio.h>
typedef int Elemtype ;
const int LIST_INIT_SIZE = 100 ;
const int LISTINCREAMENT = 10 ;
typedef struct {
Elemtype *elem ;
int length = 0 ;
int listsize ;
int incrementsize ;
} Sqlist ;
//初始化操作
void InitList_Sq(Sqlist &L , int maxsize = LIST_INIT_SIZE,int incresize = LISTINCREAMENT)
{
L.elem = new Elemtype[maxsize] ;
L.listsize = maxsize ;
L.incrementsize = incresize ;
}
//顺序表追加空间函数
void increment(Sqlist &L) {
Elemtype *a ;
a = new Elemtype[L.listsize+L.incrementsize] ;
for(int i = 0 ; i < L.length ; i++)
a[i] = L.elem[i] ;
delete[] L.elem ;
L.elem = a ;
L.listsize += L.incrementsize ;
}
//插入元素操作
void ListInsert_Sq(Sqlist &L , int i , Elemtype e) {
if(i<1||i>L.length+1)
printf("i值不合法\n") ;
else if(L.length>=L.listsize)
increment(L) ;
Elemtype *q ;
q = &(L.elem[i-1]) ;
for(Elemtype *p = &(L.elem[L.length-1]) ; p >= q ; --p)
*(p+1) = *p ;
*q = e ;
L.length++ ;
}
int main() {
Sqlist L ;
InitList_Sq(L) ;
for(int i = 0 ; i < 10 ; i++) {
L.elem[i] = i ;
L.length++ ;
}
for(int i = 0 ; i < 10 ; i++)
printf("%d ",L.elem[i]) ;
printf("\n") ;
ListInsert_Sq(L,6,10) ;
for(int i = 0 ; i < 11 ; i++)
printf("%d ",L.elem[i]) ;
printf("\n") ;
return 0 ;
}