串的联结数据结构代码
时间: 2023-11-26 21:46:34 浏览: 34
以下是串的链式存储结构的Python代码实现,包括了串的联结操作:
```python
class StringNode:
def __init__(self, data):
self.data = data
self.next = None
class StringLinkedList:
def __init__(self):
self.head = None
def add(self, data):
new_node = StringNode(data)
if self.head is None:
self.head = new_node
return
current_node = self.head
while current_node.next is not None:
current_node = current_node.next
current_node.next = new_node
def join(self, other):
current_node = self.head
while current_node.next is not None:
current_node = current_node.next
current_node.next = other.head
def __str__(self):
current_node = self.head
string = ""
while current_node is not None:
string += current_node.data
current_node = current_node.next
return string
```
其中,`StringNode`类表示链表中的节点,包含一个`data`属性表示节点存储的字符,以及一个`next`属性表示下一个节点的引用。`StringLinkedList`类表示整个链表,包含一个`head`属性表示链表的头节点。`add`方法用于向链表中添加一个字符,`join`方法用于将另一个链表连接到当前链表的末尾,`__str__`方法用于将链表转换为字符串输出。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)