C语言单链表增删改查基本操作

C语言单链表增删改查基本操作

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


【C语言单链表增删改查基本操作】转载于:https://www.cnblogs.com/hzy3774/archive/2012/08/07/2627508.html

    推荐阅读