Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- struct lista {
- int val;
- struct lista *next;
- };
- struct lista *load(struct lista *head);
- struct lista *intersection(struct lista *head1, struct lista *head2);
- struct lista *deall(struct lista *head);
- void print(struct lista *head);
- int main()
- {
- int v;
- struct lista *p1, *p2, *p3;
- p1=load(p1);
- p2=load(p2);
- p3=intersection(p1,p2);
- print(p3);
- return 0;
- }
- struct lista *load(struct lista *head)
- {
- int v=1;
- struct lista *punt, *newEl;
- head=malloc(sizeof(struct lista));
- head->next=NULL;
- punt=head;
- bool once = true;
- while(v>=0)
- {
- scanf("%d", &v);
- if(once == true && v>=0)
- {
- punt->val = v;
- once = false;
- continue;
- }
- if(v<0)
- return head;
- newEl=malloc(sizeof(struct lista));
- newEl->val=v;
- punt->next=newEl;;
- punt=punt->next;
- }
- return head;
- }
- struct lista *intersection(struct lista *head1, struct lista *head2)
- {
- struct lista *punt3, *punt2, *punt1, *newEl, *head3;
- int a=1, b, c=0;
- bool once=true;
- head3=malloc(sizeof(struct lista));
- head3->next=NULL;
- punt3=malloc(sizeof(struct lista));
- punt2=head2;
- punt1=head1;
- while(punt1!=NULL)
- {
- a=1;
- while(punt2!=NULL && a!=0)
- {
- if(punt2->val==punt1->val)
- {
- a=0;
- punt3=head3;
- if(once == true)
- {
- head3->val = punt2->val;
- b=head3->val;
- once = false;
- c=1;
- break;
- }
- if(head3->val>punt2->val && punt2->val!=b)
- {
- newEl=malloc(sizeof(struct lista));
- newEl->next=head3;
- newEl->val=punt2->val;
- head3=newEl;
- }
- else
- {
- while(punt3->next!=NULL && punt2->val>punt3->next->val && punt2->val!=b)
- punt3=punt3->next;
- if((punt3->next==NULL || (punt2->val<punt3->next->val && punt2->val>punt3->val) && c==0))
- {
- newEl=malloc(sizeof(struct lista));
- newEl->val=punt2->val;
- newEl->next=punt3->next;
- punt3->next=newEl;
- } c=0;
- }
- }
- else
- punt2=punt2->next;
- }
- punt2=head2;
- punt1=punt1->next;
- }
- if(head3->next==NULL)
- head3=NULL;
- return head3;
- }
- struct lista *deall(struct lista *head)
- {
- }
- void print(struct lista *head)
- {
- while(head!=NULL)
- {
- printf("%d\n", head->val);
- head=head->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement