/* * Copyright (C) 2009 E.Albert, P.Arenas, S.Genaim, G.Puebla, and D.Zanardini * https://costa.ls.fi.upm.es * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package pubs; /** * A simple class with a method listInter which produces a new linked * list with the elements which are in both l1 and l2. Such lists are * not ordered and the complexity is O(l1) * O(l2) * * * @author E.Albert, P.Arenas, S.Genaim, and G.Puebla * @costaContext pubs.ListReverse */ class ListInter { static ListReverse listInter(ListReverse l1,ListReverse l2) { ListReverse r=new ListReverse(); r.next=null; ListReverse last = r; ListReverse aux = l1; while (aux!=null) { if (find(l2,aux.data)==1) { ListReverse node=new ListReverse(); node.data=aux.data; node.next=null; last.next=node; last=node; } aux=aux.next; } r=r.next; return r; } static int find(ListReverse l,int e) { ListReverse aux=l; while (aux!=null && aux.data != e) aux=aux.next; if (aux!=null) return 1; else return 0; } }