Python two-way linked list implementation instance code

Source: Internet
Author: User
Python two-way linked list is similar to a single-chain table, but a pointer pointing to the previous element is added. the following code illustrates the python two-way linked list method:

Python bidirectional linked list implementation code:

The code is as follows:


#! /Usr/bin/python
#-*-Coding: UTF-8 -*-

Class Node (object ):
Def _ init _ (self, val, p = 0 ):
Self. data = val
Self. next = p
Self. prev = p

Class LinkList (object ):
Def _ init _ (self ):
Self. head = 0

Def _ getitem _ (self, key ):

If self. is_empty ():
Print 'linklist is empty .'
Return

Elif key <0 or key> self. getlength ():
Print 'The given key is error'
Return

Else:
Return self. getitem (key)

Def _ setitem _ (self, key, value ):

If self. is_empty ():
Print 'linklist is empty .'
Return

Elif key <0 or key> self. getlength ():
Print 'The given key is error'
Return

Else:
Self. delete (key)
Return self. insert (key)

Def initlist (self, data ):

Self. head = Node (data [0])

P = self. head

For I in data [1:]:
Node = Node (I)
P. next = node
Node. prev = p
P = p. next

Def getlength (self ):

P = self. head
Length = 0
While p! = 0:
Length + = 1
P = p. next

Return length

Def is_empty (self ):

If self. getlength () = 0:
Return True
Else:
Return False

Def clear (self ):

Self. head = 0


Def append (self, item ):

Q = Node (item)
If self. head = 0:
Self. head = q
Else:
P = self. head
While p. next! = 0:
P = p. next
P. next = q
Q. prev = p


Def getitem (self, index ):

If self. is_empty ():
Print 'linklist is empty .'
Return
J = 0
P = self. head

While p. next! = 0 and j P = p. next
J + = 1

If j = index:
Return p. data

Else:

Print 'Target is not exist! '

Def insert (self, index, item ):

If self. is_empty () or index <0 or index> self. getlength ():
Print 'linklist is empty .'
Return

If index = 0:
Q = Node (item, self. head)

Self. head = q

P = self. head
Post = self. head
J = 0
While p. next! = 0 and j Post = p
P = p. next
J + = 1

If index = j:
Q = Node (item, p)
Post. next = q
Q. prev = post
Q. next = p
P. prev = q


Def delete (self, index ):

If self. is_empty () or index <0 or index> self. getlength ():
Print 'linklist is empty .'
Return

If index = 0:
Q = Node (item, self. head)

Self. head = q

P = self. head
Post = self. head
J = 0
While p. next! = 0 and j Post = p
P = p. next
J + = 1

If index = j:
Post. next = p. next
P. next. prev = post

Def index (self, value ):

If self. is_empty ():
Print 'linklist is empty .'
Return

P = self. head
I = 0
While p. next! = 0 and not p. data = value:
P = p. next
I + = 1

If p. data = value:
Return I
Else:
Return-1


L = LinkList ()
L. initlist ([1, 2, 3, 4, 5])
Print l. getitem (4)
L. append (6)
Print l. getitem (5)

L. insert (4, 40)
Print l. getitem (3)
Print l. getitem (4)
Print l. getitem (5)

L. delete (5)
Print l. getitem (5)

L. index (5)

Result;

5
6
4
40
5
6

The result is the same as that of a single-chain table.

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.