写个创建链表头指针的代码
时间: 2023-06-04 09:06:01 浏览: 44
好的,下面是创建链表头指针的代码:
```
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* head = new ListNode(0);
```
请问还有什么需要帮助的吗?
相关问题
用C语言逆向建立动态链表,返回链表头指针head
以下是用C语言逆向建立动态链表并返回链表头指针head的代码:
```c
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* node = (struct Node*) malloc(sizeof(struct Node));
node->data = data;
node->next = NULL;
return node;
}
struct Node* createList() {
struct Node* head = NULL;
int data;
while (1) {
printf("Enter a number (0 to end): ");
scanf("%d", &data);
if (data == 0) {
break;
}
struct Node* node = createNode(data);
node->next = head;
head = node;
}
return head;
}
void traverseList(struct Node* head) {
struct Node* p = head;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
void destroyList(struct Node* head) {
struct Node* p = head;
while (p != NULL) {
struct Node* q = p;
p = p->next;
free(q);
}
}
int main() {
struct Node* head = createList();
traverseList(head);
destroyList(head);
return 0;
}
```
在 createList() 函数中,我们使用 while 循环逆向建立链表。首先,我们初始化 head 为 NULL,表示链表为空。然后,循环读取用户输入的数字,每读取一个数字,我们就创建一个新的节点,将它的 next 指向 head,然后将 head 指向新的节点。这样,我们就成功地逆向建立了链表。
最后,我们将链表的头指针 head 返回,以便在主函数中遍历链表、销毁链表等操作。
C语言链表头节点插入完整代码
下面是一个简单的示例代码,演示如何在链表的头节点插入新节点:
```c
#include <stdio.h>
#include <stdlib.h>
// 链表节点结构体
typedef struct Node {
int data; // 节点数据
struct Node* next; // 指向下一个节点的指针
} Node;
// 创建一个新节点
Node* create_node(int data) {
Node* new_node = (Node*) malloc(sizeof(Node));
new_node->data = data;
new_node->next = NULL;
return new_node;
}
// 在链表头插入新节点
void insert_at_head(Node** head_ref, int data) {
Node* new_node = create_node(data);
new_node->next = *head_ref;
*head_ref = new_node;
}
// 打印链表中所有节点的值
void print_list(Node* head) {
Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
Node* head = NULL;
// 插入值为 1, 2, 3 的节点
insert_at_head(&head, 1);
insert_at_head(&head, 2);
insert_at_head(&head, 3);
// 打印链表中所有节点的值
print_list(head);
return 0;
}
```
在上面的代码中,我们定义了一个 `Node` 结构体,表示链表中的一个节点。`create_node` 函数用于创建一个新节点,并返回指向新节点的指针。`insert_at_head` 函数用于在链表头插入新节点,它接受一个指向头节点指针的指针,以便能够修改头节点指针。`print_list` 函数用于打印链表中所有节点的值。在 `main` 函数中,我们创建一个空链表,并插入值为 1、2、3 的节点,然后打印链表中所有节点的值。
相关推荐
![](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)