2013-05-16加入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | #include <stdio.h> #include <stdlib.h> typedef int DT; typedef struct node { DT data; struct node * pNext; } Node, * pNode; pNode CreateList(void); void DelelteList(pNode); void DeleteByData(pNode, DT); void DeleteByIndex(pNode, int); DT FindDataByIndex(pNode pCurr, int index); void Insert(pNode, int, DT); void PrintList(pNode); void UpdateByIndex(pNode, int, DT); int main(void) { pNode pHead = CreateList(); Insert(pHead, 0, 134); Insert(pHead, 1, 345); Insert(pHead, 2235, 234); PrintList(pHead); Insert(pHead, 222220, 285); PrintList(pHead); DeleteByIndex(pHead, 2); PrintList(pHead); //DeleteByIndex(pHead, 2345); //DeleteByIndex(pHead, 1); //DeleteByIndex(pHead, 1); //DeleteByIndex(pHead, 1); //PrintList(pHead); //DeleteByData(pHead, 285); //PrintList(pHead); //DeleteByData(pHead, 34444); //PrintList(pHead); UpdateByIndex(pHead, 2, 3333); PrintList(pHead); printf("%d\n", FindDataByIndex(pHead, 3)); DelelteList(pHead); return 0; } pNode CreateList() { pNode pHead = (pNode)malloc(sizeof(Node)); pHead->pNext = NULL; return pHead; } void DelelteList(pNode pCurr) { if (pCurr != NULL) { pNode pTemp = pCurr; pCurr = pCurr->pNext; free(pTemp); DelelteList(pCurr); } } // 想插入到链表结尾,给index赋以不溢出的大值即可 void Insert(pNode pCurr, int index, DT data) { static int i = 0; if ((index == i++) || (pCurr == NULL) || (pCurr->pNext == NULL)) { pNode pNew = (pNode)malloc(sizeof(Node)); pNew->data = data; pNew->pNext = pCurr->pNext; pCurr->pNext = pNew; i = 0; } else Insert(pCurr->pNext, index, data); } // 索引不正确时会返回-1 DT FindDataByIndex(pNode pCurr, int index) { static int i = 1; if (pCurr->pNext == NULL) return -1; if (index == i++) { return pCurr->pNext->data; } else FindDataByIndex(pCurr->pNext, index); } void UpdateByIndex(pNode pCurr, int index, DT data) { DeleteByIndex(pCurr, index); Insert(pCurr, index - 1, data); } void DeleteByIndex(pNode pCurr, int index) { static int i = 1; if ((0 >= index) || (pCurr == NULL) || (pCurr->pNext == NULL)) return; if (index == i++) { pNode pTemp = pCurr->pNext; pCurr->pNext = pCurr->pNext->pNext; free(pTemp); } else DeleteByIndex(pCurr->pNext, index); i = 1; } void DeleteByData(pNode pCurr, DT data) { if((pCurr == NULL) || (pCurr->pNext == NULL)) return; if(pCurr->pNext->data == data) { pNode pTemp = pCurr->pNext; pCurr->pNext = pCurr->pNext->pNext; free(pTemp); } DeleteByData(pCurr->pNext, data); } void PrintList(pNode pCurr) { if (pCurr == NULL) return; if (pCurr->pNext != NULL) { printf("%d\t", pCurr->pNext->data); PrintList(pCurr->pNext); } else { printf("\n"); return; } } |
目前还没有评论