資料結構及演算法--線性表
阿新 • • 發佈:2018-12-08
順序線性表
三個基本屬性
儲存空間的起始位置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
完整程式碼見我的Github
廣告時間
個人部落格:http://ruanshubin.top
GitHub:https://github.com/Ruanshubin/
歡迎您掃一掃上面的二維碼,關注我的微信公眾號!