zoukankan      html  css  js  c++  java
  • 双向链表操作-简单

    源程序:

    #include <stdio.h>

    #include <stdlib.h>

    //双向链表结点的定义

    typedef struct dbnode

    {

      int data;

      struct dbnode *prio, *next;

    }DbNode, linkdblist;

     //创建双向链表

     DbNode *creatlist(void)

     {

      linkdblist *p, *q, *head;

      q = p = (linkdblist *)malloc(sizeof(linkdblist));

      p->next = NULL;

      head = q;

      p = (linkdblist *)malloc(sizeof(linkdblist));

      scanf("%d", &p->data);

      while (p->data != -1)

      {

        q->next = p;

        p->prio = q;

        q = p;

        p = (linkdblist *)malloc(sizeof(linkdblist));

        scanf("%d", &p->data);

      }

      q->next = NULL;

       return head;

    }

    //输出双向链表

     void print(linkdblist *head)

    {

      linkdblist *p;

       p = head->next;

      if (p == NULL) 

      printf("空链表! ");

      while (p != NULL) 

      {

        printf("%d  ", p->data);

        p = p->next;

      }

    //向双向链表中的第i个位置插入一个结点x

     void insertdblist(linkdblist *head, int x, int i)

    {

      linkdblist *p, *q = head;

      if (i == 1) 

        q = head;

      else

      {

        q = q->next;

         int c = 1;   

        while ((c<i - 1) && (q != NULL))

        {

           q = q->next;

           c++;

         }

      }

      if (q != NULL && q->next != NULL) 

      {

        p = (linkdblist *)malloc(sizeof(linkdblist));

        p->data = x;

        p->prio = q;

         p->next = q->next;

        q->next = p;

        q->next->prio = p;

      }

      else

        printf("找不到插入的位置!");

    }

    //删除双向链表中指定位置上的一个结点

    void deletelinkdblist(linkdblist *head, int i)

    {

      linkdblist *p, *q = head;

      if (i == 1)

        q = head;

      else

      {

        q = q->next;

        int c = 1; 

        while (c < i - 1 && q != NULL) 

        { 

          q = q->next;

          c++ ;

        }

      }

      if (q != NULL && q->next != NULL)

      {

        p = q->next;

        p->prio = q;

        p->prio->next = p->next;

        p->next->prio = p->prio; 

        free(p);

      }

      else if (q->next == NULL)

      { 

        p = q;

         p->prio->next = NULL;

        free(p);

      }

      else

        printf("没有找到待删除的结点");

    }

    //双向链表的主函数

    void main()

      linkdblist *head; 

      head = creatlist(); 

      print(head);

      printf(" ====向双向链表的某位置插入一个值==== ");

      int num, i;

      printf("输入插入的位置:");

      scanf("%d", &i);

      printf(" 输入插入的值:"); 

      scanf("%d", &num); 

      insertdblist(head, num, i);

      print(head); 

      printf(" ");

      printf(" ====删除双向链表的某位置上的一个值==== "); 

      int j;

       printf("输入删除的位置:");

      scanf("%d", &j);

       deletelinkdblist(head, j);

      print(head); 

      system("pause");

      printf(" ");

    }

  • 相关阅读:
    递归函数思想理解
    关于C++11 模板中的 using
    DES与3DES
    c++ 内存
    单词替换程序demo
    GF(256)下数的乘法 转化为矩阵乘法
    码片速率的含义
    转载 WCDMA中码片速率、符号速率、bit速率 WCDMA常用概念
    LTE 到GSM 的CCO过程是怎样的【转载自360】
    转载自搜狐科技【技术那些事儿】LTE网络中的用户数据库HSS与传统2G/3G的HLR有何区别?能否融合组网?
  • 原文地址:https://www.cnblogs.com/duanqibo/p/11841263.html
Copyright © 2011-2022 走看看