数据结构与算法"."和"->"区别
时间: 2023-11-13 17:04:10 浏览: 44
数据结构和算法是计算机科学中的两个重要概念。其中,数据结构是指数据的组织方式,而算法则是指解决问题的方法。在C++中,"."和"->"都是用来访问结构体或类中的成员变量或成员函数的运算符,但它们的使用场景略有不同。
"."运算符用于访问结构体或类的成员变量或成员函数,而且只能用于结构体或类的实例对象上。例如,如果有一个结构体Person,其中有一个成员变量name,那么可以通过person.name来访问这个成员变量。
"->"运算符则用于访问结构体或类的成员变量或成员函数,但它可以用于结构体或类的指针上。例如,如果有一个指向Person结构体的指针p,其中有一个成员变量name,那么可以通过p->name来访问这个成员变量。
因此,"."和"->"的区别在于它们能够访问的对象不同,"."只能用于实例对象上,而"->"可以用于指针上。
相关问题
四、写一段程序,让一个单链表的后一半元素反转。 如 1->2->3->5->6->7 处理后为 1->2->3->7->6->5 如 1->2->3->8->9 处理后为 1->2->3->9->8 链表数据结构同题三。描述你的算法。 描述:
算法描述:
首先,我们需要找到链表的中间节点。可以使用快慢指针的方法,一个指针每次移动两步,另一个指针每次移动一步,当快指针到达链表末尾时,慢指针正好在中间节点位置。
接下来,我们将中间节点之后的链表进行反转。可以使用三个指针的方法,依次遍历链表节点,并修改节点的指针方向,将当前节点的next指向前一个节点。
最后,我们将链表的前半部分和反转后的后半部分进行合并。可以使用两个指针分别指向两部分的头节点,依次连接两部分节点即可。
具体实现代码如下:
```python
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def reverseSecondHalf(head):
if not head or not head.next:
return head
# 寻找中间节点
slow = head
fast = head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
# 反转后半部分
pre = None
cur = slow.next
while cur:
nxt = cur.next
cur.next = pre
pre = cur
cur = nxt
slow.next = pre
return head
# 创建链表
head = ListNode(1)
node2 = ListNode(2)
node3 = ListNode(3)
node4 = ListNode(5)
node5 = ListNode(6)
node6 = ListNode(7)
head.next = node2
node2.next = node3
node3.next = node4
node4.next = node5
node5.next = node6
# 反转后半部分
head = reverseSecondHalf(head)
# 打印链表
while head:
print(head.val, end="->")
head = head.next
```
输出结果为:1->2->3->7->6->5->
输入一个有向图,其顶点数和弧数均为6: 66 12 13 30:32 24 45 51 63 要求输出其DFS遍历结果为1->2->4->5->3 ->6或1->3->2->4->5->6,要求输出其BFS遍历结果为1->2->3->4->5->6或1->3->2->4-> 5->6。 注意:DFS和BFS遍历算法都应支持非强连通的有向图。
以下是Python代码实现:
```python
# DFS遍历
def dfs(graph, start):
visited = set() # 记录已经访问过的节点
stack = [start] # 使用栈作为辅助数据结构
result = [] # 记录遍历结果
while stack:
node = stack.pop()
if node not in visited:
visited.add(node)
result.append(node)
# 将未访问过的邻居节点入栈
stack.extend([n for n in graph[node] if n not in visited])
return result
# BFS遍历
def bfs(graph, start):
visited = set() # 记录已经访问过的节点
queue = [start] # 使用队列作为辅助数据结构
result = [] # 记录遍历结果
while queue:
node = queue.pop(0)
if node not in visited:
visited.add(node)
result.append(node)
# 将未访问过的邻居节点入队
queue.extend([n for n in graph[node] if n not in visited])
return result
# 构建图
graph = {
1: [2, 3],
2: [4, 5],
3: [6],
4: [5],
5: [],
6: []
}
# DFS遍历结果
print(dfs(graph, 1)) # 输出:[1, 3, 6, 2, 5, 4]
# BFS遍历结果
print(bfs(graph, 1)) # 输出:[1, 2, 3, 4, 5, 6]
```
其中,`graph` 是一个字典,表示有向图的邻接表,键为顶点,值为该顶点的邻居节点列表。`dfs` 和 `bfs` 分别是 DFS 和 BFS 遍历算法的实现,`start` 参数表示起始节点。运行结果如下:
```
[1, 3, 6, 2, 5, 4]
[1, 2, 3, 4, 5, 6]
```
其中,DFS 遍历的结果可以有多种可能性,但BFS遍历结果唯一。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)