#include<stdio.h> #include<malloc.h> typedef int ElemType; typedef struct LNode{ ElemType data; struct LNode* Next; }LNode,* LinkList; LinkList creat_list1(LinkList p){ p=(LNode *)malloc(sizeof(LNode)); LNode * s; int x; p->Next=NULL; scanf("%d",&x); while(x!=9999){ s=(LNode *)malloc(sizeof(LNode)); s->data=x; s->Next=p->Next; p->Next=s; scanf("%d",&x); } return p; } LinkList creat_list2(LinkList p){ int x; p=(LNode *)malloc(sizeof(LNode)); LNode * s,* r=p; scanf("%d",&x); while(x!=9999){ s=(LNode *)malloc(sizeof(LNode)); s->data=x; r->Next=s; r=s; scanf("%d",&x); } r->Next=NULL; return p; } void traverse_list(LinkList p){ p=p->Next; while(p!=NULL){ printf("%d ",p->data); p=p->Next; } } LinkList getElem(LinkList L,int i){ int j=1; LNode * p=L->Next; if(i==0){ return L; } if(i<1){ return NULL; } while(p!=NULL&&j<i){ p=p->Next; j++; } return p; } LinkList locateElem(LinkList L,ElemType e){ LNode * p=L->Next; while(p&&p->data!=e){ p=p->Next; } return p; } LinkList insert(LinkList L,int i){ LNode * s=(LNode *)malloc(sizeof(LNode)); s->data=99; LNode * p=getElem(L,i-1); s->Next=p->Next; p->Next=s; return L; } LinkList delet(LinkList L,int i){ LNode * p=getElem(L,i-1); LNode * q=p->Next; p->Next=q->Next; free(q); return L; } int size(LinkList L){ int i=0; LinkList p=L->Next; while(p) { p=p->Next; i++; } return i; } void main(){ LinkList head=NULL; head=creat_list2(head); traverse_list(head); /* int i=3; LinkList p=getElem(head,i); printf("第%d个元素是%d ",i,p->data); p=locateElem(head,3); printf("%d",p->data); */ LinkList p=insert(head,2); traverse_list(head); p=delet(head,2); traverse_list(head); printf("长度为%d ",size(head)); }