用python实现1、编写一组程序,基于单链表,用头插法建表,实现两个寝室学生姓名数据的建表、展示、查找、定位、插入、删除、判定表空和求表长的操作。依次输入两个宿舍学生姓名:自己宿舍和隔壁宿舍同学。测试要求如下: (1)展示两个宿舍所有学生的姓名及人数; (2)查找本宿舍长在表中的位置; (3)在表中自己的位置后面加入宿舍新成员“...”,删除宿舍里的已转走的学生“...”,展示两个宿舍现有的同学。
时间: 2023-12-06 14:39:40 浏览: 48
下面是使用Python实现的单链表程序:
```python
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def insert_at_beginning(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def display(self):
temp = self.head
while temp:
print(temp.data)
temp = temp.next
def search(self, data):
temp = self.head
pos = 1
while temp:
if temp.data == data:
return pos
temp = temp.next
pos += 1
return -1
def insert_at_position(self, data, pos):
if pos == 1:
self.insert_at_beginning(data)
else:
new_node = Node(data)
temp = self.head
for i in range(pos-2):
temp = temp.next
new_node.next = temp.next
temp.next = new_node
def delete(self, data):
temp = self.head
if temp and temp.data == data:
self.head = temp.next
temp = None
return
prev = None
while temp and temp.data != data:
prev = temp
temp = temp.next
if temp is None:
return
prev.next = temp.next
temp = None
def is_empty(self):
return self.head is None
def length(self):
temp = self.head
count = 0
while temp:
count += 1
temp = temp.next
return count
if __name__ == '__main__':
dorm1 = LinkedList()
dorm2 = LinkedList()
# input dorm1 students' names
while True:
name = input("Enter a student's name or 'done' to finish: ")
if name == 'done':
break
dorm1.insert_at_beginning(name)
# input dorm2 students' names
while True:
name = input("Enter a student's name or 'done' to finish: ")
if name == 'done':
break
dorm2.insert_at_beginning(name)
# display both dorms' students' names
print("Dorm 1:")
dorm1.display()
print("Total number of students:", dorm1.length())
print("Dorm 2:")
dorm2.display()
print("Total number of students:", dorm2.length())
# search for a student in dorm1
name = input("Enter a student's name to search in dorm 1: ")
pos = dorm1.search(name)
if pos == -1:
print("Student not found in dorm 1")
else:
print(name, "is at position", pos, "in dorm 1")
# insert a new student in dorm1
name = input("Enter the name of new student in dorm 1: ")
pos = int(input("Enter the position to insert the new student: "))
dorm1.insert_at_position(name, pos)
print("After adding a new student, the list is:")
dorm1.display()
# delete a student in dorm1
name = input("Enter the name of student to delete from dorm 1: ")
dorm1.delete(name)
print("After deleting the student, the list is:")
dorm1.display()
# delete a student who has transferred from dorm1
name = input("Enter the name of student who has transferred from dorm 1: ")
dorm1.delete(name)
print("After deleting the transferred student, the list is:")
dorm1.display()
# display both dorms' students' names
print("Dorm 1:")
dorm1.display()
print("Total number of students:", dorm1.length())
print("Dorm 2:")
dorm2.display()
print("Total number of students:", dorm2.length())
```
运行结果:
```
Enter a student's name or 'done' to finish: Alice
Enter a student's name or 'done' to finish: Bob
Enter a student's name or 'done' to finish: Charlie
Enter a student's name or 'done' to finish: done
Enter a student's name or 'done' to finish: Dave
Enter a student's name or 'done' to finish: Eve
Enter a student's name or 'done' to finish: Fred
Enter a student's name or 'done' to finish: done
Dorm 1:
Charlie
Bob
Alice
Total number of students: 3
Dorm 2:
Fred
Eve
Dave
Total number of students: 3
Enter a student's name to search in dorm 1: Alice
Alice is at position 3 in dorm 1
Enter the name of new student in dorm 1: Dan
Enter the position to insert the new student: 2
After adding a new student, the list is:
Charlie
Dan
Bob
Alice
Enter the name of student to delete from dorm 1: Bob
After deleting the student, the list is:
Charlie
Dan
Alice
Enter the name of student who has transferred from dorm 1: Alice
After deleting the transferred student, the list is:
Charlie
Dan
Dorm 1:
Charlie
Dan
Total number of students: 2
Dorm 2:
Fred
Eve
Dave
Total number of students: 3
```
阅读全文