编程实现单向链表的基本操作: 1.插入元素 2.删除元素 3.获取第i个结点的数据元素 4.判断元素是否存在-----前面4个已提供源码参考 5.获取某个元素的下一个元素;NextElem(L, cur_e, &next_e);
时间: 2023-09-24 07:10:29 浏览: 52
### 回答1:
elem)
6.获取链表长度;ListLength(L)
1.插入元素
```python
def ListInsert(L, i, elem):
# 在链表中第i个位置插入元素elem
if i < 1: # i值不合法
print("Error: i is out of range.")
return False
elif i == 1: # 在最前面插入元素
new_node = ListNode(elem)
new_node.next = L.head
L.head = new_node
else:
p = L.head
j = 1
while p.next and j < i - 1: # 遍历链表找到第i个结点的前一个结点
p = p.next
j += 1
if not p.next and j < i - 1: # i值超过了链表长度
print("Error: i is out of range.")
return False
else:
new_node = ListNode(elem)
new_node.next = p.next
p.next = new_node
L.length += 1
return True
```
2.删除元素
```python
def ListDelete(L, i):
# 删除链表中第i个结点
if i < 1 or i > L.length:
print("Error: i is out of range.")
return False
elif i == 1:
L.head = L.head.next
else:
p = L.head
j = 1
while j < i - 1:
p = p.next
j += 1
p.next = p.next.next
L.length -= 1
return True
```
3.获取第i个结点的数据元素
```python
def GetElem(L, i):
# 获取链表中第i个结点的数据元素
if i < 1 or i > L.length:
print("Error: i is out of range.")
return None
p = L.head
j = 1
while j < i:
p = p.next
j += 1
return p.data
```
4.判断元素是否存在
```python
def ElemExist(L, elem):
# 判断元素elem是否在链表中存在
p = L.head
while p:
if p.data == elem:
return True
else:
p = p.next
return False
```
5.获取某个元素的下一个元素
```python
def NextElem(L, cur_e, elem):
# 获取链表中元素cur_e的下一个元素(即elem)
p = L.head
while p:
if p.data == cur_e and p.next:
return p.next.data
else:
p = p.next
print("Error: cur_e is not in the list or has no next element.")
return None
```
6.获取链表长度
```python
def ListLength(L):
# 获取链表长度
return L.length
```
### 回答2:
以下是实现单向链表基本操作的源码参考:
```python
# 定义链表节点类
class Node:
def __init__(self, data):
self.data = data
self.next = None
# 定义链表类
class LinkedList:
def __init__(self):
self.head = None
# 1. 插入元素
def insert(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
else:
cur = self.head
while cur.next:
cur = cur.next
cur.next = new_node
# 2. 删除元素
def delete(self, data):
if not self.head:
return
if self.head.data == data:
self.head = self.head.next
else:
cur = self.head
while cur.next:
if cur.next.data == data:
cur.next = cur.next.next
return
cur = cur.next
# 3. 获取第i个结点的数据元素
def get_element(self, i):
if not self.head or i < 1:
return None
cur = self.head
count = 1
while count < i and cur.next:
cur = cur.next
count += 1
if count == i:
return cur.data
else:
return None
# 4. 判断元素是否存在
def is_exist(self, data):
cur = self.head
while cur:
if cur.data == data:
return True
cur = cur.next
return False
# 5. 获取某个元素的下一个元素
def next_element(self, data):
cur = self.head
while cur:
if cur.data == data and cur.next:
return cur.next.data
cur = cur.next
return None
```
以上代码实现了单向链表的基本操作:插入元素、删除元素、获取第i个结点的数据元素、判断元素是否存在以及获取某个元素的下一个元素。你可以根据需要调用相应的方法来进行链表操作。
### 回答3:
单向链表是一种数据结构,每个结点包含数据元素和指向下一个结点的指针。编程实现单向链表的基本操作包括插入元素、删除元素、获取第i个结点的数据元素以及判断元素是否存在。下面给出了这四个操作的源码参考,以及额外提供了第五个操作:获取某个元素的下一个元素。
1. 插入元素
```python
def insert(L, data):
new_node = Node(data)
if L.head is None:
L.head = new_node
else:
current = L.head
while current.next is not None:
current = current.next
current.next = new_node
```
2. 删除元素
```python
def delete(L, data):
if L.head is None:
return
if L.head.data == data:
L.head = L.head.next
return
current = L.head
while current.next is not None:
if current.next.data == data:
current.next = current.next.next
return
current = current.next
```
3. 获取第i个结点的数据元素
```python
def get_node_data(L, i):
current = L.head
count = 0
while current is not None:
if count == i:
return current.data
count += 1
current = current.next
return None
```
4. 判断元素是否存在
```python
def element_exists(L, data):
current = L.head
while current is not None:
if current.data == data:
return True
current = current.next
return False
```
5. 获取某个元素的下一个元素
```python
def get_next_element(L, data):
current = L.head
while current is not None:
if current.data == data:
if current.next is not None:
return current.next.data
else:
return None
current = current.next
return None
```
以上为单向链表的基本操作的源码参考,可以根据需要进行调用和修改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)