输出带头结点单链表的所有结点元素值
时间: 2024-05-15 13:14:57 浏览: 19
对于带头结点的单链表,头结点不存储任何元素值,因此只需要遍历头结点的后继结点,输出其元素值即可。代码如下:
```python
def print_linked_list(head):
p = head.next # p指向头结点的后继结点
while p:
print(p.data)
p = p.next
```
其中,`head.next`表示头结点的后继结点,`p = head.next`将p指向头结点的后继结点,`while p:`表示在p不为空的情况下循环,每次循环输出p的元素值,并将p指向下一个结点,即`p = p.next`。
相关问题
输出带头结点单链表的函数
以下是带头结点单链表的函数实现:
```
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node, *LinkedList;
LinkedList createLinkedList() {
LinkedList L = (LinkedList)malloc(sizeof(Node));
L->next = NULL;
return L;
}
void insert(LinkedList L, int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = L->next;
L->next = newNode;
}
void delete(LinkedList L, int data) {
Node* p = L->next;
Node* pre = L;
while (p != NULL) {
if (p->data == data) {
pre->next = p->next;
free(p);
return;
}
pre = p;
p = p->next;
}
}
void display(LinkedList L) {
Node* p = L->next;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
void destroy(LinkedList L) {
Node* p = L->next;
while (p != NULL) {
Node* next = p->next;
free(p);
p = next;
}
free(L);
}
int main() {
LinkedList L = createLinkedList();
insert(L, 1);
insert(L, 2);
insert(L, 3);
display(L);
delete(L, 2);
display(L);
destroy(L);
return 0;
}
```
用c语言实现从尾到头反向输出带头结点单链表的值
当我们要从尾到头反向输出带头结点单链表的值时,可以使用递归的方式来实现。下面是用C语言实现的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义单链表节点结构
typedef struct ListNode {
int val;
struct ListNode* next;
} ListNode;
// 递归方式反向输出链表值
void reversePrint(ListNode* head) {
if (head->next != NULL) {
reversePrint(head->next);
}
if (head->val != -1) { // 过滤掉头结点的值
printf("%d ", head->val);
}
}
int main() {
// 创建头结点
ListNode* head = (ListNode*)malloc(sizeof(ListNode));
head->val = -1; // 头结点的值设为-1
head->next = NULL;
// 创建链表节点
ListNode* node1 = (ListNode*)malloc(sizeof(ListNode));
node1->val = 1;
node1->next = NULL;
ListNode* node2 = (ListNode*)malloc(sizeof(ListNode));
node2->val = 2;
node2->next = NULL;
ListNode* node3 = (ListNode*)malloc(sizeof(ListNode));
node3->val = 3;
node3->next = NULL;
// 构建链表
head->next = node1;
node1->next = node2;
node2->next = node3;
// 从尾到头反向输出链表值
reversePrint(head);
// 释放内存
free(node1);
free(node2);
free(node3);
free(head);
return 0;
}
```
这段代码创建了一个带头结点的单链表,并通过递归方式实现了从尾到头反向输出链表的值。输出结果为:3 2 1。注意在递归过程中,过滤掉了头结点的值(即值为-1的节点)。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)