#include #define MAXNUM 20 #define true 1 #define false 0 typedef struct { int data[MAXNUM]; int length; }list_type; void createlist(list_type *lp) //创建一个顺序表 { int i,elem; lp->length=0; printf("\nplease input datas of the list\n"); for(i=0;idata[i]=elem; lp->length++; } } void printlist(list_type *lp) //输出 { int i; printf("\nThese %d records are:\n",lp->length); if(lp->length<=0) { printf("No data!\n"); return; } for(i=0;ilength;i++) printf("%d ",lp->data[i]); printf("\nlength of the list is: %d",lp->length); } void insertlist(list_type *lp,int i,int a) //插入一个元素 { int j; if(lp->length>=MAXNUM) { printf("the list is full,can not insert."); } if(i<1 || i>lp->length+1) { printf("\n%d is invalid value",i); } for(j=lp->length-1;j>i-1;j--) lp->data[j+1]=lp->data[j]; lp->data[i]=a; lp->length++; } int deletelist(list_type *lp,int i) //删除一个元素 { int j; if(i<1 || i>lp->length) { printf("elem not exist"); return(false); } for(j=i;jlength;j++) lp->data[j-1]=lp->data[j]; lp->length--; return(true); } void delete_negative(list_type *l) //删除所有的负数 { int i=0,k; for(;ilength;i++) { if(l->data[i]<0) { for(int j=i;jlength-1;j++) l->data[i]=l->data[i+1]; l->length--; i--; } } } void main() { list_type list; int i,j,a; createlist(&list); printlist(&list); printf("\ninsert:Enter i and data:\n"); scanf("%d%d",&i,&a); insertlist(&list,i,a); printf("\nlist after insert:\n"); printlist(&list); printf("\ndelete:Enter j:\n"); scanf("%d",&j); deletelist(&list,j); printf("\nlist after delete:\n"); printlist(&list); printf("\ndelete negative nums:\n"); delete_negative(&list); printlist(&list); }