单链表操作:读取,插入和删除
1 #include "stdafx.h" 2 #include <string.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 struct Node 7 { 8 int val; 9 struct Node *next; 10 }; 11 12 // build No head node 13 struct Node * build_N_LinkList(int a[], int len) 14 { 15 struct Node * phead = NULL; //struct Node * phead, last; 16 struct Node * last = NULL; 17 int k; 18 for (k = 0; k <=len-1; k++) 19 { 20 struct Node * pnewnode = (struct Node *)malloc(sizeof(struct Node)); 21 pnewnode->val = a[k]; 22 pnewnode->next = NULL; 23 if (k == 0) 24 { 25 phead = pnewnode; 26 /*last->next = pnewnode;*/ 27 last = pnewnode; 28 } 29 else 30 { 31 last->next = pnewnode; 32 last = pnewnode; 33 } 34 } 35 return phead; 36 } 37 38 //build Head node 39 struct Node *build_H_LinkList(int a[], int len ) 40 { 41 struct Node * headnode = (struct Node *)malloc(sizeof(struct Node)); 42 int k; 43 struct Node * phead = NULL; 44 struct Node * plast = NULL; 45 for (k = 0; k <= len - 1; k++) 46 { 47 struct Node * newnode = (struct Node *)malloc(sizeof(struct Node)); 48 newnode->val = a[k]; 49 newnode->next = NULL; 50 if (k == 0) 51 { 52 headnode->next = newnode; 53 phead = headnode; 54 plast = newnode; 55 } 56 else 57 { 58 plast->next = newnode; 59 plast = newnode; 60 } 61 } 62 return phead; 63 } 64 65 // read with no head node 66 int GetElem(struct Node *phead, int i) 67 { 68 int k = i; 69 struct Node * p; 70 p = phead; 71 if (NULL==p) //这就是为什么很多时候常量(NULL)写在前面的原因,一旦写成“=”,编绎器就会报错,就可以直接看出来 ,你以后会经常看到这种写法 72 return -1; 73 while (k != 1) 74 { 75 p = p -> next; 76 --k; 77 } 78 return p->val; 79 } 80 81 //insert element before ith. (no head node)//放在最后一个算插入么??? 82 int ListInsert_N (struct Node *phead, int a, int i, int len) 83 { 84 int k =1,m; 85 struct Node *p = phead; 86 struct Node *pr = phead; 87 struct Node *newnode = (struct Node*)malloc(sizeof (struct Node)); 88 newnode->val = a; 89 newnode->next = NULL; 90 if (i <= 0 || i > len + 1) //either first or last 91 return -1; 92 else if (i==1) //first 93 { 94 newnode->next = p; 95 pr = newnode; 96 } 97 else //1 < i <= len+1, middle&last 98 { 99 100 while (k<i-1) 101 { 102 p = p-> next; 103 ++k; 104 } 105 newnode ->next = p->next; 106 p->next = newnode; 107 } 108 len = len + 1; 109 for (m = 1; m <= len; m++) 110 { 111 printf("%d ", pr->val); 112 pr = pr->next; 113 } 114 return 0; 115 } 116 117 //insert element before ith. (with head node) 118 int ListInsert_H(struct Node *phead, int a, int i, int len) 119 { 120 struct Node * p = phead; 121 struct Node * pr = phead; 122 struct Node *newnode = (struct Node *)malloc(sizeof(struct Node)); 123 newnode->val = a; 124 newnode->next = NULL; 125 int k = 1; 126 int m; 127 if (i <= 0 || i > len + 1)// 128 return -1; 129 else 130 { 131 132 while (k < i)//i==len+1 : add in last. 133 { 134 p = p ->next; 135 ++k; 136 } 137 //此时p指向啥?第i个节点?k==i,退出循环,p指向i? 138 newnode->next = p->next; 139 p->next = newnode; 140 } 141 142 len = len + 1; 143 for (m = 1; m <= len; m++) 144 { 145 pr = pr->next; 146 printf("%d ", pr->val); 147 } 148 return 0; 149 } 150 151 // delete element before ith (No head node) 152 int ListDelete_N(struct Node *phead, int i, int len) 153 { 154 int k; 155 int m; 156 struct Node *p = phead; 157 struct Node *q = NULL; 158 struct Node *pr = phead; 159 if (i <= 0 || i > len) 160 return -1; 161 else if (i == 1) 162 { 163 q = p; 164 pr = p->next; 165 free(q); 166 } 167 else 168 { 169 k = i-1; 170 while (k != 1) 171 { 172 p = p->next; 173 --k; 174 } 175 q = p->next; 176 p->next = q->next; 177 free(q); 178 } 179 len = len - 1; 180 for (m = 1; m <= len; m++) 181 { 182 printf("%d ", pr->val); 183 pr = pr->next; 184 } 185 return 0; 186 } 187 188 //delete element before ith (Head node) 189 int ListDelete_H(struct Node * phead, int i, int len) 190 { 191 struct Node *p = phead; 192 struct Node *pr = phead; 193 struct Node *q = NULL; 194 int k,m ; 195 if (i <= 0 || i > len) 196 return -1; 197 else 198 { 199 k = i - 1; 200 while ( k != 0) 201 { 202 p = p->next; 203 --k; 204 } 205 q = p->next; 206 p->next = q->next; 207 free(q); 208 } 209 len = len - 1; 210 for (m = 1; m <= len; m++) 211 { 212 pr = pr->next; 213 printf("%d ", pr->val); 214 } 215 216 } 217 218 int main() 219 { 220 int a[] = { 1,2,3,4,5 }; 221 int len,tmp; 222 struct Node * phead; 223 224 len = sizeof(a) / sizeof(a[0]); //注意:是sizeof(a),不是sizeof(a[])。 225 phead = build_H_LinkList(a, len); //注意:是a,不是a[]。 226 //phead = build_N_LinkList(a, len); //注意:是a,不是a[]。 227 //ListInsert_H(phead,8,2,len); 228 //ListInsert_N(phead, 8, 6, len); 229 //ListDelete_N(phead, 5,len); 230 ListDelete_H(phead, 1, len); 231 //tmp = GetElem(phead, 3); 232 //printf("%d ", tmp); 233 }