#define NULL 0 struct L { int data; int *next; }L,list;
Status InitList(LinkList *L) { *L=(LinkList)malloc(sizeof(struct L)); if(!*L) exit(OVERFLOW); (*L)->next=NULL; return OK; }
Status DestroyList(LinkList *L) { LinkList q; while(*L) { q=(*L)->next; free(*L); *L=q; } return OK; }
Status ListInsert(LinkList L,int i,ElemType e) { int j=0; LinkList p=L,s; while(p&&j<i-1) { p=p->next; j++; } if(!p||j>i-1) return ERROR; s=(LinkList)malloc(sizeof(struct LNode)); s->data=e; s->next=p->next; p->next=s; return OK; } Status ClearList(LinkList L) { LinkList p,q; p=L->next; while(p) { q=p->next; free(p); p=q; } L->next=NULL; return OK; }
Status ListEmpty(LinkList L) { if(L->next) return FALSE; else return TRUE; }
int ListLength(LinkList L) { int i=0; LinkList p=L->next; while(p) { i++; p=p->next; } return i; }
Status GetElem(LinkList L,int i,ElemType *e) { int j=1; LinkList p=L->next; while(p&&j<i) { p=p->next; j++; } if(!p||j>i) return ERROR; *e=p->data; return OK; }
int LocateElem(LinkList L,ElemType e,Status(*compare)(ElemType,ElemType)) { int i=0; LinkList p=L->next; while(p) { i++; if(compare(p->data,e)) return i; p=p->next; } return 0; }
Status PriorElem(LinkList L,ElemType cur_e,ElemType *pre_e) { LinkList q,p=L->next; while(p->next) { q=p->next; if(q->data==cur_e) { *pre_e=p->data; return OK; } p=q; } return INFEASIBLE; }
Status NextElem(LinkList L,ElemType cur_e,ElemType *next_e) { LinkList p=L->next; while(p->next) { if(p->data==cur_e) { *next_e=p->next->data; return OK; } p=p->next; } return INFEASIBLE; }
Status ListDelete(LinkList L,int i,ElemType *e) { int j=0; LinkList p=L,q; while(p->next&&j<i-1) { p=p->next; j++; } if(!p->next||j>i-1) return ERROR; q=p->next; p->next=q->next; *e=q->data; free(q); return OK; }
Status ListTraverse(LinkList L,void(*vi)(ElemType)) { LinkList p=L->next; while(p) { vi(p->data); p=p->next; } printf("\n"); return OK; } main() { STUDENT *head; head=init(); clrscr(); for(;;) { switch(menu_list()) { case 1:InitList();break; case 2:DestroyList();break; case 3:ListInsert();break; case 4:ClearList();break; case 5:ListEmpty();break; case 6:ListLength();break; case 7:GetElem();break; case 8:LocateElem(); break; case 9:PriorElem();break; case 10: NextElem(); break; case 11:ListDelete();break; case 12:ListTraverse();break; case 13:exit(0); } } menu_list() { int i; char *menu[15]={"*****************************", "char1. InitList", "char2. DestroyList", "char3. ListInsert", "char4. ClearList", "char5. ListEmpty", "char6. ListLength", "char7. GetElem", "char8. LocateElem", "char9. PriorElem", "char10. NextElem", "char11. ListDelete", "char12. ListTraverse", "char13. Quit"}; } }