顺序线性表
三个基本属性
-
存储空间的起始位置data
-
线性表的最大存储量maxSize
-
线性表的当前长度length
Python实现
# Python 2.7
class seqList(object):
# 初始化
def __init__(self, maxSize):
self.data = [None] * maxSize
self.maxSize = maxSize
self.length = 0
# 获取顺序表长度
def getLength(self):
return self.length
# 清空顺序表
def clear(self):
for i in range(self.length):
self.data[i] = None
self.length = 0
# 是否为空
def is_empty(self):
if self.length == 0:
return True
else:
return False
# 是否满
def is_full(self):
if self.length == self.maxSize:
return True
else:
return False
# 根据索引获得数据
def get_elem(self, index):
if self.is_empty():
print 'seqList is empty!'
if index < 0 or index > self.length - 1:
print 'position is error!'
else:
return self.data[index]
# 根据数据获得索引
def locate_elem(self, elem):
if self.is_empty():
print 'seqList is empty!'
else:
for i in range(self.length):
if self.data[i] == elem:
return i
# 追加数据
def append(self, elem):
if self.is_full():
print 'seqList is full!'
else:
self.data[self.length] = elem
self.length += 1
# 插入数据
def insert(self, index, elem):
if self.is_full():
print 'seqList is full!'
else:
i = self.length
for item in self.data[self.length-1:index-1:-1]:
self.data[i] = item
i -= 1
self.data[index] = elem
self.length += 1
# 删除元素
def delete(self, index):
if self.is_empty():
print 'seqList is empty!'
else:
if index < 0 or index > self.length-1:
print 'Position is error!'
else:
for item in self.data[index+1:self.length]:
self.data[index] = item
index += 1
self.data[index] = None
self.length -= 1
链式线性表
节点的基本属性
-
数据域data
-
后继指针next
链表的基本属性
-
头结点
-
链表的长度
Python实现
# Python 2.7
# 节点
class Node(object):
def __init__(self, data, next=None):
self.data = data
self.next = next
# 链表
class lkList(object):
# 初始化
def __init__(self):
self.head = Node(None)
self.length = 0
# 是否为空
def is_empty(self):
if self.length == 0:
return True
else:
return False
# 获取链表长度
def getLength(self):
return self.length
# 插入元素
def insert(self, index, elem):
p = self.head
if index < 0 or index > self.length-1:
print 'Position is error!'
else:
for i in range(index):
p = p.next
node = Node(elem, p.next)
p.next = node
self.length += 1
# 追加元素
def append(self, elem):
p = self.head
for i in range(self.length):
p = p.next
node = Node(elem)
p.next = node
self.length += 1
# 清空链表
def clear(self):
p = self.head
p.next = None
self.length = 0
# 删除元素
def delete(self, index):
if self.is_empty():
print 'lkList is empty!'
else:
if index < 0 or index > self.length-1:
print 'Position is error!'
else:
p = self.head
for i in range(index):
p = p.next
q = p.next
p.next = q.next
self.length -= 1
# 获取元素
def get_elem(self, index):
if self.is_empty():
print 'lkList is empty!'
else:
if index < 0 or index > self.length-1:
print 'Position is error!'
else:
p = self.head
for i in range(index):
p = p.next
return p.next.data
# 查找某值的位置
def locate_elem(self, elem):
if self.is_empty():
print 'lkList is empty!'
else:
p = self.head
flag = False # 是否存在该值
for i in range(self.length):
p = p.next
if p.data == elem:
flag = True
return i
if not flag:
print 'elem not in lkList!'
# 构建链表
def create(self, dataList):
p = self.head
for item in dataList:
node = Node(item)
p.next = node
p = p.next
self.length += 1
# 展示链表
def show(self):
if self.is_empty():
print 'lkList is empty!'
else:
p = self.head
for i in range(self.length):
p = p.next
print p.data