单链表头插法c++实现
时间: 2023-11-08 10:06:54 浏览: 64
单链表头插法是一种在链表头部插入新节点的方法,它的实现方法如下:
1. 定义一个头节点,将其next指针指向NULL。
2. 对于每个要插入的节点,将其next指针指向头节点的next指针所指向的节点,然后将头节点的next指针指向该节点。
3. 插入完成后,头节点的next指针所指向的节点即为链表的第一个节点。
下面是单链表头插法的C++实现代码:
```
struct Node {
int data;
Node* next;
};
Node* create() {
Node* first = new Node;
first->next = NULL;
return first;
}
void insert(Node* first, int data) {
Node* node = new Node;
node->data = data;
node->next = first->next;
first->next = node;
}
```
其中,create函数用于创建头节点,insert函数用于插入新节点。使用时,先调用create函数创建头节点,然后调用insert函数插入新节点即可。
相关问题
c++ 单链表头插法
c语言中的单链表头插法是一种创建链表的方法。它通过在头结点之后插入新节点来构建链表。下面是一个示例代码来演示如何使用头插法创建单链表:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义节点结构
typedef struct Node {
int data; // 数据域
struct Node* next; // 指针域
} Node;
// 头插法创建链表
Node* createList(int n) {
Node* head = (Node*)malloc(sizeof(Node)); // 创建头结点
head->next = NULL; // 初始化头结点的指针域
for (int i = 0; i < n; i++) {
Node* newNode = (Node*)malloc(sizeof(Node)); // 创建新节点
scanf("%d", &(newNode->data)); // 输入新节点的数据
newNode->next = head->next; // 将新节点的指针域指向头结点的后继节点
head->next = newNode; // 将头结点的指针域指向新节点
}
return head; // 返回链表的头结点
}
// 打印链表
void printList(Node* head) {
Node* p = head->next; // 从头结点的后继节点开始遍历链表
while (p != NULL) {
printf("%d\n", p->data);
p = p->next; // 移动到下一个节点
}
}
int main() {
int n;
scanf("%d", &n); // 输入链表的长度
Node* head = createList(n); // 创建链表
printList(head); // 打印链表
return 0;
}
```
以上代码中,首先定义了一个节点结构体 `Node`,其中包含数据域 `data` 和指针域 `next`。然后使用头插法创建链表的函数 `createList`,其中通过循环输入节点的数据,并将新节点插入到头结点之后。最后,通过函数 `printList` 打印链表的节点数据。
请注意,以上代码是使用C语言编写的。如果您想使用C++编写,可以将代码进行适当的修改。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [c++实现单链表头插法、尾插法、删除、遍历](https://blog.csdn.net/qq_45014727/article/details/114934481)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [数据结构:单链表前插法(头插法)C++](https://blog.csdn.net/weixin_45800887/article/details/108076723)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
C++实现单链表的插入删除
单链表的插入删除可以通过以下代码实现:
```c++
#include <iostream>
using namespace std;
// 定义链表节点结构体
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
// 在链表头插入节点
ListNode* insertAtHead(ListNode* head, int val) {
ListNode* node = new ListNode(val);
node->next = head;
return node;
}
// 在链表尾插入节点
ListNode* insertAtTail(ListNode* head, int val) {
ListNode* node = new ListNode(val);
if (head == NULL) {
head = node;
} else {
ListNode* cur = head;
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = node;
}
return head;
}
// 在链表中删除节点
ListNode* deleteNode(ListNode* head, int val) {
if (head == NULL) {
return NULL;
} else if (head->val == val) {
ListNode* newHead = head->next;
delete head;
return newHead;
} else {
ListNode* cur = head;
while (cur->next != NULL && cur->next->val != val) {
cur = cur->next;
}
if (cur->next != NULL) {
ListNode* temp = cur->next;
cur->next = cur->next->next;
delete temp;
}
return head;
}
}
// 打印链表
void printList(ListNode* head) {
ListNode* cur = head;
while (cur != NULL) {
cout << cur->val << " ";
cur = cur->next;
}
cout << endl;
}
int main() {
ListNode* head = NULL;
// 在链表头插入节点
head = insertAtHead(head, 1);
head = insertAtHead(head, 2);
head = insertAtHead(head, 3);
printList(head); // 3 2 1
// 在链表尾插入节点
head = insertAtTail(head, 4);
head = insertAtTail(head, 5);
head = insertAtTail(head, 6);
printList(head); // 3 2 1 4 5 6
// 在链表中删除节点
head = deleteNode(head, 3);
head = deleteNode(head, 6);
printList(head); // 2 1 4 5
return 0;
}
```