intprint(node head){if(head ==NULL){printf("It is empty!\n");return0;}while(head ->next !=NULL)//只要当前节点的后驱不为空(未到表尾){printf("%d ", head ->next ->value);//打印当前节点储存的数值
head = head ->next;//再将当前节点的后驱指向下一个节点}printf("\n");}
4.双链表的倒序打印
intreprint(node head){if(head ==NULL){printf("It is empty!\n");return0;}while(head ->next !=NULL)//从表头遍历到表尾{
head = head ->next;}while(head ->prev !=NULL)//只要当前节点的前驱不为空(未到表头){printf("%d ", head ->value);//打印当前节点储存的数值
head = head ->prev;//再将当前节点的前驱接上前一个节点}printf("\n");}
5.计算双链表长度
int length (struct Node *head){int count =0;while(head -> next !=NULL){
count ++;
head = head ->next;}return count;}
intinsert_head(node head,int x){
node newnode =(node)malloc(sizeof(struct Node));if(NULL== newnode){return-1;}
newnode ->value = x;if(NULL== head ->next){
newnode ->next =NULL;
head ->next = newnode;
newnode ->prev = head;}else{
newnode ->next = head ->next;
head ->next ->prev = newnode;
head ->next = newnode;
newnode ->prev = head;}return0;}
8.双链表中间插法
intinsert_index(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;}
node newnode =(node)malloc(sizeof(struct Node));if(NULL== newnode){return-1;}
newnode ->value = x;
newnode ->next = head ->next;
head ->next ->prev = newnode;
head ->next = newnode;
newnode ->prev = head;}
9.按位更新
intupdate_index(node head,int x,int index){if(index <0|| index >length(head)){printf("index is error!\n");return-1;}for(int i =0; i < index ; i++){
head = head ->next;}
head -> value = x;}
10.按位删除
intdelete_index(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;}
node newnode =(node)malloc(sizeof(struct Node));if(NULL== newnode){return-1;}
node ptr = head -> next;
head -> next = ptr -> next;
ptr ->next ->prev = head;free(ptr);}
11.按值更新
intupdate_value(node head ,int x,int value){while(head -> next !=NULL){if(head -> next -> value == value){
head -> next -> value = x;}
head = head ->next;}if(head ->value == value){
head ->value = x;}return0;}
12.按值删除
intdelete_value(node head,int value){int len =length(head);for(int i =0; i < len -1; i++){if(head -> next -> value == value){
node str = head ->next;
head -> next = str -> next;
str ->next ->prev = head;free(str);}else{
head = head -> next;}}if(head ->next ->value == value){
head ->next =NULL;}}
13.按值查找
intsearch_value(node head,int value){int count =0;int index =1;while(head ->next !=NULL){if(head ->next -> value == value){
count ++;printf("The index of the number is :%d\n", index);}
head = head ->next;
index++;}printf("The sum of the number is:%d\n", count);}
14.按位查找
intsearch_index(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;}
15.双链表排序
intsort(node head){int i =0;int j =0;
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){
node ptr1 = head ->next;
node ptr2 = head ->next ->next;if(ptr2 ->next !=NULL){
ptr1 ->next = ptr2 ->next;
ptr2 ->next ->prev = ptr1;
ptr2 ->next = ptr1;
ptr1 ->prev = ptr2;
ptr2 ->prev = head;
head ->next = ptr2;
flag =1;}else{
ptr1 ->next =NULL;
ptr2 ->next = ptr1;
ptr1 ->prev = ptr2;
ptr2 ->prev = head;
head ->next = ptr2;
flag =1;}}
head = head ->next;}if(flag ==0){break;}}}
16.程序结束释放链表空间
node freeall(struct Node *head){while(head ->next !=NULL){struct Node *ptr = head;
head = head ->next;free(ptr);}free(head);
head =NULL;return head;}
17.主函数(测试部分)
intmain(){int i =0;
node head;init(&head);for( i =0; i <10; i++){insert_tail(head, i);}print(head);reprint(head);for( i =0; i <10; i++){insert_head(head, i);}print(head);reprint(head);insert_index(head,99,6);print(head);reprint(head);update_index(head ,88,6);print(head);reprint(head);delete_index(head,9);print(head);reprint(head);update_value(head,0,9);print(head);reprint(head);delete_value(head,0);print(head);reprint(head);search_value(head,5);search_index(head,8);sort(head);print(head);reprint(head);
head =freeall(head);print(head);reprint(head);return0;}