取交集

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

 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 }

 

posted @ 2015-07-20 20:20  御心飞行  阅读(221)  评论(0编辑  收藏  举报