@@ -0,0 +1,2336 @@
+class node(object):%0A%0A    def __init__(self, val, pnext=None, pprev=None):%0A        self.val = val%0A        self._next = pnext%0A        # self._prev = pprev%0A%0A    def __repr__(self):%0A        return str(self.val)%0A%0A%0Aclass Chain(object):%0A%0A    def __init__(self):%0A        self.head = None%0A        self.length = 0%0A%0A    def __repr__(self):%0A        context = '%3C'%0A        start = self.head%0A        if not start:%0A            return context + '%3E'%0A        else:%0A            context += str(self.head)%0A%0A        while start._next:%0A            context += ', ' + str(start._next)%0A            start = start._next%0A        return context + '%3E'%0A%0A    def append(self, input_item):%0A        item = None%0A        if isinstance(input_item, node):%0A            item = input_item%0A        else:%0A            item = node(input_item)%0A%0A        if not self.head:%0A            self.head = item%0A            self.length += 1%0A        else:%0A            start = self.head%0A            while start._next:%0A                start = start._next%0A            start._next = item%0A            self.length += 1%0A%0A    def getIndex(self, val):%0A        start = self.head%0A        index = 0%0A        if self.length == 0:%0A            raise ValueError('%25s is not in chain' %25 str(val))%0A        while start:%0A            if start.val == val:%0A                return index%0A%0A            start = start._next%0A            index += 1%0A        raise ValueError('%25s is not in chain' %25 str(val))%0A%0A    def getVal(self, index):%0A        if self.length %3C= index:%0A            raise IndexError('chain index out of range')%0A%0A        start_index = 0%0A        start = self.head%0A        while start_index %3C index:%0A            start_index += 1%0A            start = start._next%0A        return start.val%0A%0A    def remove(self, val):%0A        start = self.head%0A        prev_start = None%0A        if self.length == 0:%0A            raise ValueError('remove(%25s): %25s is not in chain' %25%0A                             (str(val), str(val)))%0A        while start:%0A            if start.val == val:%0A                if prev_start:%0A                    prev_start._next = start._next%0A                else:%0A                    self.head = start._next%0A                self.length -= 1%0A                del start%0A                return%0A            prev_start = start%0A            start = start._next%0A%0A        raise ValueError('%25s is not in chain' %25 str(val))%0A

add chain