Python single-linked list implementation code example

Source: Internet
Author: User
Definition of a linked list:
A linked list (linked list) is a data structure consisting of a set of data elements called nodes, each of which contains information about the node itself and the address that points to the next node. Since each node contains address information that can be linked, a variable can be used to access the entire sequence of nodes. In other words, the node contains two pieces of information: the value that is used to store the data element, called the information field, and a pointer to store the address of the next data element, called the pointer field. The address of the first node in the list is stored in a separate node, called the head node or the first node. The last node in the list has no successor element, and its pointer field is empty.

Python single-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

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
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


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.next = P


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

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)

Results:

5
6
4
40
5
6

  • 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.