/*************************************************************************** *功 能* 实现线性表的基本操作 *描 述* 实现线性表的顺序存储,及其插入、删除、查询等操作 *时 间* 2010-11-14 ***************************************************************************/ #include<stdio.h> #include<malloc.h> #include<windows.h> #include<stdlib.h> #define false 0 #define true 1 #define INITIAL_SIZE 100 //初始化数组的大小 #define INC_SIZE 10 //每次扩展的数组大小 typedef int ElemType; //元素的类型 typedef struct{ // SequentialList{ ElemType* ElemArray; int Used_length; //数组已用大小 int Max_Length; //数组大小 }SqList; //-----------------------------------------表属性操作--------------------------------------- /*初始化线性表*/ int InitSqList(SqList* sqlist){ sqlist->ElemArray = (ElemType*)malloc(sizeof(ElemType)*INITIAL_SIZE); if(sqlist->ElemArray == NULL){ return false; } sqlist->Used_length = 0; sqlist->Max_Length = INITIAL_SIZE; return true; } /*置为空表*/ void CleanSqList(SqList* sqlist){ sqlist->Used_length = 0; } /*销毁表*/ void DestroySqList(SqList* sqlist){ free(sqlist->ElemArray); sqlist = NULL; } /*返回线性表的长度*/ int GetLength(SqList* sqlist){ return sqlist->Used_length; } /*判断线性表是否为空*/ int IsEmpty(SqList* sqlist){ return (sqlist->Used_length==0)? true:false; } //-----------------------------------------表扩展操作--------------------------------------- /*检查并确定是否需增加线性表大小*/ int IncSqListSize(SqList* sqlist){ int i; ElemType* temp = NULL; if(sqlist->Used_length == sqlist->Max_Length){ temp = (ElemType*)malloc(sizeof(ElemType)*(sqlist->Max_Length+INC_SIZE)); if(temp == NULL){ return false; } for(i=0;i<sqlist->Max_Length;i++){ temp[i] = sqlist->ElemArray[i]; } free(sqlist->ElemArray); sqlist->ElemArray = temp; sqlist->Max_Length += INC_SIZE; } return true; } //-----------------------------------------取值操作--------------------------------------- ElemType GetElem(SqList* sqlist,int index){ if(index < sqlist->Used_length && index >= 0){ return sqlist->ElemArray[index]; } printf("Error Index! Please Check!"); return -1; } //-----------------------------------------插入操作--------------------------------------- /*线性表的头插*/ int InsertHead(SqList* sqlist,ElemType elem){ int i; if(IncSqListSize(sqlist)){ for(i=sqlist->Used_length;i>0;i--){ //数组元素后移 腾出 0 位置 sqlist->ElemArray[i] = sqlist->ElemArray[i-1]; } sqlist->ElemArray[0] = elem; sqlist->Used_length++; return true; } return false; } /*线性表的尾插*/ int InsertTail(SqList* sqlist,ElemType elem){ if(IncSqListSize(sqlist)){ sqlist->ElemArray[sqlist->Used_length] = elem; sqlist->Used_length++; return true; } return false; } /*在线性表的index位置插入数据*/ int Insert(SqList* sqlist,ElemType elem,int index){ int i = index; if(IncSqListSize(sqlist)){ for(i=sqlist->Used_length;i>index;i--){ //数组元素后移 腾出 index 位置 sqlist->ElemArray[i] = sqlist->ElemArray[i-1]; } sqlist->ElemArray[index] = elem; sqlist->Used_length++; return true; } return false; } //-----------------------------------------删除操作--------------------------------------- /*删除头结点,并赋值给pElem*/ int DelHead(SqList* sqlist,ElemType* pElem){ int i; if(!IsEmpty(sqlist)){ *pElem = sqlist->ElemArray[0]; for(i=0;i<(sqlist->Used_length)-1;i++){ sqlist->ElemArray[i] = sqlist->ElemArray[i+1]; } sqlist->Used_length--; return true; } return false; } /*删除尾结点,并赋值给pElem*/ int DelTail(SqList* sqlist,ElemType* pElem){ if(!IsEmpty(sqlist)){ *pElem = sqlist->ElemArray[sqlist->Used_length-1]; sqlist->Used_length--; return true; } return false; } /*删除结点index,并赋值给pElem*/ int Delele(SqList* sqlist,ElemType* pElem,int index){ int i; if(!IsEmpty(sqlist)){ *pElem = sqlist->ElemArray[index]; for(i=index;i<(sqlist->Used_length)-1;i++){ sqlist->ElemArray[i] = sqlist->ElemArray[i+1]; } sqlist->Used_length--; return true; } return false; } //-----------------------------------------顺序表连接操作--------------------------------------- /*将sqlist2连接在sqlist1的尾部*/ void AttachSqList(SqList* sqlist1,SqList* sqlist2){ int i; if(!IsEmpty(sqlist2)){ for(i=0;i<sqlist2->Used_length;i++){ InsertTail(sqlist1,sqlist2->ElemArray[i]); } } } //-----------------------------------------显示操作--------------------------------------- /*显示元素(当元素类型变化时,需要重写该函数)*/ void DisplayElem(ElemType elem){ printf("%d",elem); } /*显示表*/ int DisplaySqlist(SqList* sqlist){ int i; if(!IsEmpty(sqlist)){ for(i=0;i<sqlist->Used_length;i++){ DisplayElem(sqlist->ElemArray[i]); printf(" "); } printf("/n"); return true; } return false; } //-----------------------------------------主函数 测试用--------------------------------------- void main(){ int i; char choice; ElemType elem; SqList sqlist,sqlist2; InitSqList(&sqlist); //初始化 InitSqList(&sqlist2); for(i=0;i<10;i++){ //尾插十个数据 InsertTail(&sqlist,i); } DisplaySqlist(&sqlist); //显示 for(i=0;i<10;i++){ InsertHead(&sqlist,i); //头插九个数据 } DisplaySqlist(&sqlist); //显示 Insert(&sqlist,1,10); //在10位置插入数据 DisplaySqlist(&sqlist); //显示 printf(" "); DelHead(&sqlist,&elem); //头删 DisplaySqlist(&sqlist); //显示 printf(" "); DelTail(&sqlist,&elem); //尾删 DisplaySqlist(&sqlist); //显示 printf(" "); Delele(&sqlist,&elem,9); //尾删 DisplaySqlist(&sqlist); //显示 CleanSqList(&sqlist); //清空 printf("Clean the Screen, Y/N? ..."); //清屏 scanf("%c",&choice); if(choice == 'Y'){ system("cls"); } for(i=0;i<8;i++){ InsertTail(&sqlist,i); InsertHead(&sqlist2,i); } AttachSqList(&sqlist,&sqlist2); DisplaySqlist(&sqlist); }