1.请编程实现一个对顺序表进行基本操作的系统,主要包括表的创建、输入、查询、取值、插入、删除和输出等操作。

时间: 2023-12-20 07:04:59 浏览: 30
以下是一个基本的顺序表操作系统的 Python 代码实现: ```python class SeqList: def __init__(self, max_size): self.max_size = max_size self.data = [None] * max_size self.length = 0 def is_empty(self): return self.length == 0 def is_full(self): return self.length == self.max_size def insert(self, index, value): if self.is_full(): print("Error: SeqList is full!") return False if index < 0 or index > self.length: print("Error: Invalid index!") return False for i in range(self.length, index, -1): self.data[i] = self.data[i-1] self.data[index] = value self.length += 1 return True def delete(self, index): if self.is_empty(): print("Error: SeqList is empty!") return False if index < 0 or index >= self.length: print("Error: Invalid index!") return False for i in range(index, self.length-1): self.data[i] = self.data[i+1] self.data[self.length-1] = None self.length -= 1 return True def get_value(self, index): if index < 0 or index >= self.length: print("Error: Invalid index!") return None return self.data[index] def search(self, value): for i in range(self.length): if self.data[i] == value: return i return -1 def input_data(self): for i in range(self.max_size): value = int(input("Please input value for index %d: " % i)) if value == -1: break self.data[i] = value self.length += 1 def output_data(self): print("SeqList: ", end="") for i in range(self.length): print(self.data[i], end=" ") print() max_size = 10 seq_list = SeqList(max_size) while True: print("1. Create SeqList") print("2. Insert element") print("3. Delete element") print("4. Get value of an element") print("5. Search element") print("6. Output SeqList") print("0. Exit") choice = int(input("Please input your choice: ")) if choice == 0: break elif choice == 1: seq_list.input_data() elif choice == 2: index = int(input("Please input the index to insert: ")) value = int(input("Please input the value to insert: ")) seq_list.insert(index, value) elif choice == 3: index = int(input("Please input the index to delete: ")) seq_list.delete(index) elif choice == 4: index = int(input("Please input the index to get value: ")) value = seq_list.get_value(index) if value is not None: print("Value of index %d is: %d" % (index, value)) elif choice == 5: value = int(input("Please input the value to search: ")) index = seq_list.search(value) if index != -1: print("Value %d is at index %d" % (value, index)) else: print("Value %d not found!" % value) elif choice == 6: seq_list.output_data() else: print("Invalid choice, please try again!") ``` 运行上述代码,可以实现一个简单的顺序表操作系统,支持创建、插入、删除、取值、查询和输出等基本操作。通过菜单式的交互界面,用户可以方便地进行各种操作。

相关推荐

最新推荐

recommend-type

模拟通讯录-数据结构(顺序表实现基本功能).doc

模拟通讯录,设计相关数据结构,用菜单选择方式实现:通讯录的建立、通讯联系人的插入、删除、修改、查找等功能。
recommend-type

实验一:顺序表基本操作

大学生实验作业
recommend-type

C语言实现顺序表的顺序查找和折半查找

主要为大家详细介绍了C语言实现顺序表的顺序查找和折半查找,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

员工考勤系统.docx

员工考勤系统.docx
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB图像处理算法宝典:从理论到实战

![MATLAB图像处理算法宝典:从理论到实战](https://img-blog.csdnimg.cn/20200717112736401.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2d1emhhbzk5MDE=,size_16,color_FFFFFF,t_70) # 1. MATLAB图像处理基础理论 MATLAB图像处理是一种利用MATLAB编程语言进行图像处理的强大工具。它提供了丰富的函数和工具箱,用于图像获取、增强、分
recommend-type

matlab中1/x的非线性规划

在MATLAB中,可以使用非线性规划函数(`fmincon`)来优化一个包含1/x的非线性目标函数。下面是一个简单的例子: ```matlab % 定义目标函数 fun = @(x) 1/x; % 定义约束函数(这里没有约束) nonlcon = []; % 定义初始点 x0 = 1; % 定义优化选项 options = optimoptions('fmincon', 'Display', 'iter'); % 进行非线性规划 [x, fval] = fmincon(fun, x0, [], [], [], [], [], [], nonlcon, options); ``` 在
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依