1、/单链表的各种操作*/include stdio。h#include malloc.h#define null 0typedef int ElemType; /* 字符型数据/struct LNodeElemType data;struct LNode next;;void setnull(struct LNode *p);int length (struct LNode p);ElemType get(struct LNode *p,int i);void insert(struct LNode *p,ElemType x,int i);void dele(struct LNode p,in
2、t i);void display(struct LNode p);int locate(struct LNode *p,ElemType x);void main()struct LNode head,*q; /定义静态变量*/int select,x1,x2,x3,x4;int i,n; int m,g;char e,y; setnull(&head); /建设链表并设置为空表/ printf(”请输入数据长度: ”); scanf(”%d”,n); for(i=1;inext; return(n);ElemType get(struct LNode *p,int i)int j=1;st
3、ruct LNode *q=p;while (ji&q!=null) q=qnext; j+; if(q!=null) return(qdata); elseprintf(位置参数不正确!n);return 0;int locate(struct LNode *p,ElemType x)int n=0; struct LNode q=*p;while (q!=null&qdata!=x) q=qnext; n+; if(q=null) return(1); else return(n+1);void insert(struct LNode *p,ElemType x,int i)int j=1
4、; struct LNode *s,*q; s=(struct LNode )malloc(sizeof(struct LNode)); s-data=x; q=p; if(i=1) snext=q; p=s; else while(ji1&q-next!=null) q=q-next; j+; if(j=i-1) snext=qnext; q-next=s; else printf(”位置参数不正确!n); void dele(struct LNode p,int i)int j=1; struct LNode q=*p,t; if(i=1) t=q; *p=q-next; else while(jnext; q-next=tnext; else printf(”位置参数不正确!n); if(t!=null) free(t);void display(struct LNode *p)struct LNode q; q=p; printf(”单链表显示: ); if(q=null) printf(链表为空!”); else if (qnext=null) printf(”%dn,q-data); else while(qnext!=null) printf(d-”,qdata); q=q-next; printf(”%d,qdata); printf(”n);