• 【原创】双向链表的基本操作


      1 #include<iostream>
      2 using namespace std;
      3 
      4 //链表的结构
      5 typedef struct doubleLink
      6 {
      7     int data;
      8     struct doubleLink *pre;
      9     struct doubleLink *next;
     10 }dnode;
     11 
     12 //创建链表
     13 dnode* Creatdlink()
     14 {
     15     dnode* head;
     16     dnode* p;
     17     dnode* pnext;
     18     int temp;
     19     head = (dnode*)malloc(sizeof(dnode));
     20     head->pre = NULL;
     21     head->next = NULL;
     22     p = head;
     23     printf("请输入数据:");
     24     while (scanf("%d", &temp)!=EOF)
     25     {
     26         pnext = (dnode*)malloc(sizeof(dnode));
     27         pnext->data = temp;
     28         p->next = pnext;
     29         pnext->pre = p;
     30         pnext->next = NULL;
     31         p = pnext;
     32     }
     33     return head;
     34 }
     35 
     36 //插入结点
     37 dnode* insertdnode(dnode* head, int pos, int i)
     38 {
     39     int temp;
     40     dnode* prev;
     41     dnode* p;
     42     prev = head;
     43     for (temp = 0; temp < pos - 1; temp++)
     44         prev = prev->next;
     45     p = (dnode*)malloc(sizeof(dnode));
     46     p->data = i;
     47     p->pre = prev;
     48     p->next = prev->next;                                                                                     
     49     prev->next = p;
     50     prev->next->pre = p;
     51     return head;
     52 }
     53 
     54 //删除结点
     55 dnode* deletednode(dnode* head, int i)
     56 {
     57     dnode* p;
     58     p = head;
     59     while (p != NULL)
     60     {
     61         if (p->data == i)
     62         {
     63             p->pre->next = p->next;
     64             p->next->pre = p->pre;
     65             free(p);
     66             return head;
     67         }
     68         p = p->next;
     69     }
     70     printf("data is not found
    ");
     71     return head;
     72 }
     73 int main()
     74 {
     75     dnode* head;
     76     dnode* p;
     77     head = Creatdlink();
     78     p = head->next;
     79     while (p != NULL)
     80     {
     81         printf("%d ", p->data);
     82         p=p->next;
     83     }
     84     cout << endl;
     85 
     86     head = deletednode(head, 8);
     87     head = deletednode(head, 2);
     88     p = head->next;
     89     while (p != NULL)
     90     {
     91         printf("%d ", p->data);
     92         p = p->next;
     93     }
     94     cout << endl;
     95 
     96     head = insertdnode(head, 1, 8);
     97     p = head->next;
     98     while (p != NULL)
     99     {
    100         printf("%d ", p->data);
    101         p = p->next;
    102     }
    103     cout << endl;
    104 
    105     system("pause");
    106     return 0;
    107 }

      如有不对的地方,非常欢迎给予指导!

      如果您觉得这篇文章对您有所帮助,您可以点击右边的“打赏”功能,也可以点击下方的“好文要顶”按钮,因为这两种肯定,都让我更加相信自己所做的工作是有意义的,也是支持我继续写下去的最大动力!
      感谢您给予的支持!

  • 相关阅读:
    邻接表(网)
    邻接表(无向图)
    邻接表(无向图)
    邻接表(有向图)
    邻接表(有向图)
    邻接表(无向图)
    邻接表(有向图)
    邻接表(有向图)
    邻接表(网)
    完美解决循环线性操作倒计时无报错
  • 原文地址:https://www.cnblogs.com/engraver-lxw/p/7631545.html
Copyright © 2020-2023  润新知