单链表插入两个数据代码
时间: 2023-09-26 22:06:59 浏览: 38
以下是单链表插入两个数据的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertNode(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
void printList(struct Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
}
int main() {
struct Node* head = NULL;
insertNode(&head, 1);
insertNode(&head, 2);
printList(head);
return 0;
}
```
希望能够帮到你!
相关问题
头歌数据结构与算法单链表插入
对于单链表的插入操作,有两种常见的情况:在链表的头部插入一个新节点和在链表的指定位置插入一个新节点。
1. 在链表头部插入新节点:
首先,创建一个新的节点,并将新节点的值设为待插入的值。
然后,将链表的头节点作为新节点的下一个节点。
最后,将新节点设为链表的新头节点。
2. 在链表指定位置插入新节点:
首先,遍历链表找到待插入位置的前一个节点。
然后,创建一个新的节点,并将新节点的值设为待插入的值。
接下来,将原来前一个节点的下一个节点设为新节点的下一个节点。
最后,将新节点设为原来前一个节点的下一个节点。
具体实现代码如下(使用Python语言):
```python
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def insertAtHead(head, value):
new_node = ListNode(value)
new_node.next = head
return new_node
def insertAtPosition(head, position, value):
if position == 0:
return insertAtHead(head, value)
cur = head
for _ in range(position-1):
if cur.next is None:
raise ValueError("Invalid position")
cur = cur.next
new_node = ListNode(value)
new_node.next = cur.next
cur.next = new_node
return head
```
使用示例:
```python
# 创建一个链表 1 -> 2 -> 4
head = ListNode(1)
head.next = ListNode(2)
head.next.next = ListNode(4)
# 在头部插入新节点 0
head = insertAtHead(head, 0)
# 在位置 2 插入新节点 3
head = insertAtPosition(head, 2, 3)
```
以上是单链表插入操作的基本思路和实现代码。在实际使用中,可以根据具体需求进行相应的修改和优化。
连接两个单链表的简单c语言代码
以下是连接两个单链表的简单 C 语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void printList(struct Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
}
void push(struct Node** head_ref, int new_data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
struct Node* concatenate(struct Node* head1, struct Node* head2) {
if (head1 == NULL) {
return head2;
}
if (head2 == NULL) {
return head1;
}
struct Node* ptr = head1;
while (ptr->next != NULL) {
ptr = ptr->next;
}
ptr->next = head2;
return head1;
}
int main() {
struct Node* head1 = NULL;
struct Node* head2 = NULL;
push(&head1, 3);
push(&head1, 2);
push(&head1, 1);
push(&head2, 6);
push(&head2, 5);
push(&head2, 4);
printf("List 1: ");
printList(head1);
printf("\nList 2: ");
printList(head2);
head1 = concatenate(head1, head2);
printf("\nConcatenated list: ");
printList(head1);
return 0;
}
```
这个程序定义了一个 `Node` 结构体,其中包含一个整数数据和一个指向下一个节点的指针。`printList` 函数用于打印链表中的所有元素。`push` 函数用于在链表的开头插入一个新节点。`concatenate` 函数用于连接两个链表,返回连接后的链表的头节点。在 `main` 函数中,我们创建了两个链表,将它们连接起来,并打印结果。
相关推荐
![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://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)