《数据结构之顺序表的C语言实现》由会员分享,可在线阅读,更多相关《数据结构之顺序表的C语言实现(4页珍藏版)》请在金锄头文库上搜索。
1、#include#include#include#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;isize;i+)if(p-alisti=x) break;return i;int insert(struct list *p,int i,ET x)int j;if(ip-size)printf(Can Not Insert Th
2、e 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(ip-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;jsize;j+)p-alistj=p-alistj+1;p-alistp-si
3、ze=0;p-size-;return 0;char get(struct list * p,int i)if(ip-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;jsize;j+,i+)l1-alisti=l2-alistj;l1-size+=l2-size;void display(struct list * p)int i;for(i=1;isize;i+)pr
4、intf(%c,p-alisti);printf(n);int main()int i;struct list a1,a2;struct list *p1,*p2;char l130=&*(&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;ialist);puts(p2-alist);printf(n);/连接两个顺序表printf(Merge:n);merge(p1,p2);display(p1);printf(n);/删除数据printf(Delete:n);for(i=1;i5;i+)del(p1,14);display(p1);printf(n);/定位printf(Locate:n);i=locate(p1,);printf(%d,i);printf(n);return 0;