Python顺序表实现:基本运算、高级操作与菜单驱动
以下是 Python 代码实现:
class SeqList:
def __init__(self, maxsize=100):
self.maxsize = maxsize
self.data = [None] * maxsize
self.length = 0
def __len__(self):
return self.length
def is_empty(self):
return self.length == 0
def is_full(self):
return self.length == self.maxsize
def clear(self):
self.length = 0
def __getitem__(self, index):
if not 0 <= index < self.length:
raise IndexError('Index out of range.')
return self.data[index]
def __setitem__(self, index, value):
if not 0 <= index < self.length:
raise IndexError('Index out of range.')
self.data[index] = value
def __contains__(self, value):
for i in range(self.length):
if self.data[i] == value:
return True
return False
def index(self, value):
for i in range(self.length):
if self.data[i] == value:
return i
raise ValueError('Value not found.')
def count(self, value):
count = 0
for i in range(self.length):
if self.data[i] == value:
count += 1
return count
def append(self, value):
if self.length == self.maxsize:
raise ValueError('List is full.')
self.data[self.length] = value
self.length += 1
def insert(self, index, value):
if self.length == self.maxsize:
raise ValueError('List is full.')
if not 0 <= index <= self.length:
raise IndexError('Index out of range.')
for i in range(self.length, index, -1):
self.data[i] = self.data[i-1]
self.data[index] = value
self.length += 1
def remove(self, value):
for i in range(self.length):
if self.data[i] == value:
for j in range(i, self.length-1):
self.data[j] = self.data[j+1]
self.length -= 1
return
raise ValueError('Value not found.')
def pop(self, index=-1):
if self.length == 0:
raise ValueError('List is empty.')
if not -self.length <= index < self.length:
raise IndexError('Index out of range.')
if index < 0:
index += self.length
value = self.data[index]
for i in range(index, self.length-1):
self.data[i] = self.data[i+1]
self.length -= 1
return value
def delete_range(self, start, end):
if not 0 <= start <= end < self.length:
raise IndexError('Index out of range.')
count = end - start + 1
for i in range(start, self.length-count):
self.data[i] = self.data[i+count]
self.length -= count
def unique(self):
i = 0
while i < self.length:
j = i + 1
while j < self.length:
if self.data[j] == self.data[i]:
self.delete_range(j, j)
else:
j += 1
i += 1
def partition(self, value):
i = 0
j = self.length - 1
while i < j:
while i < j and self.data[j] > value:
j -= 1
while i < j and self.data[i] <= value:
i += 1
if i < j:
self.data[i], self.data[j] = self.data[j], self.data[i]
self.data[0], self.data[i] = self.data[i], self.data[0]
def sort(self):
for i in range(1, self.length):
value = self.data[i]
j = i - 1
while j >= 0 and self.data[j] > value:
self.data[j+1] = self.data[j]
j -= 1
self.data[j+1] = value
def display(self):
print('[', end='')
for i in range(self.length):
print(self.data[i], end='')
if i < self.length - 1:
print(',', end='')
print(']')
def show_menu():
print('1. Display')
print('2. Get element by index')
print('3. Get index by value')
print('4. Insert element')
print('5. Delete element')
print('6. Delete elements in range')
print('7. Unique')
print('8. Partition')
print('9. Sort')
print('10. Quit')
def main():
lst = SeqList()
while True:
show_menu()
choice = input('Enter your choice: ')
if choice == '1':
lst.display()
elif choice == '2':
index = int(input('Enter index: '))
try:
value = lst[index]
print('Value:', value)
except IndexError:
print('Index out of range.')
elif choice == '3':
value = int(input('Enter value: '))
try:
index = lst.index(value)
print('Index:', index)
except ValueError:
print('Value not found.')
elif choice == '4':
index = int(input('Enter index: '))
value = int(input('Enter value: '))
try:
lst.insert(index, value)
except IndexError:
print('Index out of range.')
except ValueError:
print('List is full.')
elif choice == '5':
index = int(input('Enter index: '))
try:
lst.pop(index)
except IndexError:
print('Index out of range.')
except ValueError:
print('List is empty.')
elif choice == '6':
start = int(input('Enter start index: '))
end = int(input('Enter end index: '))
try:
lst.delete_range(start, end)
except IndexError:
print('Index out of range.')
elif choice == '7':
lst.unique()
elif choice == '8':
value = int(input('Enter value: '))
lst.partition(value)
elif choice == '9':
lst.sort()
elif choice == '10':
break
else:
print('Invalid choice.')
if __name__ == '__main__':
main()
运行程序后,会输出一个菜单,用户可以根据需要选择相应的功能。在实现各种基本运算的过程中,我们都利用了顺序表的基本操作,如访问元素、插入元素、删除元素等。需要注意的是,在删除元素和删除一段元素的操作中,我们采用了两种不同的实现方式,即 pop 和 delete_range 方法,分别用于删除单个元素和删除一段元素。此外,我们还实现了顺序表的去重、划分和排序等高级操作。
原文地址: https://www.cveoy.top/t/topic/lYuA 著作权归作者所有。请勿转载和采集!