首先摘抄一下某博客上面的代码实现:http://www.cnblogs.com/yupeng/p/3413763.html
#!/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 initlist(self,data):
self.head = Node(data[0])#将链表头节点指向Node节点的第一个data数据
p = self.head
for i in data[1:]:#以data列表数据创建节点,并生成一个链表
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#c语言要回收内存,看样子python自动回收内存
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 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 jself.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
我的确是根据上面的代码才有的灵感,但是我感觉链表其实主要实现插入和删除操作,就简化了上面的代码,自己独立完成了单向链表的数据结构实现:
#http://www.cnblogs.com/yupeng/p/3413763.html 这是一篇好文章,但我要靠自己来实现单链表
class Node(object):
def __init__(self, data, next = None):
self.data = data
self.next = next
class ListNode(object):
def __inti__(self):
self.head = None
def ListInit(self, listdata):
self.head = Node(listdata[0])
p = self.head
for i in range(len(listdata) - 1):#我这个不如下面注释的简单,但这时我的思路
node = Node(listdata[i+1])
p.next = node
p = p.next
'''
for i in listdata[1:]:
node = Node(i)
p.next = node
p = p.next
'''
def getlength(self):
p = self.head
count = 0
while(p != None):
p = p.next
count += 1
return count
def is_empty(self):
if self.getlength() == 0:
return True
else:
return False
def ListAddtail(self, value):
node = Node(value)
p = self.head
while(p!= None):
pre = p
p = p.next
pre.next = node
'''
while(p!= None): #开始用下面的代码发现不对,这个和c语言不同,需要让原节点的node的next指针指向node节点才能插入该节点
p = p.next
p = node
'''
def ListAddtop(self, value):
p = self.head
node = Node(value)
node.next = p
self.head = node
def ListDelvalue(self, value):
p = self.head
if p.data == value:#如果头节点就找到要删除的值
self.head = p.next#就将第二个节点赋值给头节点,就相当于删除了第一个节点了
else:
while(p!= None):#如果不是头节点,就要循环查找
pre = p;
p = p.next
if p != None:#如果没有这句话,那么编译会报错误,并且也不好实现如果删除不存在的数据就报错的功能
if (p.data == value):
pre.next = p.next
break
else:
print("there is no data in listnode!")
alist = [1,2,3,4,5]
nodelist = ListNode()
nodelist.ListInit(alist)
lengh = nodelist.getlength()
print(lengh)
nodelist.ListAddtail(123)
lengh = nodelist.getlength()
print(lengh)
nodelist.ListAddtop(90)
lengh = nodelist.getlength()
print(lengh)
nodelist.ListDelvalue(90)
lengh = nodelist.getlength()
print(lengh)
nodelist.ListDelvalue(123)
lengh = nodelist.getlength()
print(lengh)
nodelist.ListDelvalue(12333)
lengh = nodelist.getlength()
print(lengh)
运行结果如下:
C:\Users\snowshuang\AppData\Local\Programs\Python\Python37\python.exe C:/Users/snowshuang/PycharmProjects/logging/log1.py
5
6
7
6
5
there is no data in listnode!
5
Process finished with exit code 0
不足之处是删除的时候,如果链表有重复元素,我只实现了第一个节点的删除,没有考虑多处节点删除