/* * List.java * * Created on March 28, 2007, 5:51 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package exercise5; import java.util.*; public class List implements Element { private static class EleArrList extends ArrayList { } private static class linkListNode { EleArrList _data; linkListNode _next; @SuppressWarnings("unchecked") public linkListNode(EleArrList L){ _data = (EleArrList)(L.clone()); _next = null; } } static private linkListNode _head = null; static private linkListNode _tail = null; private linkListNode _ptr; List(){ // this is a dummy } List(EleArrList L) { linkListNode N = new linkListNode(L); // case 1: link list is empty if (_head==null){ _head = N; _tail = N; _ptr = _head; return; } // case two: check if the List is already in the link list linkListNode current = _head; while (current!=null){ if (current._data.equals(L)==true){ _ptr = current; return; } current = current._next; } // case three: add a List to link list _tail._next = N; _tail = _tail._next; _ptr = N; return; } public static List nil(){ EleArrList L = new EleArrList(); return new List(L); } public static List cons(Element x, List k){ @SuppressWarnings("unchecked") EleArrList L = (EleArrList)k._ptr._data.clone(); L.add(0,x); return new List(L); } public Element getMember(int i) throws BadIndexException{ if ((i<0)||(i>(_ptr._data.size()-1))) { throw new BadIndexException(this,i); } return (Element)(_ptr._data.get(i)); } public static List take(int i, List k) throws BadIndexException { @SuppressWarnings("unchecked") EleArrList L = (EleArrList)(k._ptr._data.clone()); int len = L.size(); if ((i<0)||(i>len)) { throw new BadIndexException(k,i); } for (int count=i; countlen)) { throw new BadIndexException(k,i); } for (int count=0; count0) { s = s+this._ptr._data.get(len-1).toString()+"]"; }else{ s = s+"]"; } return s; } }