数据结构学习(C语言)–顺序表的基本操作
# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
struct Sqlist {
int len;
int eNum;
int * pdata;
} ;
void initSqList ( struct Sqlist * psqlist, int length) ;
void showSqList ( struct Sqlist * psqlist) ;
bool append ( struct Sqlist * psqlist, int val) ;
bool isFull ( struct Sqlist * psqlist) ;
bool isEmpty ( struct Sqlist * psqlist) ;
bool insert ( struct Sqlist * psqlist, int pos, int val) ;
bool deleteIndex ( struct Sqlist * psqlist, int pos, int * pval) ;
bool deleteValue ( struct Sqlist * psqlist, int val) ;
bool serchIndex ( struct Sqlist * psqlist, int pos, int * pval) ;
bool serchValue ( struct Sqlist * psqlist, int val, int * pindex) ;
bool modifyByPos ( struct Sqlist * psqlist, int pos, int val) ;
void invert ( struct Sqlist * psqlist) ;
void sortBubbling ( struct Sqlist * psqlist) ;
int main ( void ) {
struct Sqlist sqlist;
int val;
int index;
initSqList ( & sqlist, 10 ) ;
append ( & sqlist, 2 ) ;
append ( & sqlist, 5 ) ;
append ( & sqlist, 8 ) ;
printf_s ( "以追加的方式,添加2,5,8, 结果为:\n" ) ;
showSqList ( & sqlist) ;
printf_s ( "以插入的方式,位置2添加15, 结果为:\n" ) ;
insert ( & sqlist, 2 , 15 ) ;
showSqList ( & sqlist) ;
printf_s ( "以插入的方式,位置6添加12, 结果为(错误测试!):\n" ) ;
insert ( & sqlist, 6 , 12 ) ;
showSqList ( & sqlist) ;
printf_s ( "删除第三个元素,结果为:\n" ) ;
deleteIndex ( & sqlist, 3 , & val) ;
showSqList ( & sqlist) ;
printf_s ( "删除值为15的元素,结果为:\n" ) ;
deleteValue ( & sqlist, 15 ) ;
showSqList ( & sqlist) ;
printf_s ( "补充几个元素,结果为:\n" ) ;
append ( & sqlist, 10 ) ;
append ( & sqlist, 9 ) ;
append ( & sqlist, 22 ) ;
append ( & sqlist, 3 ) ;
append ( & sqlist, 19 ) ;
showSqList ( & sqlist) ;
printf_s ( "查询第5个元素的值:\n" ) ;
serchIndex ( & sqlist, 5 , & val) ;
printf_s ( "第五个元素的值为:%d\n" , val) ;
printf_s ( "查询值为22的元素的位置(下标):\n" ) ;
serchValue ( & sqlist, 22 , & index) ;
printf_s ( "将第一个元素改成11:\n" ) ;
modifyByPos ( & sqlist, 1 , 11 ) ;
showSqList ( & sqlist) ;
printf_s ( "冒泡排序:\n" ) ;
sortBubbling ( & sqlist) ;
showSqList ( & sqlist) ;
return 0 ;
}
void initSqList ( struct Sqlist * psqlist, int length) {
psqlist-> pdata = ( int * ) malloc ( sizeof ( int ) * length) ;
if ( psqlist == NULL ) {
printf_s ( "动态分配空间失败!\n" ) ;
exit ( - 1 ) ;
}
else {
psqlist-> len = length;
psqlist-> eNum = 0 ;
}
}
bool isFull ( struct Sqlist * psqlist) {
if ( psqlist-> eNum == psqlist-> len) {
return true;
}
return false;
}
bool isEmpty ( struct Sqlist * psqlist) {
if ( psqlist-> eNum == 0 )
return true;
return false;
}
bool append ( struct Sqlist * psqlist, int val) {
if ( isFull ( psqlist) ) {
printf_s ( "顺序表已满!\n" ) ;
return false;
}
else {
psqlist-> pdata[ psqlist-> eNum] = val;
printf_s ( "追加%d成功!\n" , val) ;
psqlist-> eNum++ ;
return true;
}
}
void showSqList ( struct Sqlist * psqlist) {
if ( isEmpty ( psqlist) ) {
printf_s ( "顺序表为空!\n" ) ;
}
else {
for ( int i = 0 ; i < psqlist-> eNum; i++ ) {
printf_s ( "%d " , psqlist-> pdata[ i] ) ;
}
putchar ( '\n' ) ;
}
}
bool insert ( struct Sqlist * psqlist, int pos, int val) {
if ( pos< 1 || pos> ( psqlist-> eNum) + 1 ) {
printf_s ( "插入位置不合法!\n" ) ;
return false;
}
if ( isFull ( psqlist) ) {
printf_s ( "空间已满!\n" ) ;
return false;
}
for ( int i = ( psqlist-> eNum) - 1 ; i >= pos - 1 ; i-- ) {
psqlist-> pdata[ i+ 1 ] = psqlist-> pdata[ i] ;
}
psqlist-> pdata[ pos - 1 ] = val;
psqlist-> eNum++ ;
return true;
}
bool deleteIndex ( struct Sqlist * psqlist, int pos, int * pval) {
if ( isEmpty ( psqlist) ) {
printf_s ( "顺序表为空!\n" ) ;
return false;
}
if ( pos< 1 || pos> psqlist-> eNum) {
printf_s ( "删除位置不合法!\n" ) ;
return false;
}
* pval = psqlist-> pdata[ pos- 1 ] ;
for ( int i = pos; i < ( psqlist-> eNum) ; i++ ) {
psqlist-> pdata[ i - 1 ] = psqlist-> pdata[ i] ;
}
printf_s ( "已将第%d个元素%d删除\n" , pos, * pval) ;
psqlist-> eNum-- ;
return true;
}
bool deleteValue ( struct Sqlist * psqlist, int val) {
if ( isEmpty ( psqlist) ) {
printf_s ( "顺序表为空!\n" ) ;
return false;
}
int posi;
for ( int i = 0 ; i < psqlist-> eNum; i++ ) {
if ( psqlist-> pdata[ i] == val) {
posi = i;
}
}
for ( int j = posi + 1 ; j < psqlist-> eNum; j++ ) {
psqlist-> pdata[ j- 1 ] = psqlist-> pdata[ j] ;
}
printf_s ( "已将元素%d删除\n" , val) ;
psqlist-> eNum-- ;
return true;
}
bool serchIndex ( struct Sqlist * psqlist, int pos, int * pval) {
if ( isEmpty ( psqlist) ) {
printf_s ( "顺序表为空!\n" ) ;
return false;
}
if ( pos< 1 || pos> psqlist-> eNum) {
printf_s ( "查询位置不合法!\n" ) ;
return false;
}
* pval = psqlist-> pdata[ pos - 1 ] ;
return true;
}
bool serchValue ( struct Sqlist * psqlist, int val, int * pindex) {
if ( isEmpty ( psqlist) ) {
printf_s ( "顺序表为空!\n" ) ;
return false;
}
int i;
for ( i = 0 ; i < psqlist-> eNum; i++ ) {
if ( psqlist-> pdata[ i] == val) {
* pindex = i;
printf_s ( "第一个22的下标为:%d\n" , * pindex) ;
return true;
}
}
printf_s ( "顺序表中没有%d这个元素\n" , val) ;
return false;
}
bool modifyByPos ( struct Sqlist * psqlist, int pos, int val) {
if ( isEmpty ( psqlist) ) {
printf_s ( "顺序表为空!\n" ) ;
return false;
}
if ( pos< 1 || pos> psqlist-> eNum) {
printf_s ( "修改位置不合法!\n" ) ;
return false;
}
psqlist-> pdata[ pos - 1 ] = val;
printf_s ( "已将第%d个元素修改为:%d\n" , pos, val) ;
return true;
}
void invert ( struct Sqlist * psqlist) {
if ( isEmpty ( psqlist) ) {
printf_s ( "顺序表为空!\n" ) ;
exit ( - 1 ) ;
}
int i = 0 ;
int j = psqlist-> eNum - 1 ;
int t;
while ( i < j) {
t = psqlist-> pdata[ i] ;
psqlist-> pdata[ i] = psqlist-> pdata[ j] ;
psqlist-> pdata[ j] = t;
i++ ;
j-- ;
}
return ;
}
void sortBubbling ( struct Sqlist * psqlist) {
if ( isEmpty ( psqlist) ) {
printf_s ( "顺序表为空!\n" ) ;
exit ( - 1 ) ;
}
int i, j, t;
for ( i = 0 ; i < ( psqlist-> eNum ) ; i++ ) {
for ( j = 0 ; j < ( psqlist-> eNum) - 1 - i; j++ ) {
if ( psqlist-> pdata[ j] > psqlist-> pdata[ j + 1 ] ) {
t = psqlist-> pdata[ j] ;
psqlist-> pdata[ j] = psqlist-> pdata[ j+ 1 ] ;
psqlist-> pdata[ j+ 1 ] = t;
}
}
}
}