1、/*单链表的各种操作 */#include #include #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,int i);void
2、 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( /*建设链表并设置为空表*/printf(“请输入数据长度: “);scanf(“%d“,for(i=1;inext;return(n);ElemType get(struct LNode *p,int i)int j=1;struct LNode *q=*p;w
3、hile (jnext;j+;if(q!=null)return(q-data);elseprintf(“位置参数不正确!n“);return 0;int locate(struct LNode *p,ElemType x)int n=0;struct LNode *q=*p;while (q!=nulln+;if(q=null)return(-1);elsereturn(n+1);void insert(struct LNode *p,ElemType x,int i)int j=1;struct LNode *s,*q;s=(struct LNode *)malloc(sizeof(str
4、uct LNode);s-data=x;q=*p;if(i=1)s-next=q;*p=s;elsewhile(jnext!=null)q=q-next;j+;if(j=i-1)s-next=q-next;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;elsewhile(jnext!=null)q=q-next;j+;if(q-next!=nullq-next=t-next;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 (q-next=null)printf(“%dn“,q-data);elsewhile(q-next!=null)printf(“%d-“,q-data);q=q-next;printf(“%d“,q-data);printf(“n“);