2链表(python实现)

Posted Aluosen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2链表(python实现)相关的知识,希望对你有一定的参考价值。

# -*- coding:utf-8 -*-
#@Time : 2020/5/2 0:01
#@Author: Aluosen
#@File : Linklist.py
#节点Node
class Node:
def __init__(self,initdata):
self.data = initdata
self.next = None
def getData(self):
return self.data
def getNext(self):
return self.next
def setData(self,newdata):
self.data = newdata
def setNext(self,newnext):
self.next = newnext

#无序表
class UnorderedList:
def __init__(self):
self.head = None
def add(self,item):
temp = Node(item)
temp.setNext(self.head)
self.head = temp
def size(self):
current = self.head
count = 0
while current != None:
count = count + 1
current = current.getNext()
return count
def search(self,item):
current = self.head
found = False
while current != None and not found:
if current.getData() == item:
found = True
else:
current = current.getNext()
return found
def remove(self):
current = self.head
previous = None
found = False
while not found:
if current.getData() == item:
found = True
else:
previous,current = current,current.getNext()
if previous == None:
self.head = current.getNext()
else:
previous.getNext(current.getNext())

#有序表
class OrderList:
def __init__(self):
self.head = None
def search(self,item):
current = self.head
found = False
stop = False
while current != None and not found and not stop:
if current.getData() == item:
found = True
else:
current = current.getNext()
return found
def add(self,item):
current = self.head
previous = None
stop = False
while current != None and not stop:
if current.getData() > item:
stop = True
else:
previous = current
current = current.getNext()
temp = Node(item)
if previous == None:
temp.setNext(self.head)
self.head = temp
else:
temp.setNext(current)
previous.setNext(temp)

 

 



以上是关于2链表(python实现)的主要内容,如果未能解决你的问题,请参考以下文章

2链表(python实现)

Leetcode147-对链表进行插入排序(Python3实现)

Python数据结构学习笔记——链表:无序链表和有序链表

python实现链表反转(转置)

python实现双向链表

python实现双向链表