Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'subject:Python': 0.05; 'none,': 0.05; '21,': 0.07; 'python': 0.09; 'self.data': 0.09; 'sep': 0.09; 'subject:using': 0.09; 'def': 0.10; 'elements,': 0.16; 'node,': 0.16; 'remove(self,': 0.16; 'value:': 0.16; 'wrote:': 0.17; 'char': 0.17; 'element': 0.17; 'yield': 0.17; '>>>': 0.18; 'seems': 0.23; 'raise': 0.24; 'header:In-Reply-To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'fine': 0.28; 'node': 0.29; 'skip:_ 10': 0.29; 'probably': 0.29; 'class': 0.29; "i'm": 0.29; 'fri,': 0.30; '(and': 0.32; 'implement': 0.32; 'print': 0.32; 'shift': 0.33; 'to:addr:python- list': 0.33; 'received:google.com': 0.34; 'data,': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'next': 0.35; 'but': 0.36; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'skip:l 20': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'skip:n 10': 0.63; 'become': 0.65; '"remove"': 0.81; 'to:name:python': 0.84; 'comment.': 0.91; 'dennis': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=34oASaGo0prCJZqc5TAabVDRkSuBHjOL1PqQH3uUAzo=; b=nK3qIcrnIbDyJVQCR+2qjhUKAwg1JpsClFiwvnKSX38qHE76BUAvabscUi2eHv/OCz YBSKEyqBLlaM89qKKS3gwZMmrycodBz60v871x3FUSFbJQFPwZF/QgRY875MCjtf90KL kVTozidPZpoiA681S3Jib7DrB5LPXLToEsVu8H4oM4qr+j1/HBgvymLGp+O5MsdAGj7O ZT13ZViUL5v4Efy65ELZTXGpvYI8QrDbtGODVyf7hg0xWww5FIinCP1e5PwGArnvnyx0 YBfSnqZ5ppyN2wEeTYsmNw458TEzjLwcx1pXwZlEvZRyDomUR1Lqq5NvQJUw68rYEpch SvmQ== MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Fri, 21 Sep 2012 14:07:01 -0600 Subject: Re: Algorithms using Python? To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1348258054 news.xs4all.nl 6912 [2001:888:2000:d::a6]:48118 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29670 On Fri, Sep 21, 2012 at 1:45 PM, Dennis Lee Bieber wrote: > You can probably implement them, but they're not going to be very > efficient. (And never "remove" an element from the linked-list > implementation because Python would shift all the other elements, hence > your "links" become invalid). I'm not sure what you mean by that last comment. class Node(object): def __init__(self, data, next): self.data = data self.next = next class LinkedList(object): def __init__(self): self._head = None def __iter__(self): node = self._head while node: yield node.data node = node.next def insert_front(self, value): self._head = Node(value, self._head) def remove(self, value): prior, node = None, self._head while node: if node.data == value: if prior: prior.next = node.next else: self._head = node.next break prior, node = node, node.next else: raise ValueError("value not found") >>> li = LinkedList() >>> for char in 'edcba': ... li.insert_front(char) ... >>> print ''.join(li) abcde >>> li.remove('c') >>> print ''.join(li) abde It seems to work fine to me.