提交 62817655 编写于 作者: W willing0228

Mon Mar 25 11:15:00 CST 2024 inscode

上级 0520122c
import streamlit as st
from streamlit_option_menu import option_menu
import inscode
# 设置Streamlit应用程序的标题
st.set_page_config(page_title="app name", layout="wide")
menu1="菜单1"
menu2="菜单2"
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
last_node = self.head
while last_node.next:
last_node = last_node.next
last_node.next = new_node
def insert(self, data, position):
new_node = Node(data)
if position == 0:
new_node.next = self.head
self.head = new_node
return
current_node = self.head
current_position = 0
while current_node.next and current_position != position - 1:
current_node = current_node.next
current_position += 1
if current_position == position - 1:
new_node.next = current_node.next
current_node.next = new_node
def delete(self, data):
if self.head is None:
return
if self.head.data == data:
self.head = self.head.next
return
current_node = self.head
while current_node.next and current_node.next.data != data:
current_node = current_node.next
if current_node.next and current_node.next.data == data:
current_node.next = current_node.next.next
def search(self, data):
current_node = self.head
while current_node:
if current_node.data == data:
return True
current_node = current_node.next
return False
def traverse(self):
data_list = []
current_node = self.head
while current_node:
data_list.append(current_node.data)
current_node = current_node.next
return data_list
def sort(self):
if self.head is None:
return
current_node = self.head
while current_node.next:
next_node = current_node.next
while next_node:
if current_node.data > next_node.data:
current_node.data, next_node.data = next_node.data, current_node.data
next_node = next_node.next
current_node = current_node.next
with st.sidebar:
menu = option_menu("菜单", [menu1, menu2],
icons=['house', "list-task"],
menu_icon="cast", default_index=0)
def main():
st.title("C语言数据结构链表")
linked_list = LinkedList()
option = st.sidebar.selectbox("请选择操作", ("添加元素", "插入元素", "删除元素", "查找元素", "遍历链表", "排序链表"))
if option == "添加元素":
st.subheader("添加元素")
data = st.text_input("请输入要添加的元素")
if st.button("添加"):
linked_list.append(data)
st.success(f"元素 {data} 已成功添加到链表")
elif option == "插入元素":
st.subheader("插入元素")
data = st.text_input("请输入要插入的元素")
position = st.text_input("请输入要插入的位置(从0开始)")
if st.button("插入"):
if not position.isdigit():
st.error("插入位置必须为数字")
elif int(position) < 0:
st.error("插入位置不能小于0")
else:
linked_list.insert(data, int(position))
st.success(f"元素 {data} 已成功插入到链表的位置 {position}")
elif option == "删除元素":
st.subheader("删除元素")
data = st.text_input("请输入要删除的元素")
if st.button("删除"):
linked_list.delete(data)
st.success(f"元素 {data} 已成功删除")
elif option == "查找元素":
st.subheader("查找元素")
data = st.text_input("请输入要查找的元素")
if st.button("查找"):
if linked_list.search(data):
st.success(f"元素 {data} 存在于链表中")
else:
st.warning(f"元素 {data} 不存在于链表中")
elif option == "遍历链表":
st.subheader("遍历链表")
data_list = linked_list.traverse()
if data_list:
st.write("链表中的元素为:")
for data in data_list:
st.write(f"- {data}")
else:
st.warning("链表为空,无法遍历")
elif option == "排序链表":
st.subheader("排序链表")
if st.button("排序"):
linked_list.sort()
st.success("链表已成功排序")
if menu == menu1:
st.subheader(f"{menu1}")
if st.button("清空链表"):
linked_list = LinkedList()
st.success("链表已成功清空")
if menu == menu2:
st.subheader(f"{menu2}")
if __name__ == '__main__':
main()
if __name__ == "__main__":
main()
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册