Implementation of bidirectional linked list of PYTHON--PYTHON data structure

Source: Internet
Author: User
Tags prev

Like a single-linked list, it just adds a pointer to the previous element.

Python implementation code:

#Personal Python Data Structure--ppds##!/usr/bin/python#-*-coding:utf-8-*-classNode (object):def __init__(self,val,p=0): Self.data=Val Self.next=P Self.prev=Pclasslinklist (object):def __init__(self): Self.head=0definitlist (self,data): Self.head=Node (data[0]) p=Self.head forIinchData[1:]: node=Node (i) P.next=node Node.prev=P P=P.nextdefGetLength (self): length=0 P=Self.head whileP! =0:length+=1P=P.nextreturnlengthdefIs_empty (self):ifSelf.getlength () = =0:returnTrueElse:            returnFalsedefClear (self): Self.head=0defAppend (self,item): Q=Node (item)ifSelf.head = =0:self.head=QElse: P=Self.head whileP.next! =0:p=P.next P.next=Q Q.prev=PdefGetItem (self,index):ifself.is_empty ():Print('linklist is empty.')            returnJ=0 P=Self.head whileP.next! = 0 andJ <index:p=P.next J+=1ifj = =Index:returnP.dataElse:            Print('target is not exist!')    defInsert (Self,index,item):ifSelf.is_empty ()orIndex < 0orIndex >self.getlength ():Print('linklist is empty.')            return        ifindex = =0:q=Node (item,self.head) Self.head=Q J=0 P=Self.head Post=Self.head whileP.next! = 0 andJ <Index:post=P P=P.next J+=1ifindex = =J:q=Node (item,p) Post.next=Q Q.prev=Post Q.next=P P.prev=QdefDelete (self,index):ifSelf.is_empty ()orIndex < 0orIndex >self.getlength ():Print('linklist is empty.')            return        ifindex = =0:q=Node (item,self.head) Self.head=Q J=0 P=Self.head Post=Self.head whileP.next! = 0 andJ <Index:post=P P=P.next J+=1ifindex = =J:post.next=P.next P.next.prev=PostdefIndex (self,value):ifself.is_empty ():Print('linklist is empty.')            returnI=0 P=Self.head whileP.next! = 0 and  notP.data = =value:p=P.next i+=1ifP.data = =Value:returnIElse:            return-1if __name__=='__main__': L=linklist () llist=[7,3,10,4,5,] l.initlist (llist)Print(L.getlength ())Print(L.is_empty ())#L.append (one)    #L.insert (2,100)    Print(L.getlength ())Print(L.getitem (0)) forIinchRange (L.getlength ()):Print(L.index (Llist[i])) l.clear ()Print(L.getlength ())

Implementation of bidirectional linked list of PYTHON--PYTHON data structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.