C语言程序——性顺序表.docVIP

  • 6
  • 0
  • 约1.01万字
  • 约 7页
  • 2016-10-20 发布于贵州
  • 举报
C语言程序——性顺序表

程序清单: 线性顺序表1: #define MaxSize 100 typedef char ET; #include stdio.h #include string.h #include stdlib.h struct list { ET alist[MaxSize]; int size; }; void Setnull(struct list *p) { if(p-alist[0]= =NULL) exit(1); else { p-alist[0]=NULL; p-size=0; } } int Length(struct list *p) { return strlen(p-alist); } ET Get(struct list*p,int i) { if(i0||ip-size) { printf(the number is wrong!\n); exit(1); } else return (p-alist[--i]); } int Locate(struct list *p,ET x) { int j; for(j=0;p-alist[j]!=NULL;j++) if(p-alist[j]= =x)break; return ++j; } void Insert(struct list *p,int i,ET x) { if(p-size= =MaxSize) { printf(the list is full\n); exit(1); } int j; j=p-size; for(;j=(i-1);j--) p-alist[j+1]=p-alist[j]; p-alist[i-1]=x; p-size=p-size+1; } void Delete(struct list *p,int i) { if(i0||ip-size) { printf(the number is wrong!\n); exit(1); } for(;p-alist[i]!=NULL;i++) p-alist[i-1]=p-alist[i]; p-alist[i-1]=NULL; p-size=p-size-1; } void Display(struct list *p) { int i; for(i=0;p-alist[i+1]!=NULL;i++) printf(%c-,p-alist[i]); printf(%c,p-alist[i]); printf(\n); } void main() { int i,j,k,l; ET a,b,c; struct list p; Setnull(p); printf(please enter the elements:\n); scanf(%s,p.alist); p.size=Length(p); printf(my list is:\n); Display(p); printf(the length of the list is %d\n,p.size); printf(please enter the elements you want to search:\n); scanf(%s,b); j=Locate(p,b); printf(%cs position is:%d\n,b,j); printf(please enter the position you want to search:); scanf(%d,i); a=Get(p,i); printf(the element in position %d is:%c\n,i,a); printf(please enter the position and the element:); scanf(%d,%c,k,c); Insert(p,k,c); printf(after insert %c,the list is:\n,c); Display(p); printf(please enter the position you want to delete:\n); scanf(%d,l); Delete(p,l); printf(after delete the element in position %d,the list is:\n,l); Display(p); printf(\n); } 线性顺序表2: #include stdio.h #include stdlib.h #include time.

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档