数据结构之顺序表的C语言实现
#include<stdio.h>#include<stdlib.h>#include<string.h>#define MAXSIZE 800#define ET charstruct listchar alistMAXSIZE;int size;int length(struct list * p)return p->size;int locate(struct list * p,ET x)int i;for(i=1;i<=p->size;i+)if(p->alisti=x) break;return i;int insert(struct list *p,int i,ET x)int j;if(i>p->size)printf("Can Not Insert The ELEMENT!n");return 1;for(j=p->size;j>=i;j-)p->alistj+1=p->alistj;p->alisti=x;p->size+;/线性表的数量+1return 0;/插入成功返回0int del(struct list *p,int i)int j;if(i>p->size)printf("Can Not Delete No.%d Element Because %d is Bigger Than the Size Of the List!n",i);return -1;for(j=i;j<p->size;j+)p->alistj=p->alistj+1;p->alistp->size='0'p->size-;return 0;char get(struct list * p,int i)if(i>p->size)printf("%d is Bigger Than The list!n",i);exit(1);return p->alisti;void merge(struct list * l1,struct list *l2)int i,j;for(i=l1->size,j=1;j<=l2->size;j+,i+)l1->alisti=l2->alistj;l1->size+=l2->size;void display(struct list * p)int i;for(i=1;i<=p->size;i+)printf("%c",p->alisti);printf("n");int main()int i;struct list a1,a2;struct list *p1,*p2;char l130="&*(&<>?#%$#$%$#$"char l230="|"p1=&a1;p2=&a2;p1->size=strlen(l1);p2->size=strlen(l2);strcpy(a1.alist,l1);strcpy(a2.alist,l2);/原始数据printf("Data:n");puts(p1->alist);puts(p2->alist);printf("n");/插入printf("Insert:n");for(i=1;i<5;i+) insert(p1,16,'t');printf("n");puts(p1->alist);puts(p2->alist);printf("n");/连接两个顺序表printf("Merge:n");merge(p1,p2);display(p1);printf("n");/删除数据printf("Delete:n");for(i=1;i<5;i+) del(p1,14);display(p1);printf("n");/定位printf("Locate:n");i=locate(p1,''); printf("%d",i);printf("n");return 0;