// ????????????????????????????????¦Ê????????????????????????§¿?? // ???:totuma.cn #include #include typedef struct LNode { int data; struct LNode *next; } LNode, *LinkList; // ????????? bool List_Init(LinkList &pHead) { pHead = NULL; return true; } // ?§Ø???????????? bool List_Empty(LinkList pHead) { return pHead == NULL; } // ???????????§¹????????? int List_Length(LinkList pHead) { int count = 0; for (LNode *p = pHead; p != NULL; p = p->next) count++; return count; } // totuma.cn // ???????????????????? LinkList List_Create(LinkList &pHead) { LNode *pTemp; int x; scanf("%d", &x); while (x != 999) { pTemp = (LNode *)malloc(sizeof(LNode)); pTemp->data = x; pTemp->next = pHead; pHead = pTemp; scanf("%d", &x); } return pHead; } // totuma.cn // ??¦Ë?????i=1????????i=length+1?????¦Â bool List_Insert(LinkList &pHead, int i, int e) { if (i < 1 || i > List_Length(pHead) + 1) return false; LNode *pTemp = (LNode *)malloc(sizeof(LNode)); if (i == 1) { // ?????1????????? pTemp->data = e; pTemp->next = pHead; pHead = pTemp; return true; } LNode *p; // ???p??????????Úb???? int j = 1; // ???p????????????? p = pHead; // p ????1???????????????? while (p != NULL && j < i - 1) { // ???????????¦Ë????????? p = p->next; j++; } pTemp->data = e; pTemp->next = p->next; p->next = pTemp; return true; } // totuma.cn // ???????????¦Ë?? LNode *List_Get_Elem(LinkList pHead, int e, int &i) { i = 1; LNode *p = pHead; while (p != NULL && p->data != e) { p = p->next; i = i + 1; } return p; } // totuma.cn // ??¦Ë???????i=1??????i=length??¦Â bool List_Del(LinkList &pHead, int i) { if (i < 1 || i > List_Length(pHead)) return false; LNode *p = pHead; // p ????? if (i == 1) { // ??????????? pHead = p->next; free(p); return true; } // ????????¦Ë?????¦Ë???i==1??????????j=2??? for (int j = 2; j < i; j++) p = p->next; LNode *q = p->next; // ???????? p->next = q->next; // p->next q q->next ????q free(q); return true; } // totuma.cn // ???????????? void List_Show(LinkList pHead) { LNode *node = pHead; printf("???????"); do { printf("%d??", node->data); node = node->next; } while (node != NULL); printf("\n"); } int main() { LinkList pHead; List_Init(pHead); printf("?????§á??%s\n", List_Empty(pHead) ? "??" : "???"); List_Create(pHead); List_Show(pHead); printf("?????????%d\n\n", List_Length(pHead)); printf("¦Ë???1(???)??????0\n"); List_Insert(pHead, 1, 0); List_Show(pHead); printf("?????????%d\n\n", List_Length(pHead)); printf("¦Ë???%d(¦Â??)??????99\n", List_Length(pHead) + 1); List_Insert(pHead, List_Length(pHead) + 1, 99); List_Show(pHead); printf("?????????%d\n\n", List_Length(pHead)); printf("???¦Ë???1(??)\n"); List_Del(pHead, 1); List_Show(pHead); printf("?????????%d\n\n", List_Length(pHead)); printf("???¦Ë???%d(¦Â??)\n", List_Length(pHead)); List_Del(pHead, List_Length(pHead)); List_Show(pHead); printf("?????????%d\n\n", List_Length(pHead)); printf("???????5????¦Ë??\n"); int i = -1; // -1 ?¦Ä??? LNode *p5 = List_Get_Elem(pHead, 5, i); printf("??5????¦Ë?????%d\n\n", i); printf("?????§á??%s\n", List_Empty(pHead) ? "??" : "???"); return 0; }