运行程序,屏幕显示:“请输入你想建立的顺序表的元素,以?结束:” 输入:54381 你建立的顺序表为:5—>4—>3—>8—>1 顺序表的长度为:5 输入你想查找的元素:4 你查找的元素为4序位为2 输入你想查找的元素序位:4 你查找的元素为:8 输入你想插入的元素以及序位:<用逗号隔开>":6,3 插入后顺序表为:5—>4—>6—>3—>8—>1 请输入你想删除的元素序位:5 删除后的顺序表为:5—>4—>6—>3—>1
时间: 2024-02-12 12:09:32 浏览: 11
好的,下面是一个简单的Python程序,实现了上述功能:
```python
# 定义顺序表类
class SeqList:
def __init__(self, data=[]):
self.data = data
def __len__(self):
return len(self.data)
def __getitem__(self, index):
return self.data[index]
def __setitem__(self, index, value):
self.data[index] = value
# 在指定位置插入元素
def insert(self, index, value):
self.data.insert(index, value)
# 删除指定位置元素
def delete(self, index):
del self.data[index]
# 主函数
if __name__ == '__main__':
# 输入元素,以?结束
data = input("请输入你想建立的顺序表的元素,以?结束:")
data = [int(x) for x in data.split('?')[0].split()]
# 建立顺序表
seq = SeqList(data)
print("你建立的顺序表为:", end="")
for i in range(len(seq)):
print(seq[i], end="—>")
print()
# 输出顺序表长度
print("顺序表的长度为:", len(seq))
# 查找元素
value = int(input("输入你想查找的元素:"))
index = seq.data.index(value)
print("你查找的元素为%d,序位为%d" % (value, index+1))
# 查找序位
index = int(input("输入你想查找的元素序位:"))
value = seq[index-1]
print("你查找的元素为:%d" % value)
# 插入元素
value, index = input("输入你想插入的元素以及序位:<用逗号隔开>:").split(',')
value, index = int(value), int(index)
seq.insert(index-1, value)
print("插入后顺序表为:", end="")
for i in range(len(seq)):
print(seq[i], end="—>")
print()
# 删除元素
index = int(input("请输入你想删除的元素序位:"))
seq.delete(index-1)
print("删除后的顺序表为:", end="")
for i in range(len(seq)):
print(seq[i], end="—>")
print()
```
希望对你有所帮助!
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![text/plain](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)