#include <stdio.h>
#include <stdlib.h>
#define true 1
#define false 0
typedef struct _nodo{
int data;
struct _nodo *next;
struct _nodo *prev;
}nodo;
typedef struct _lista{
nodo *head;
}lista;
lista *criaLista(){
lista
*l
= (lista
*)malloc(sizeof(lista
)); if (l != NULL){
l->head = NULL;
return l;
}
return NULL;
}
nodo *criaNodo(int data){
nodo
*n
= (nodo
*)malloc(sizeof(nodo
)); if (n != NULL){
n->data = data;
n->next = NULL;
n->prev = NULL;
return n;
}
return NULL;
}
int inserirFim(lista *l, int data){
if (l == NULL) return false; //o teste de lista NULL logo aqui no topo
nodo *n = criaNodo(data);
if (l->head == NULL){
l->head = n;
n->next = n->prev = n;
}
else{
n->next = l->head;
n->prev = l->head->prev;
l->head->prev->next = n;
l->head->prev = n;
}
return true; //retorno true so uma vez
}
void display(lista *l){
if (l != NULL && l->head != NULL){
nodo *temp = l->head;
while (temp->next != l->head){
temp = temp->next;
}
}
}
lista *intersecao(lista *l, lista *l1){
if (l != NULL && l->head != NULL && l1 != NULL && l1->head != NULL){
lista *l2 = criaLista();
nodo *temp = l->head->next;
nodo *temp1 = l1->head->next;
inserirFim(l2, l->head->data);
inserirFim(l2, l1->head->data);
while (temp != l->head && temp1 != l1->head){
inserirFim(l2, temp->data);
inserirFim(l2, temp1->data);
temp = temp->next;
temp1 = temp1->next;
}
if (temp == l->head) {
temp = temp1;
l = l1;
}
while (temp != l->head){
inserirFim(l2, temp->data);
temp = temp->next;
}
return l2;
}
return NULL; //estava em falta
}
int main()
{
//desconsidera a main, era só pra testar se tava funcionando.
// por isso ja inserir os numeros ordenados
lista *l = criaLista();
lista *l1 = criaLista();
inserirFim(l, 100);
inserirFim(l, 90);
inserirFim(l, 80);
inserirFim(l, 70);
inserirFim(l, 60);
inserirFim(l, 50);
inserirFim(l, 40);
display(l);
inserirFim(l1, 100);
inserirFim(l1, 70);
inserirFim(l1, 50);
inserirFim(l1, 40);
display(l1);
lista *l3 = intersecao(l1, l);
display(l3);
return 0;
}
I2lu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