Chinaunix首页 | 论坛 | 博客
  • 博客访问: 17372
  • 博文数量: 17
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 22
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-01 17:59
个人简介

天下没有笨人,只有不努力的人!

文章分类

全部博文(17)

文章存档

2013年(17)

我的朋友

分类: C/C++

2013-09-01 18:04:09


点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <conio.h>
  5. using namespace std;


  6. typedef struct student
  7. {
  8.   int data;
  9.   struct student *next;
  10.   struct student *pre;
  11. }dnode;


  12. //双链表的建立
  13. dnode * creat()
  14. {
  15.   dnode *head,*p,*s;
  16.   int x,cycle =1;
  17.   head = (dnode*)malloc(sizeof(dnode));
  18.   p=head;
  19.   while(cycle)
  20.  {
  21.     printf("\n 请输入数据:");
  22.     scanf("%d",&x);
  23.     if (x !=0)
  24.     {
  25.       s= (dnode*) malloc(sizeof(dnode));
  26.       s->data = x;
  27.       printf("\n %d",s->data);
  28.       p->next = s;
  29.       s->pre =p;
  30.       p= s;
  31.     }
  32.     else cycle = 0;
  33. }


  34.  head = head->next;//此时的head指向双链表中第一个有数据元素的节点
  35.  head->pre= NULL;
  36.  p->next =NULL;


  37.  printf("\n 第一个有数据元素的节点的数据:%d",head->data);
  38.  return (head);
  39. }
  40. //双链表的查找
  41. dnode *find(dnode *head, int num)
  42. {
  43.    dnode *p;
  44.    p1 = head;
  45.    while(p != NULL && p1->data !=num)
  46.    {
  47.      p=p->next;
  48.    }


  49.   if (p=NULL)
  50.   {
  51.     printf("未找到该数据");
  52.   }
  53.    return p;
  54. }

  55. //双链表的删除
  56. dnode *del(dnode *head, int num)
  57. {
  58.    dnode *p1,*p2;
  59.    p1 = head;
  60.    while(num != p1->data && p1->next !=NULL)
  61.    {
  62.      p1=p1->next;
  63.    }
  64.    if (num == p1->data)
  65.    {
  66.      if(p1==head)//要删除的节点为表头节点
  67.      {
  68.         head=head->next;
  69.         head->pre =NULL;
  70.      }
  71.      else if(p1->next == NULL)//要删除的节点为表尾节点
  72.      {
  73.         p1->pre->next = NULL;
  74.      }
  75.      else//要删除的节点处于表中间
  76.      {
  77.         p1->next->pre = p1->pre;
  78.         p1->pre->next = p1->next;
  79.       }
  80.    free(p1);
  81.   }
  82.    else printf("\n %d 未找到该数据",num);
  83.    return (head);
  84. }

  85. //双链表的插入
  86. dnode *insert(dnode *head,int num)
  87. {
  88.     dnode *p0,*p1; p1=head;
  89.     p0=(dnode*)malloc(sizeof(dnode));
  90.     p0->data = num;
  91.     while(p0->data >p1->data && p1->next != NULL)
  92.     {
  93.       p1=p1->next;
  94.      }
  95.     //三种位置的插入,表头,表尾,表中间
  96.     if (p0->data <= p1->data)
  97.     {
  98.        if (head == p1) //在表头插入节点
  99.         {
  100.            p0->next =p1;
  101.            p1->pre = p0;
  102.            head =p0;
  103.         }
  104.        else //在表中间插入节点
  105.        {
  106.            p1->pre->next=p0;
  107.            p0->next =p1;
  108.            p0->pre=p1->pre;
  109.            p1->pre =p0;
  110.        }
  111.    }
  112.    else //在表尾插入节点
  113.    {
  114.       p1->next =p0;
  115.       p0->pre=p1;
  116.       p0->next=NULL;
  117.     }
  118. }


  119. int main()
  120. {
  121.        dnode *head,stud;
  122.        int n,del_num,insert_num;
  123.        head = creat();
  124.        print(head);
  125.        cout < "\n nInt: ";
  126.        cin >> del_num;
  127.        head = del(head,del_num);
  128.        print(head);
  129.        cout << "\n 请输入要插入的数据:";
  130.        cin >> insert_num;
  131.        head = insert(head,insert_num);
  132.        print(head);
  133.        return 0;
  134. }

阅读(301) | 评论(0) | 转发(0) |
0

上一篇:C++内存管理

下一篇:宏与函数

给主人留下些什么吧!~~