线性表的基本操作

}

Status CreateList_L(LinkList L){ int i,n; struct LNode *p; printf(\请输入线性表长度n=\ scanf(\ printf(\输入数据:\\n\ for(i=n;i>0;i--){

p=(LinkList)malloc(sizeof(LNode)); scanf(\ p->next=L->next;L->next=p; } return OK; }

Status DestroyList_L(LinkList *L){ if(L) free(L); return OK; }

Status ClearList_L(LinkList L){ L->next=NULL; return OK; }

Status ListEmpty_L(LinkList L){ if(L->next==NULL) return OK; else return ERROR; }

Status ListLength_L(LinkList L) { int j;

struct LNode *p; p=L->next;j=1; while(p){ p=p->next;++j; } j--; return j; }

Status GetElem_L(LinkList L,int i,ElemType *e){ int j;

struct LNode *p; p=L->next;j=1; while(p&&j

p=p->next;++j; } if(!p||j>i) return ERROR; *e=p->date; return OK; }

Status LocateElem_L(LinkList L,ElemType e){ int j;

struct LNode *p; p=L->next;j=1; while(p&&p->date!=e){ p=p->next;++j; } if(!p) return ERROR; return j; }

Status PriorElem_L(LinkList L, ElemType cur_e, ElemType *pre_e){ int i,j;

struct LNode *p; i=LocateElem_L( L, cur_e); p=L->next;j=1; while(p&&jnext;++j; } if(!p||j>i-1) return ERROR; *pre_e=p->date; return OK; }

Status NextElem_L(LinkList L, ElemType cur_e, ElemType *next_e) { int i,j;

struct LNode *p;

i=LocateElem_L( L, cur_e); p=L;j=0;

while(p&&jnext;++j; } if(!p||j>i+1) return ERROR; *next_e=p->date; return OK; }

Status ListInsert_L(LinkList L, int i, ElemType e) { int j;

struct LNode *p,*s; p=L;j=0;

while(p&&jnext;++j; }

if(!p||j>i-1) return ERROR; s=(LinkList)malloc(sizeof(LNode)); s->date=e; s->next=p->next; p->next=s; return OK; }

Status ListDelete_L(LinkList L, int i, ElemType *e) { int j; ElemType m; struct LNode *p,*q; p=L;j=0; while(p->next&&jnext;++j; } if(!(p->next)||j>i-1) return ERROR; q=p->next; p->next=q->next; *e=q->date; free(q); return OK; }

Status ListTraverse_L(LinkList L,int i) { struct LNode *p; printf(\线性表为:\\n\ p=L->next; while(p){ printf(\ p=p->next; } printf(\ return OK; }

void main(){ LinkList L; int i,j,n,select,t; ElemType e,m; struct LNode *p,*q; InitList_L(&L); CreateList_L(L); printf(\ do{

printf(\:判断线性表是否为空\\n\ printf(\:线性表的长度\\n\ printf(\:查询线性表的第i个元素\\n\ printf(\:查找值为e的元素的位置\\n\ printf(\:求数据元素t的前驱\\n\ printf(\:求数据元素t的后继\\n\ printf(\:在线性表第i个位置前插入新元素e\\n\ printf(\:删除线性表第i个元素,返回其值\\n\ printf(\:遍历线性表\\n\ printf(\:清空线性表\\n\ printf(\:结束\\n\ scanf(\ switch(select){

case 1: if(ListEmpty_L(L)) printf(\线性表为空\\n\ else printf(\线性表为非空\\n\ case 2:; printf(\线性表的长度为%d\\n\ case 3: printf(\ scanf(\ if(GetElem_L(L,i,&e)==ERROR) printf(\值不合法\\n\ else printf(\第%d个元素的值为%d\\n\ case 4:printf(\ scanf(\ i=LocateElem_L (L,e); if(i) printf(\第%d个元素的值为%d\\n\ else printf(\线性表中没有值为%d的元素\\n\ case 5:printf(\ scanf(\

if(PriorElem_L(L,t,&e)==1) printf(\元素%d的前驱为%d\\n\\n\ else printf(\元素%d无前驱\\n\ case 6:printf(\ scanf(\

if(NextElem_L(L,t,&e)==1) printf(\元素%d的后继为%d\\n\\n\ else

联系客服:779662525#qq.com(#替换为@)