Status InitList(LinkList& L){ L = (LinkList)malloc(sizeof(LNode)); if (!L) return ERROR; L->next = nullptr; return OK; }
Status InsertList(LinkList& L, int i, ElemType m){ int j = 1; LNode* pre = L; if (i < 1) return ERROR; while (j++ < i && pre->next != nullptr) pre = pre->next; LNode* newLNode = (LNode*) malloc(sizeof (LNode)); newLNode->data = m; newLNode->next = pre->next; pre->next = newLNode; return OK; }
Status FindInList(LinkList& L, int i, ElemType &m){ int j = 1; LNode* pre = L; if (i < 1) return ERROR; while (j++ < i && pre->next != nullptr) pre = pre->next; if (j - 1 != i || pre->next == nullptr) return ERROR;
m = pre->next->data; return OK; }
intmain(){ LinkList L = nullptr; int i;
if (InitList(L) != OK) { printf("hhhhhh"); return0; }
for (i = 1; i < 10; i++) { if (InsertList(L, i,i) != OK) { printf("hhhhh:%d", i); return0; } else{ printf("insert Ok:%d\n",i); } }
ElemType m = 0; for (i = 1; i < 10; i++) { if (FindInList(L, i, m) == OK) { printf("%d %d\n", i, m); } else{ printf("find wrong:%d\n",i); } }