#include<stdio.h> #include<stdlib.h> #define LIST_INIT_SIZE 100 #define LISTINCREMEMT 10 #define OK 1 #define OVERFLOW -2 #define ERROR -1 typedef int ElemType; typedef int Status; typedef struct { ElemType *elem; int length; int listsize; } Sqlist; //**********构造空的线性表 void InitLIst_Sq(Sqlist &L) { L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType)); if(!L.elem)exit(OVERFLOW); L.length=0; L.listsize=LIST_INIT_SIZE; } //**********销毁线性表 void DestroyList(Sqlist &L) { if(L.length>0)free(L.elem); } //**********置空线性表 void ClearList(Sqlist &L) { if(L.length>0)L.length=0; } //判断线性表是否空 Status IsEmpty(Sqlist &L) { if(L.length>0)return 1; else return 0; } //取表长 Status GetLength(Sqlist &L) { return L.length; } //返回指定位置元素值 Status GetElem (Sqlist &L,int i) { return L.elem[i-1]; } //**********值查找 Status SearchElem_Val(Sqlist L,ElemType e) { ElemType *p=L.elem; int i=1; while(i<=L.length&&(*(p++)!=e))i++; if(i<=L.length)return i; else return 0; } //**********定位插入 Status ListInsert(Sqlist &L,int i,ElemType e) { if(i<1||i+1>L.length+1)return ERROR; if(L.length>L.listsize) { ElemType *newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMEMT)*sizeof(ElemType)); if(!newbase)exit(OVERFLOW); L.elem=newbase; L.listsize+=LISTINCREMEMT; } ElemType *q=&(L.elem[i-1]),*p=&(L.elem[L.length-1]); for(; p>=q; p--) *(p+1)=*p; *q=e; ++L.length; return OK; } //**********定位删除 Status ListDelete_loc(Sqlist &L,int i) { if(i<1||i>L.length)return ERROR; ElemType *p=&(L.elem[i-1]); ElemType *q=L.elem+L.length-1; for( ++p; p<=q; ++p) *(p-1)=*p; --L.length; return OK; } //定值删除 void ListDelete_val(Sqlist &L,ElemType e) { ElemType *tmp,*p=L.elem,*q=L.elem+L.length-1; for(; p<=q; p++) { if(*p==e) { L.length--; for( tmp=p; tmp<=q; tmp++) *tmp=*(tmp+1); } } } //冒泡排序 void Maopao(Sqlist &L) { int tmp,i,j,len=GetLength(L); for(int i=0;i<len;i++) { for(int j=i+1;j<len;j++) { if(L.elem[j]<L.elem[i]) { tmp=L.elem[j]; L.elem[j]=L.elem[i]; L.elem[i]=tmp; } } } } void Print(Sqlist L) { printf(" ****打印线性表**** "); for(int i=0; i<L.length; i++) { if(i==0)printf("%d",L.elem[i]); else printf(" %d",L.elem[i]); } printf(" "); } int main() { Sqlist L; InitLIst_Sq(L); while(~scanf("%d",&L.length)) { for(int i=0; i<L.length; i++) { scanf("%d",&L.elem[i]); } Print(L); printf(" ****排序**** "); Maopao(L); Print(L); printf(" ****判空线性表**** "); if(IsEmpty(L))printf("非空 "); else printf("空表 "); printf(" ****取表长**** "); printf("%d ",GetLength(L)); printf(" ****定位元素**** 输入位置 "); int num,num2; scanf("%d",&num); if(GetElem(L,num)) { printf("%d ",GetElem(L,num)); } printf(" ****按值查找位置**** 输入值 "); scanf("%d",&num); if(SearchElem_Val(L,num)) { printf("%d ",SearchElem_Val(L,num)); } printf(" ****定位插入元素**** 输入位置和插入的值 "); scanf("%d%d",&num,&num2); if(ListInsert(L,num,num2)==1) { Print(L); } printf(" ****取表长**** "); printf("%d ",GetLength(L)); printf(" ****定位删除元素**** 输入删除位置 "); scanf("%d",&num); if(ListDelete_loc(L,num)==1) { Print(L); } printf(" ****定值删除元素**** 输入值 "); scanf("%d",&num); ListDelete_val(L,num); Print(L); printf(" ****取表长**** "); printf("%d ",GetLength(L)); } }