创建随机数链表

来源:互联网 发布:大学生沉迷网络 编辑:程序博客网 时间:2024/06/13 21:43

编写自定义函数:建立一个带有头结点head的有20个结点的链表,20个结点所需数值由随机数产生。
编写自定义函数:建立两个链表,把存有数据的链表中的偶数存入一个链表,奇数存入另一个链表中。
编写主函数调用上述两个函数并可输出三个链表中的数据。

完整的代码如下:

[cpp:firstline[0]] view plaincopyprint?
  1. #include "iostream"   
  2. using namespace std;  
  3. #include "time.h"   
  4. struct node  
  5. {  
  6.     int data;  
  7.     node *next;  
  8. };  
  9.   
  10. node *head=NULL;  
  11.   
  12. void InsertNode(node* &head,int value)  
  13. {  
  14.     if(head==NULL)  
  15.     {  
  16.         head=(node *)malloc(sizeof(node));  
  17.         if(head==NULL)  
  18.         {  
  19.             printf("malloc failed");  
  20.             return ;  
  21.         }  
  22.         else  
  23.         {  
  24.             head->data=value;  
  25.             head->next=NULL;  
  26.         }  
  27.     }  
  28.     else  
  29.     {  
  30.         node *temp=(node *)malloc(sizeof(node));  
  31.         if(temp==NULL)  
  32.         {  
  33.             printf("malloc failed");  
  34.             return ;  
  35.         }  
  36.         else  
  37.         {  
  38.             temp->data=value;  
  39.             temp->next=head;  
  40.             head=temp;  
  41.         }  
  42.     }  
  43.     return  ;  
  44. }  
  45. void Insert(int n)  
  46. {  
  47.     srand( (unsigned)time( NULL ) );         //初始化随机数  
  48.     for(int i=0;i<n;i++)  
  49.         InsertNode(head,rand());  
  50.     node *p=(node *)malloc(sizeof(node));  
  51.     p->next=head;  
  52.     head=p;  
  53.     printf("所有的随机数为: ");  
  54.     p=head->next;    //输出测试   
  55.     while(p)  
  56.     {  
  57.         printf("%d  ",p->data);  
  58.         p=p->next;  
  59.     }  
  60.     printf("/n/n");  
  61.     return ;  
  62. }  
  63. void odd_even()  
  64. {  
  65.     node *p=head->next;  
  66.     node *head_odd,*head_even,*q,*t;  
  67.     head_odd=NULL;  
  68.     head_even=NULL;  
  69.     while(p)  
  70.     {  
  71.         if(p->data%2==0)  
  72.         {  
  73.             if(head_even==NULL)  
  74.             {  
  75.                 head_even=(node *)malloc(sizeof(node));  
  76.                 if(head_even==NULL)  
  77.                 {  
  78.                     printf("malloc failed");  
  79.                     return ;  
  80.                 }  
  81.                 else  
  82.                 {  
  83.                     head_even->data=p->data;  
  84.                     head_even->next=NULL;  
  85.                     q=head_even;  
  86.                 }  
  87.             }  
  88.             else  
  89.             {  
  90.                 node *temp=(node *)malloc(sizeof(node));  
  91.                 if(temp==NULL)  
  92.                 {  
  93.                     printf("malloc failed");  
  94.                     return ;  
  95.                 }  
  96.                 else  
  97.                 {  
  98.                     temp->data=p->data;  
  99.                     q->next=temp;  
  100.                     temp->next=NULL;  
  101.                     q=q->next;  
  102.                 }  
  103.             }  
  104.         }  
  105.         else  
  106.         {  
  107.             if(head_odd==NULL)  
  108.             {  
  109.                 head_odd=(node *)malloc(sizeof(node));  
  110.                 if(head_odd==NULL)  
  111.                 {  
  112.                     printf("malloc failed");  
  113.                     return ;  
  114.                 }  
  115.                 else  
  116.                 {  
  117.                     head_odd->data=p->data;  
  118.                     head_odd->next=NULL;  
  119.                     t=head_odd;  
  120.                 }  
  121.             }  
  122.             else  
  123.             {  
  124.                 node *temp=(node *)malloc(sizeof(node));  
  125.                 if(temp==NULL)  
  126.                 {  
  127.                     printf("malloc failed");  
  128.                     return ;  
  129.                 }  
  130.                 else  
  131.                 {  
  132.                     temp->data=p->data;  
  133.                     t->next=temp;  
  134.                     temp->next=NULL;  
  135.                     t=t->next;  
  136.                 }  
  137.             }  
  138.         }  
  139.         p=p->next;  
  140.     }  
  141.     q=head_even;  
  142.     t=head_odd;  
  143.     printf("其中偶数为: ");  
  144.     while(q)  
  145.     {  
  146.         printf("%d  ",q->data);  
  147.         q=q->next;  
  148.     }  
  149.     printf("/n/n");  
  150.     printf("其中奇数为: ");  
  151.     while(t)  
  152.     {  
  153.         printf("%d  ",t->data);  
  154.         t=t->next;  
  155.     }  
  156.     printf("/n/n");  
  157. }  
  158.   
  159. int main(void)  
  160. {  
  161.     Insert(20);  
  162.     odd_even();  
  163.     system("pause");  
  164.     return 0;  
  165. }  

运行的结果如下: