zoukankan      html  css  js  c++  java
  • 取交集

    书上题目,取链表交集,放置于另一链表中,使用带头链表

     1 #include <iostream>
     2 using namespace std;
     3 typedef struct node 
     4 {
     5     int date;
     6     struct node *next;
     7 }*linklist,listnode;
     8 linklist initlist(linklist head)
     9 {
    10     head=new listnode;
    11     head->next=NULL;
    12     return head;
    13 }
    14 bool isempty(linklist head)
    15 {
    16     if(head->next==NULL)
    17         return 1;
    18     return 0;
    19 }
    20 void input(linklist head,int n)
    21 {
    22     linklist p=head,temp=NULL,tail=NULL;
    23     while(n--)
    24     {
    25         temp=new listnode;
    26         cin>>temp->date;
    27         temp->next=NULL;
    28         p->next=temp;
    29         p=p->next;
    30     }
    31 }
    32 void output(linklist head)
    33 {
    34     linklist p=head->next;
    35     while(p!=NULL)
    36     {
    37         cout<<p->date<<' ';
    38         p=p->next;
    39     }
    40     cout<<endl;
    41 }
    42 linklist jiao(linklist head1,linklist head2)
    43 {
    44     linklist p2=head2,temp,p1=head1,head,p;
    45     head=new listnode;
    46     head->next=NULL;
    47     p=head;
    48     while(p1!=NULL)
    49     {
    50         p2=head2;
    51         while(p2!=NULL)
    52         {
    53             if(p2->date==p1->date)
    54             {
    55                 temp=new listnode;
    56                 temp->date=p2->date;
    57                 temp->next=NULL;
    58                 p->next=temp;
    59                 p=p->next;
    60             }
    61             p2=p2->next;
    62         }
    63         p1=p1->next;
    64     }
    65     return head;
    66 }
    67 int main()
    68 {
    69     linklist head1,head2,head;
    70     head1=initlist(head1);
    71     head2=initlist(head2);
    72     input(head1,5);
    73     input(head2,5);
    74     head=jiao(head1,head2);
    75     output(head);
    76 
    77 }
  • 相关阅读:
    java Double数据类型比较大小
    java基础02-标识符和关键字
    java基础01-注释
    java程序运行机制
    面试准备之java异常体系
    双亲委派模型
    java类加载器有哪些?
    什么是字节码?采用字节码的好处是什么?
    如何实现一个ioc容器
    ConcurrentHashMap原理,jdk7和jdk8的区别
  • 原文地址:https://www.cnblogs.com/a1225234/p/4663499.html
Copyright © 2011-2022 走看看