Chinaunix首页 | 论坛 | 博客
  • 博客访问: 612841
  • 博文数量: 113
  • 博客积分: 2554
  • 博客等级: 少校
  • 技术积分: 1428
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-21 19:53
文章分类

全部博文(113)

文章存档

2014年(1)

2013年(2)

2012年(94)

2011年(16)

分类: LINUX

2012-03-06 20:37:50

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

  3. typedef struct node
  4. {
  5.     int data;
  6.     struct node *next;
  7. } list_node ,*link_list;


  8. //link_list head; //头指针

  9. link_list create_list(void);

  10. void show_list(link_list head);

  11. void insert_node_list(link_list head,int temp,int position);

  12. void delete_node_list(link_list head,int position);

  13. int list_len(list_node *head);

  14. int main(int argc,int *argv[])
  15. {
  16.     link_list head;

  17.     int len;

  18.     head = create_list();

  19.     show_list(head);

  20.    // insert_node_list(head,10,3);

  21.    // delete_node_list(head,3);

  22.     len = list_len(head);

  23.     printf("len is%d\n",len);

  24.    // show_list(head);

  25.     return 0;
  26. }

  27. link_list create_list(void)
  28. {
  29.     list_node *p,*s; //节点指针
  30.     link_list head;

  31.     head = (list_node *)malloc(sizeof(list_node));
  32.     int temp;
  33.     p = head;

  34.     scanf("%d",&temp);

  35.     while(temp!=0)
  36.     {
  37.         s = (list_node *)malloc(sizeof(list_node));
  38.         s->data = temp;
  39.         p->next = s;
  40.         p = s;
  41.         scanf("%d",&temp);
  42.     }
  43.     head = head->next;
  44.     p->next = NULL;
  45.     return head;
  46. }

  47. void show_list(link_list head)
  48. {
  49.     link_list p;
  50.     p = head;

  51.     while(p != NULL)
  52.     {
  53.         printf("%d\t",p->data);
  54.         p = p->next;
  55.     }
  56.     putchar('\n');
  57. }

  58. void insert_node_list(link_list head,int temp,int position)
  59. {
  60.     int i,j;
  61.     link_list p,s;

  62.     i = 0;
  63.     p = head;

  64.     while(p&& i<position)
  65.     {
  66.         p = p->next;
  67.         i++;
  68.     }
  69.     if(p==NULL)
  70.         printf("error position\n");
  71.     else
  72.     {
  73.         s = (list_node *)malloc(sizeof(list_node));
  74.         s->data = temp;
  75.         s->next = p->next;
  76.         p->next = s;
  77.     }
  78. }


  79. void delete_node_list(link_list head,int position)
  80. {
  81.     int i,j;
  82.     link_list p,q;

  83.     i = 0;
  84.     p = head;

  85.     while(p!=NULL&& i<position)
  86.     {
  87.         p = p->next;
  88.         i++;
  89.     }
  90.     if(p->next==NULL)
  91.         printf("error position\n");
  92.     else
  93.     {
  94.         q = (list_node *)malloc(sizeof(list_node));
  95.         q = p->next;
  96.         p->next = q->next;
  97.         free(q);
  98.         printf("delete ok\n");
  99.     }
  100. }



  101. int list_len(list_node *head)
  102. {
  103.     int len = 0;
  104.     list_node *p;
  105.     p = head;

  106.     while(p != NULL)
  107.     {
  108.         p = p->next;
  109.         len++;
  110.     }
  111.     return (len);
  112. }


  113. void remove_head(list_node *head)
  114. {
  115.     list_node *p;

  116.     p = head->next;
  117.     head->next = p->next;

  118.     free(p);
  119. }
阅读(567) | 评论(0) | 转发(0) |
0

上一篇:约瑟夫问题

下一篇:双向链表操作

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