Chinaunix首页 | 论坛 | 博客
  • 博客访问: 70687
  • 博文数量: 12
  • 博客积分: 277
  • 博客等级: 二等列兵
  • 技术积分: 130
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-01 02:45
文章分类

全部博文(12)

文章存档

2011年(12)

我的朋友

分类: C/C++

2011-12-25 14:35:25

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

  4. typedef struct student {
  5.     int data;
  6.     struct student *next;
  7. }node;

  8. node *create()
  9. {
  10.     node *head, *p, *s;
  11.     int x, cycle = 1;
  12.     head = (node *)malloc(sizeof(node));
  13.     p = head;

  14.     while (cycle)
  15.     {
  16.         printf("\nplease input the data: ");
  17.         scanf("%d", &x);

  18.         if (x != 0)
  19.         {
  20.             s = (node *)malloc(sizeof(node));
  21.             s->data = x;
  22.             p->next = s;
  23.             p = s;
  24.         }
  25.         else
  26.         {
  27.             cycle = 0;
  28.         }
  29.     }

  30.     head = head->next;
  31.     p->next = NULL;
  32.     return head;
  33. }

  34. int length(node *head)
  35. {
  36.     int n = 0;
  37.     node *p;
  38.     p = head;

  39.     while (p != NULL)
  40.     {
  41.         p = p->next;
  42.         n++;
  43.     }
  44.     return n;
  45. }

  46. void print(node *head)
  47. {
  48.     node *p;
  49.     int n;

  50.     n = length(head);
  51.     p = head;

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

  57.     printf("\n");
  58. }

  59. node *del(node *head, int value)
  60. {
  61.     node *p1, *p2;

  62.     p1 = head;

  63.     while ((value != p1->data) && (p1->next != NULL))
  64.     {
  65.         p2 = p1;
  66.         p1 = p1->next;
  67.     }

  68.     if (value == p1->data)
  69.     {
  70.         if (p1 == head)
  71.         {
  72.             head = p1->next;
  73.             free(p1);
  74.         }
  75.         else
  76.         {
  77.             p2->next = p1->next;
  78.             free(p1);
  79.         }
  80.     }
  81.     else
  82.     {
  83.         printf("\n%d could not been found\n", value);
  84.     }
  85.     return head;
  86. }

  87. node *insert(node *head, int value)
  88. {
  89.     node *p0, *p1, *p2;

  90.     p1 = head;
  91.     p0 = (node *)malloc(sizeof(node));
  92.     p0->data = value;

  93.     while ((p0->data > p1->data)&&(p1->next != NULL))
  94.     {
  95.         p2 = p1;
  96.         p1 = p1->next;
  97.     }

  98.     if (p0->data <= p1->data)
  99.     {
  100.         if (p1 == head)
  101.         {
  102.             head = p0;
  103.             p0->next = p1;
  104.         }
  105.         else
  106.         {
  107.             p0->next = p1;
  108.             p2->next = p0;
  109.         }
  110.     }
  111.     else
  112.     {
  113.         p1->next = p0;
  114.         p0->next = NULL;
  115.     }

  116.     return head;
  117. }

  118. node *reverse(node *head)
  119. {
  120.     node *p, *q, *s;
  121.     
  122.     p = head;
  123.     q = p->next;
  124.     s = q->next;
  125.     p->next = NULL;

  126.     while (s->next)
  127.     {
  128.         q->next = p;
  129.         p = q;
  130.         q = s;
  131.         s = s->next;
  132.     }

  133.     q->next = p;
  134.     s->next = q;
  135.     head = s;

  136.     return head;
  137. }

  138. node *merge(node *head1, node *head2)
  139. {
  140.     if (head1 == NULL)
  141.     {
  142.         return head2;
  143.     }
  144.     if (head2 == NULL)
  145.     {
  146.         return head1;
  147.     }

  148.     node *head, *p1, *p2;
  149.     
  150.     if (head1->data < head2->data)
  151.     {
  152.         head = head1;
  153.         p1 = head1->next;
  154.         p2 = head2;
  155.     }
  156.     else
  157.     {
  158.         head = head2;
  159.         p2 = head2->next;
  160.         p1 = head1;
  161.     }

  162.     node *pcurrent = head;
  163.     while (p1 != NULL && p2 != NULL)
  164.     {
  165.         if (p1->data < p2->data)
  166.         {
  167.             pcurrent->next = p1;
  168.             pcurrent = p1;
  169.             p1 = p1->next;
  170.         }
  171.         else
  172.         {
  173.             pcurrent->next = p2;
  174.             pcurrent = p2;
  175.             p2 = p2->next;
  176.         }
  177.     }

  178.     if (p1 != NULL)
  179.         pcurrent->next = p1;
  180.     if (p2 != NULL)
  181.         pcurrent->next = p2;
  182.     return head;
  183. }

  184. int main()
  185. {
  186.     node *head1, *head2;
  187.     
  188.     head1 = create();
  189.     head2 = create();
  190.     print(head1);
  191.     print(head2);

  192.     head1 = merge(head1, head2);
  193.     print(head1);
  194.     return 0;
  195. }
阅读(1205) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~