标签:style class blog code ext color
常规的删除单链表结点的做法是挨个查找到该结点的前一个结点,然后去掉要删除的这个结点,回收内存。这个需要O(n)的时间。
有一种比较快速的删除掉链表指定一个节点的方法,就是把下一个结点的内容复制到当前这个结点,然后把下一次结点删除掉,这个需要考虑当要删除的结点是最后一个结点的情况。
如果刚好是最后一个结点,则需要O(n)的时间,如果不是最后一个结点,可以在O(1)时间内完成删除操作。
1 // 2 // main.c 3 // SingleListDeleteNode 4 // 5 // Created by xxx on 6/20/14. 6 // Copyright (c) 2014 xxx. All rights reserved. 7 // 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 12 typedef struct Node { 13 struct Node *next; 14 int value; 15 }Node; 16 17 Node* genList(int *content, int length); 18 void printList(Node *header); 19 void releaseList(Node *header); 20 void normalDeleteNode(Node *header, Node *node); 21 void fashDeleteNode(Node *header, Node *node); 22 Node* getNodeAtIndex(Node *header, int index); 23 24 int main(int argc, const char * argv[]) 25 { 26 27 // insert code here... 28 printf("Hello, World!\n"); 29 30 Node *header = NULL; 31 int values[10] = {1,2,3,4,5,6,7,8,9,0}; 32 header = genList(values, 10); 33 printList(header); 34 releaseList(header); 35 36 37 printf("show normal delete node result!\n"); 38 header = genList(values, 10); 39 Node *nodeToDel = getNodeAtIndex(header, 3); 40 normalDeleteNode(header, nodeToDel); 41 printList(header); 42 releaseList(header); 43 44 printf("show fast delete node result!\n"); 45 header = genList(values, 10); 46 nodeToDel = getNodeAtIndex(header, 3); 47 fashDeleteNode(header, nodeToDel); 48 printList(header); 49 releaseList(header); 50 51 52 53 return 0; 54 } 55 56 Node* genList(int *content, int length) { 57 Node *header = NULL; 58 Node *curNode = NULL; 59 for (int i=0; i<length; i++) { 60 Node *tmp = (Node *)malloc(sizeof(Node)); 61 tmp->value = content[i]; 62 if (i == 0) { 63 header = tmp; 64 } 65 if (curNode != NULL) { 66 curNode->next = tmp; 67 } 68 curNode = tmp; 69 } 70 curNode->next = NULL; 71 return header; 72 } 73 74 void releaseList(Node *header) { 75 while (header != NULL) { 76 Node *tmp = header; 77 header = header->next; 78 tmp->next = NULL; 79 free(tmp); 80 tmp = NULL; 81 } 82 } 83 84 void printList(Node *header) { 85 printf("LIST : "); 86 while (header != NULL) { 87 printf("%d",header->value); 88 if (header->next != NULL) { 89 printf(" -> "); 90 } 91 header = header->next; 92 } 93 printf("\n"); 94 } 95 96 void normalDeleteNode(Node *header, Node *node) { 97 while (header->next != node) { 98 header = header->next; 99 if (header == NULL) { 100 return; 101 } 102 } 103 header->next = header->next->next; 104 free(node); 105 node = NULL; 106 } 107 108 void fashDeleteNode(Node *header, Node *node) { 109 if (node->next == NULL) { 110 while (header->next != node) { 111 header = header->next; 112 } 113 header->next = NULL; 114 free(node); 115 node = NULL; 116 return; 117 } 118 node->value = node->next->value; 119 Node *tmp = node->next; 120 node->next = node->next->next; 121 free(tmp); 122 tmp = NULL; 123 } 124 125 Node* getNodeAtIndex(Node *header, int index) { 126 while (index > 0 && header != NULL) { 127 header = header->next; 128 index--; 129 } 130 return header; 131 }
两种删除单链表结点的实现,一种O(n),一种O(1),布布扣,bubuko.com
标签:style class blog code ext color
原文地址:http://www.cnblogs.com/huangzizhu/p/3799350.html