C语言单链表增删改查基本操作
C语言单链表增删改查基本操作
1 #include <stdio.h> 2 #include <malloc.h> 3 #define LEN sizeof(NODE) 4 5 typedef struct _NODE//节点声明 6 { 7 int val; 8 struct _NODE* next; 9 } NODE, *PNODE; 10 11 void print(PNODE head){//打印所有节点 12 while (head) 13 { 14 printf("%3d",head->val); 15 head = head->next; 16 } 17 printf("\n"); 18 } 19 20 void insertHead(PNODE *pHead, int val){//头插法 21 PNODE n = (PNODE)malloc(LEN); 22 n->val = val; 23 n->next = *pHead; 24 *pHead = n; 25 } 26 27 void insertTail(PNODE *pHead, int val){//尾插法 28 PNODE t = *pHead; 29 PNODE n = (PNODE)malloc(LEN); 30 n->val = val; 31 n->next = NULL; 32 if (*pHead == NULL) 33 { 34 n->next = *pHead; 35 *pHead = n; 36 }else{ 37 while (t->next) 38 { 39 t = t->next; 40 } 41 t->next = n; 42 } 43 } 44 45 void deleteHead(PNODE *pHead){//删除头 46 if (*pHead == NULL) 47 { 48 return; 49 } 50 else 51 { 52 PNODE t = *pHead; 53 *pHead = (*pHead)->next; 54 free(t); 55 } 56 } 57 58 void deleteTail(PNODE *pHead){//删除尾 59 PNODE t = *pHead; 60 if (t == NULL) 61 { 62 return; 63 } 64 else if (t->next == NULL) 65 { 66 free(t); 67 *pHead = NULL; 68 } 69 else{ 70 while (t->next->next != NULL) 71 { 72 t = t->next; 73 } 74 free(t->next); 75 t->next = NULL; 76 } 77 } 78 79 PNODE findByVal(PNODE head, int val){//根据值找到满足条件的第一个节点 80 while (head != NULL && head->val != val) 81 { 82 head = head->next; 83 } 84 return head; 85 } 86 87 PNODE findByIndex(PNODE head, int index){//根据索引找节点 88 if (index == 1) 89 { 90 return head; 91 } 92 else 93 { 94 int c = 1; 95 while (head != NULL && index != c) 96 { 97 head = head->next; 98 c++; 99 } 100 } 101 return head; 102 } 103 104 void insertByIndex(PNODE *pHead, int index, int val){//根据索引插入节点 105 if (index == 1) 106 { 107 insertHead(pHead, val); 108 } 109 else 110 { 111 PNODE t = findByIndex(*pHead,index - 1); 112 if (t == NULL) 113 { 114 return; 115 } 116 else 117 { 118 PNODE n = t->next; 119 t->next = (PNODE)malloc(LEN); 120 t->next->next = n; 121 t->next->val = val; 122 } 123 } 124 } 125 126 void deleteByIndex(PNODE *pHead, int index)//根据结点索引删除结点 127 { 128 if (index == 1) 129 { 130 deleteHead(pHead); 131 } 132 else 133 { 134 PNODE t= findByIndex(*pHead, index - 1); 135 if (t == NULL || t->next == NULL) 136 { 137 return; 138 }else{ 139 PNODE n = t->next->next; 140 free(t->next); 141 t->next = n; 142 } 143 } 144 } 145 146 void deleteByVal(PNODE *pHead, int val)//根据值删掉第一个满足条件的 147 { 148 if (*pHead == NULL)//如果空表退出 149 { 150 return; 151 } 152 else 153 { 154 if ((*pHead)->val == val)//如果第一个就是,删头 155 { 156 deleteHead(pHead); 157 } 158 else 159 { 160 PNODE t = *pHead; 161 while (t->next != NULL && t->next->val != val)//遍历,若t的next为空或者next是要找的节点则退出 162 { 163 t = t->next; 164 } 165 if (t->next)//如果t指向要找的结点的上一个节点 166 { 167 PNODE n = t->next->next;//删除要找的结点 168 free(t->next); 169 t->next = n; 170 } 171 } 172 } 173 } 174 175 void clear(PNODE *pHead)//清除链表 176 { 177 while ((*pHead) != NULL) 178 { 179 deleteHead(pHead);//从头删除 180 } 181 } 182 183 void main() 184 { 185 PNODE head = NULL; 186 187 insertTail(&head,1); 188 deleteHead(&head); 189 insertTail(&head,2); 190 insertTail(&head,3); 191 insertTail(&head,4); 192 insertTail(&head,5); 193 insertTail(&head,6); 194 195 print(head); 196 insertByIndex(&head, 6, 9); 197 print(head); 198 //deleteByIndex(&head,3); 199 deleteByVal(&head, 2); 200 print(head); 201 clear(&head); 202 print(head); 203 insertByIndex(&head,1,12); 204 print(head); 205 }