voidprint(struct Node *head){if(head ==NULL){printf("Node is empty!\n");return;}while(head ->next !=NULL)//若下一个节点不为空,打印其数值{printf("%d ", head ->next ->value);
head = head ->next;}
int length (struct Node *head){int count =0;while(head -> next !=NULL)//遍历找到链表表尾{
count ++;//每遍历一次计数器加1
head = head ->next;}return count;}
7.链表中间插法
intinsert_index(struct Node *head,int x,int index){if(index <0|| index >length(head))//插入位置不符合要求{printf("index is error!\n");return-1;}for(int i =1; i < index ; i++)//循环遍历到需要插入的位置{
head = head ->next;}struct Node *newnode =(struct Node *)malloc(sizeof(struct Node));if(NULL== newnode){return-1;}
newnode ->value = x;
newnode ->next = head ->next;
head ->next = newnode;}
8.按位改动链表
intupdate_index(struct Node *head,int x,int index){if(index <0|| index >length(head)){printf("index is error!\n");return-1;}for(int i =1; i < index ; i++){
head = head ->next;}
head =head -> next;
head -> value = x;}
9.按位删除链表
intdelete_index(struct Node *head,int index){if(index <0|| index >length(head)){printf("Index is error!\n");return-1;}for(int i =1; i < index; i++){
head = head ->next;}struct Node *ptr = head -> next;
head -> next = ptr -> next;free(ptr);}
10.按数值查找链表
intsearch_value(struct Node *head,int num){int count =0;int place =0;while(head !=NULL){if(head -> value == num){
count ++;printf("The index of the number is :%d\n", place);}
head = head ->next;
place++;}printf("The sum of the number is:%d\n", count);}
11.按照位置来查找链表
intsearch_index(struct Node *head,int index){if(index <0|| index >length(head)){printf("Index is error!\n");}for(int i =0; i < index; i++){
head = head ->next;}printf("The value of the index %d is :%d\n", index, head ->value);return0;}
11.按数值更新链表
intupdate_value(struct Node *head ,int x,int num){while(head -> next !=NULL){if(head -> next -> value == num){
head -> next -> value = x;}
head = head ->next;}return0;}
12.按数值删除链表
intdelete_value(struct Node *head,int value){int len =length(head);for(int i =0; i < len; i++){if(head -> next -> value == value){struct Node *str = head ->next;
head -> next = str -> next;free(str);}else{
head = head -> next;}}}
12.单链表排序
intsort(struct Node *head){int i =0;int j =0;struct Node *p = head;int len =length(head);for( i =0; i < len -1; i++){int flag =0;
head = p;for( j =0; j < len - i -1; j++){if(head ->next ->value > head ->next ->next ->value){struct Node *ptr1 = head ->next;struct Node *ptr2 = head ->next ->next;
ptr1 ->next = ptr2 ->next;
ptr2 ->next = ptr1;
head ->next = ptr2;
flag =1;}
head = head ->next;if(flag =0){break;}}}}
13.单链表倒序排序(一般方法)
voidreserve1(struct Node **head){struct Node *prev =NULL;struct Node *cur =(*head)-> next;struct Node *Next = cur -> next;while(Next !=NULL){
cur ->next = prev;
prev = cur;
cur = Next;
Next = cur ->next;}if(cur !=NULL){
cur ->next = prev;}(*head)->next = cur;}
14.单链表倒序排序(递归法)
struct Node *reserver2(struct Node *head){struct Node *Next =NULL;if(head ->next !=NULL){
Next =reserver2(head ->next);
head ->next ->next = head;
head ->next =NULL;return Next;}return head;}
15.程序结束释放链表空间
struct Node *freeall(struct Node *head){while(head ->next !=NULL){struct Node *ptr = head;
head = head ->next;free(ptr);}free(head);
head =NULL;return head;}
16.主函数(测试部分)
intmain(){struct Node *head;init(&head);int i =1;for( i =1; i <10; i++){insert_tail(head , i);}print(head);for( i =1; i <10; i++){insert_head(head , i);}print(head);insert_index(head,0,8);print(head);update_index(head,99,8);print(head);delete_index(head,8);print(head);search_value(head,9);search_index(head,6);update_value(head,88,9);print(head);delete_value(head,1);print(head);sort(head);print(head);reserve1(&head);print(head);
head ->next =reserver2(head ->next);print(head);
head =freeall(head);print(head);return0;}