数据结构——链表
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 struct node 6 { 7 int data; 8 struct node *pNext; 9 }; 10 11 //新建node 12 struct node * create_node(int data) 13 { 14 struct node *p = (struct node *) malloc (sizeof(struct node)); 15 16 if(NULL == p) 17 { 18 printf("malloc error!.\n"); 19 return NULL; 20 } 21 memset(p, 0, sizeof(struct node)); 22 p->data = data; 23 p->pNext = NULL; 24 25 return p; 26 } 27 28 //尾部插入 29 void insert_tail(struct node *pH, struct node *newp) 30 { 31 struct node * p = pH; 32 while(NULL != p->pNext) 33 { 34 p = p->pNext; 35 } 36 p->pNext = newp; 37 } 38 39 //头部插入 40 void insert_head(struct node * pH, struct node *newp) 41 { 42 newp->pNext = pH->pNext; 43 pH->pNext = newp; 44 } 45 46 //excluding header data 47 //不能打印头结点 48 void list_for_each_1(struct node *pH) 49 { 50 struct node *p = pH->pNext; 51 printf("------------begin------------\n"); 52 while(NULL != p->pNext) 53 { 54 printf("node data : %d.\n", p->data); 55 p = p->pNext; 56 } 57 printf("node data : %d.\n", p->data); 58 printf("------------end------------\n"); 59 } 60 61 //excluding header data version 2 simplify 62 //不能打印头结点,版本优化 63 void list_for_each_3(struct node *pH) 64 { 65 struct node *p = pH; 66 printf("------------begin------------\n"); 67 while(NULL != p->pNext) 68 { 69 p = p->pNext; 70 printf("node data : %d.\n", p->data); 71 } 72 printf("------------end------------\n"); 73 } 74 75 //including header data 76 //能打印头结点 77 int list_for_each_2(struct node *pH) 78 { 79 struct node *p = pH; 80 printf("------------begin------------\n"); 81 if(NULL == p) 82 { 83 printf("nothing.\n"); 84 return -1; 85 } 86 while(NULL != p->pNext) 87 { 88 printf("node data : %d.\n", p->data); 89 p = p->pNext; 90 } 91 printf("node data : %d.\n", p->data); 92 printf("------------end------------\n"); 93 return 0; 94 } 95 96 //can not delete header node data 97 //不能删除头结点 98 int delete_node(struct node * pH, int data) 99 { 100 struct node *p = pH; 101 struct node *pPrev = NULL; 102 while(NULL != p->pNext) 103 { 104 pPrev = p; 105 p = p->pNext; 106 if(p->data == data) 107 { 108 if(NULL == p->pNext) 109 { 110 pPrev->pNext = NULL; 111 free(p); 112 } 113 else 114 { 115 pPrev->pNext = p->pNext; 116 free(p); 117 } 118 return 0; 119 } 120 } 121 printf("no node deleted.\n"); 122 return -1; 123 } 124 125 //can delete all node including the tail node and header node 126 //能删除头结点和尾节点 127 struct node * delete_node_2(struct node * pH, int data) 128 { 129 struct node *p = pH; 130 struct node *pPrev = NULL; 131 pPrev = p; 132 while(NULL != p) //traserval to tail 133 { 134 if(p->data == data) 135 { 136 //delete header node 137 if(p->data == pH->data) 138 { 139 pH = p->pNext; 140 free(p); 141 printf("node %d deleted ok.\n",data); 142 return pH; //delete ok! 143 } 144 else 145 { 146 //delete tail node 147 if(NULL == p->pNext) 148 { 149 pPrev->pNext = NULL; 150 free(p); 151 } 152 else 153 { 154 pPrev->pNext = p->pNext; 155 free(p); 156 } 157 printf("node %d deleted ok.\n",data); 158 return pH; 159 } 160 printf("node %d deleted ok.\n",data); 161 } 162 pPrev = p; 163 p = p->pNext; 164 } 165 return pH; 166 } 167 168 169 int main() 170 { 171 struct node * pHeader = create_node(1); 172 printf("Hello world!\n"); 173 174 insert_tail(pHeader, create_node(2)); 175 insert_tail(pHeader, create_node(3)); 176 insert_head(pHeader, create_node(4)); 177 insert_head(pHeader, create_node(5)); 178 list_for_each_2(pHeader); 179 pHeader = delete_node_2(pHeader, 3); 180 pHeader = delete_node_2(pHeader, 2); 181 pHeader = delete_node_2(pHeader, 5); 182 pHeader = delete_node_2(pHeader, 4); 183 pHeader = delete_node_2(pHeader, 1); 184 list_for_each_2(pHeader); 185 // printf("1 = %d\n",pHeader->data); 186 // printf("2 = %d\n",pHeader->pNext->data); 187 // printf("3 = %d\n",pHeader->pNext->pNext->data); 188 // printf("4 = %d\n",pHeader->pNext->pNext->pNext->data); 189 // printf("5 = %d\n",pHeader->pNext->pNext->pNext->pNext->data); 190 191 return 0; 192 }