Chinaunix首页 | 论坛 | 博客
  • 博客访问: 312783
  • 博文数量: 172
  • 博客积分: 25
  • 博客等级: 民兵
  • 技术积分: 895
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-09 16:57
文章分类

全部博文(172)

文章存档

2012年(86)

2011年(86)

分类:

2012-03-05 15:06:38

原文地址:双链表操作 作者:printk1986

  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. //#define TYPE int
  4. #define TRUE 1
  5. #define FALSE 0

  6. typedef struct dnode
  7. {
  8.     int data;
  9.     struct dnode *prev,*next;
  10. }d_node,*d_node_link;


  11. d_node_link create_dnode();

  12. int insert_dnode(int position,int x,d_node_link head);

  13. void find_dnode(int i,d_node_link head);

  14. void delete_dnode(int i,d_node_link head);

  15. void print_dnode(d_node_link head);


  16. int main(int argc,char *argv[])
  17. {
  18.     d_node_link d_head;

  19.     d_head = create_dnode();

  20.   // insert_dnode(2,10,d_head);

  21.    delete_dnode(3,d_head);
  22. // find_dnode(3,d_head);

  23.     print_dnode(d_head);

  24.     return 0;
  25. }

  26. d_node_link create_dnode()
  27. {
  28.     d_node_link head,p,s;

  29.     head = (d_node *)malloc(sizeof(d_node));

  30.     p = head;
  31.     int temp = 0;

  32.     while(TRUE)
  33.     {
  34.         printf("please input a int data!\n");

  35.         scanf("%d",&temp);
  36.         if(temp>0)
  37.         {
  38.             s = (d_node *)malloc(sizeof(d_node));
  39.             s->data = temp;
  40.             p->next = s;
  41.             s->prev = p;
  42.             p = s;
  43.         }
  44.         else
  45.             break;
  46.     }
  47.     head = head->next;
  48.     head->prev = NULL;
  49.     p->next = NULL;

  50.     return head;
  51. }

  52. void print_dnode(d_node_link head)
  53. {
  54.     d_node_link p;

  55.     p = head;

  56.     while(p != NULL)
  57.     {
  58.         printf("%d\n",p->data);
  59.         p = p->next;
  60.     }
  61. }

  62. void find_dnode(int i,d_node_link head)
  63. {
  64.     d_node_link p;

  65.     p = head;

  66.     int j = 0;

  67.     while(p != NULL && j<i)
  68.     {
  69.         j++;
  70.         p = p->next;
  71.     }
  72.     printf("%d",p->data);//检查
  73. }

  74. int insert_dnode(int position,int x,d_node_link head)
  75. {
  76.     d_node_link p,s;
  77.     p = head;
  78.     int i = 0;

  79.     while(p!=NULL &&i<position)
  80.     {
  81.         i++;
  82.         p = p->next;
  83.     }
  84.     s = (d_node *)malloc(sizeof(d_node));
  85.     s->data = x;
  86.     s->next = p->next;
  87.     p->next->prev = s->next;
  88.     p->next = s;
  89.     s->prev = p->next;
  90. }

  91. void delete_dnode(int i,d_node_link head)
  92. {
  93.     d_node_link p;
  94.     p = head;
  95.     int j = 0;

  96.     while(p!=NULL && j<i)
  97.     {
  98.         j++;
  99.         p = p->next;
  100.     }
  101.     p->next->prev = p->prev;
  102.     p->prev->next = p->next;
  103.     free(p);
  104. }
阅读(786) | 评论(0) | 转发(0) |
0

上一篇:谈程序员的出路

下一篇:应试题_2

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