问题11:双链表的相关操作

来源:互联网 发布:有20个usb端口的电脑 编辑:程序博客网 时间:2024/06/04 00:51

主要涉及双链表的建立、测长、打印、插入结点以及删除结点等。

实际上跟单链表的操作差不多,只是要特别注意双链表多了指向前驱结点的指针,在写相关代码的时候要特别小心,别弄错了。

代码如下:

#include <stdio.h>#include <malloc.h>typedef struct student{     int data;   struct student *next;   struct student *pre;}dnode;dnode *creat(){   dnode *head, *p, *s;   int input, cycle = 1;   head = (dnode *)malloc(sizeof(dnode));   p = head;   while(cycle)   {       printf("\nplease enter the data:");   scanf("%d", &input);   if(input != 0)   {      s = (dnode *)malloc(sizeof(dnode));  s->data = input;  printf("\n%d", s->data);  p->next = s;  s->pre = p;  p = s;   }   else      cycle = 0;   }   head = head->next;   head -> pre = NULL;   printf("\nThe value of the first node is: %d", head->data);   p->next = NULL;   return head;}//双链表测长,可以看出代码其实跟前面的单链表测长一样int length(dnode *head){    dnode *p;p = head;int count = 0;while(p){   p = p->next;   count++;}return count;}//打印链表void print(dnode *head){   dnode *p;   int len = length(head);   printf("\nNOW %d node(s) in the List:", len);   p = head;   if(head != NULL)   {      while(p)  {     printf("\n     ¥¥¥¥¥  %d", p->data); p = p->next;  }   }}//双链表删除结点//双链表的删除也别忘了有大致4种情况//一是没有找到,二是在第一个结点,三是在最后一个结点,四是在中间的情况dnode *delete_from_dlist(dnode *head, int num){   if(head == NULL)   {      printf("\n链表为空!!");      return head;   }     dnode *current = head;   while(num != current->data && current->next != NULL)   {       current = current->next;   }   if(num == current->data)   {       if(current == head)   {       head = head->next;   head->pre = NULL;   free(current);   }   else if(current->next == NULL)   {      current->pre->next = NULL;  free(current);   }   else   {       current->pre->next = current->next;   current->next->pre = current->pre;   free(current);   }   }   else   {      printf("\nNODE NOT FOUND!!");   }   return head;}//双链表插入结点dnode *insert_to_list(dnode *head, int num){   dnode *cur, *insert_node;   cur = head;   insert_node = (dnode *)malloc(sizeof(dnode));   insert_node -> data = num;   while(insert_node->data > cur->data && cur->next != NULL)   {      cur = cur->next;   }   if(insert_node->data <= cur->data)   {      if(cur == head)  {     insert_node->next = cur; cur->pre = insert_node; head = insert_node;  }  else  {     insert_node->next = cur; insert_node->pre = cur->pre; cur->pre->next = insert_node; cur->pre = insert_node;  }   }   else   {      cur->next = insert_node;  insert_node->pre = cur;  insert_node->next = NULL;   }      return head;}int main(void){   dnode *List = creat();   print(List);   List = delete_from_dlist(List, 4);   print(List);   List = insert_to_list(List, 5);   print(List);   return 0;}

运行结果如下:



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 狗吐拉稀不吃饭怎么办 狗狗咳喘怎么办最有效 半永久纹眉失败怎么办 纹的眉毛太细了怎么办 眉毛颜色做深了怎么办 半永久眼线不掉怎么办 移植9天来月经怎么办 月经迟迟不来该怎么办 lol晋级赛输了怎么办 激素正常的多囊怎么办 右侧输卵管通而不畅怎么办 小该咳嗽老不好怎么办 孩子吓着了怎么办最快 2个月宝宝吓到了怎么办 3个月宝宝吓到了怎么办 宝宝吓着了发烧怎么办 好几个月不遗精怎么办 孕妇吃了黑橄榄怎么办 内膜4mm来月经了怎么办 吃了伟哥没效果怎么办 维a酸乳膏副作用怎么办 颈椎病引起的头晕恶心怎么办 经常头疼怎么办最快最有效 感昌了头晕乏力怎么办 来例假喝啤酒了怎么办 来月经喝啤酒了怎么办 吃的油腻长痘怎么办 兔子拉黑色稀便怎么办 宝宝补钙便秘了怎么办 吃了肾宝片上火怎么办 吃肾宝片就上火怎么办 吃肾宝片后上火怎么办 孕妇吃了扁桃仁怎么办 怀孕吃了扁桃仁怎么办 经期吃了阿胶糕怎么办 10儿童咳嗽有痰怎么办 6岁宝宝咳嗽有痰怎么办 儿童6岁咳嗽有痰怎么办 零号胶囊用多了怎么办 7岁半乳房发育,怎么办 妊娠斑怎么办,能去掉吗